Skip to content

Commit

Permalink
fix(ct): solid pass children when they are defined (#29648)
Browse files Browse the repository at this point in the history
  • Loading branch information
sand4rt authored Feb 26, 2024
1 parent 7eb910a commit 7e502e9
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 64 deletions.
48 changes: 12 additions & 36 deletions packages/playwright-ct-solid/registerSource.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@

import { render as __pwSolidRender, createComponent as __pwSolidCreateComponent } from 'solid-js/web';
import __pwH from 'solid-js/h';

/** @typedef {import('../playwright-ct-core/types/component').JsxComponent} JsxComponent */
/** @typedef {() => import('solid-js').JSX.Element} FrameworkComponent */

/**
* @param {any} component
Expand All @@ -32,42 +30,20 @@ function isJsxComponent(component) {
}

/**
* @param {any} child
*/
function __pwCreateChild(child) {
if (Array.isArray(child))
return child.map(grandChild => __pwCreateChild(grandChild));
if (isJsxComponent(child))
return __pwCreateComponent(child);
return child;
}

/**
* @param {JsxComponent} component
* @returns {any[] | undefined}
* @param {any} value
*/
function __pwJsxChildArray(component) {
if (!component.props.children)
return;
if (Array.isArray(component.props.children))
return component.props.children;
return [component.props.children];
}

/**
* @param {JsxComponent} component
*/
function __pwCreateComponent(component) {
const children = __pwJsxChildArray(component)?.map(child => __pwCreateChild(child)).filter(child => {
if (typeof child === 'string')
return !!child.trim();
return true;
function __pwCreateComponent(value) {
return window.__pwTransformObject(value, v => {
if (isJsxComponent(v)) {
const component = v;
const props = component.props ? __pwCreateComponent(component.props) : {};
if (typeof component.type === 'string') {
const { children, ...propsWithoutChildren } = props;
return { result: __pwH(component.type, propsWithoutChildren, children) };
}
return { result: __pwSolidCreateComponent(component.type, props) };
}
});

if (typeof component.type === 'string')
return __pwH(component.type, component.props, children);

return __pwSolidCreateComponent(component.type, { ...component.props, children });
}

const __pwUnmountKey = Symbol('unmountKey');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
type DefaultChildrenProps = {
children?: any;
}
import type { PropsWithChildren } from 'react';

type DefaultChildrenProps = PropsWithChildren<{}>;

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>
return <>{'children' in props ? props.children : 'No Children'}</>
}
2 changes: 1 addition & 1 deletion tests/components/ct-react-vite/tests/children.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ test('render number as child', async ({ mount }) => {
await expect(component).toContainText('1337');
});

test('render without children', async ({ mount }) => {
test('absence of children when children prop is not provided', async ({ mount }) => {
const component = await mount(<CheckChildrenProp />);
await expect(component).toContainText('No Children');
});
17 changes: 4 additions & 13 deletions tests/components/ct-react17/src/components/CheckChildrenProp.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
type DefaultChildrenProps = {
children?: any;
}
import type { PropsWithChildren } from 'react';

type DefaultChildrenProps = PropsWithChildren<{}>;

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>
return <>{'children' in props ? props.children : 'No Children'}</>
}
2 changes: 1 addition & 1 deletion tests/components/ct-react17/tests/children.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ test('render number as child', async ({ mount }) => {
await expect(component).toContainText('1337');
});

test('render without children', async ({ mount }) => {
test('absence of children when children prop is not provided', async ({ mount }) => {
const component = await mount(<CheckChildrenProp />);
await expect(component).toContainText('No Children');
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { type ParentProps } from 'solid-js';

type DefaultChildrenProps = ParentProps<{}>;

export default function CheckChildrenProp(props: DefaultChildrenProps) {
return <>{'children' in props ? props.children : 'No Children'}</>
}
6 changes: 6 additions & 0 deletions tests/components/ct-solid/tests/children.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { test, expect } from '@playwright/experimental-ct-solid';
import Button from '@/components/Button';
import DefaultChildren from '@/components/DefaultChildren';
import MultipleChildren from '@/components/MultipleChildren';
import CheckChildrenProp from '@/components/CheckChildrenProp'

test('render a default child', async ({ mount }) => {
const component = await mount(
Expand Down Expand Up @@ -58,3 +59,8 @@ test('render number as child', async ({ mount }) => {
const component = await mount(<DefaultChildren>{1337}</DefaultChildren>);
await expect(component).toContainText('1337');
});

test('absence of children when children prop is not provided', async ({ mount }) => {
const component = await mount(<CheckChildrenProp />);
await expect(component).toContainText('No Children');
});

0 comments on commit 7e502e9

Please sign in to comment.