fix(ct-react+ct-react17): only pass children to React.createElement when they are defined (#29592)

This commit is contained in:
Jeppe Reinhold 2024-02-23 21:30:42 +01:00 committed by GitHub
parent 034b550810
commit 4d868f6ba8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 59 additions and 2 deletions

View file

@ -40,7 +40,13 @@ function __pwRender(value) {
if (isJsxComponent(v)) { if (isJsxComponent(v)) {
const component = v; const component = v;
const props = component.props ? __pwRender(component.props) : {}; const props = component.props ? __pwRender(component.props) : {};
return { result: __pwReact.createElement(/** @type { any } */ (component.type), { ...props, children: undefined }, props.children) }; const {children, ...propsWithoutChildren} = props;
/** @type {[any, any, any?]} */
const createElementArguments = [component.type, propsWithoutChildren];
if(children){
createElementArguments.push(children);
}
return { result: __pwReact.createElement(...createElementArguments) };
} }
}); });
} }

View file

@ -40,7 +40,14 @@ function __pwRender(value) {
if (isJsxComponent(v)) { if (isJsxComponent(v)) {
const component = v; const component = v;
const props = component.props ? __pwRender(component.props) : {}; const props = component.props ? __pwRender(component.props) : {};
return { result: __pwReact.createElement(/** @type { any } */ (component.type), { ...props, children: undefined }, props.children) };
const {children, ...propsWithoutChildren} = props;
/** @type {[any, any, any?]} */
const createElementArguments = [component.type, propsWithoutChildren];
if(children){
createElementArguments.push(children);
}
return { result: __pwReact.createElement(...createElementArguments) };
} }
}); });
} }

View file

@ -0,0 +1,16 @@
type DefaultChildrenProps = {
children?: any;
}
export default function CheckChildrenProp(props: DefaultChildrenProps) {
const content = 'children' in props ? props.children : 'No Children';
return <div>
<h1>Welcome!</h1>
<main>
{content}
</main>
<footer>
Thanks for visiting.
</footer>
</div>
}

View file

@ -1,5 +1,6 @@
import { test, expect } from '@playwright/experimental-ct-react'; import { test, expect } from '@playwright/experimental-ct-react';
import Button from '@/components/Button'; import Button from '@/components/Button';
import CheckChildrenProp from '@/components/CheckChildrenProp';
import DefaultChildren from '@/components/DefaultChildren'; import DefaultChildren from '@/components/DefaultChildren';
import MultipleChildren from '@/components/MultipleChildren'; import MultipleChildren from '@/components/MultipleChildren';
@ -58,3 +59,8 @@ test('render number as child', async ({ mount }) => {
const component = await mount(<DefaultChildren>{1337}</DefaultChildren>); const component = await mount(<DefaultChildren>{1337}</DefaultChildren>);
await expect(component).toContainText('1337'); await expect(component).toContainText('1337');
}); });
test('render without children', async ({ mount }) => {
const component = await mount(<CheckChildrenProp />);
await expect(component).toContainText('No Children');
});

View file

@ -0,0 +1,16 @@
type DefaultChildrenProps = {
children?: any;
}
export default function CheckChildrenProp(props: DefaultChildrenProps) {
const content = 'children' in props ? props.children : 'No Children';
return <div>
<h1>Welcome!</h1>
<main>
{content}
</main>
<footer>
Thanks for visiting.
</footer>
</div>
}

View file

@ -1,5 +1,6 @@
import { test, expect } from '@playwright/experimental-ct-react17'; import { test, expect } from '@playwright/experimental-ct-react17';
import Button from '@/components/Button'; import Button from '@/components/Button';
import CheckChildrenProp from '@/components/CheckChildrenProp';
import DefaultChildren from '@/components/DefaultChildren'; import DefaultChildren from '@/components/DefaultChildren';
import MultipleChildren from '@/components/MultipleChildren'; import MultipleChildren from '@/components/MultipleChildren';
@ -58,3 +59,8 @@ test('render number as child', async ({ mount }) => {
const component = await mount(<DefaultChildren>{1337}</DefaultChildren>); const component = await mount(<DefaultChildren>{1337}</DefaultChildren>);
await expect(component).toContainText('1337'); await expect(component).toContainText('1337');
}); });
test('render without children', async ({ mount }) => {
const component = await mount(<CheckChildrenProp />);
await expect(component).toContainText('No Children');
});