feat(ct): solid render children complete (#17417)
This commit is contained in:
parent
9901ae0c21
commit
9564306297
|
|
@ -17,7 +17,8 @@
|
||||||
// @ts-check
|
// @ts-check
|
||||||
// This file is injected into the registry as text, no dependencies are allowed.
|
// This file is injected into the registry as text, no dependencies are allowed.
|
||||||
|
|
||||||
import { render as solidRender, createComponent } from 'solid-js/web';
|
import { render as solidRender, createComponent as solidCreateComponent } from 'solid-js/web';
|
||||||
|
import h from 'solid-js/h';
|
||||||
|
|
||||||
/** @typedef {import('../playwright-test/types/component').Component} Component */
|
/** @typedef {import('../playwright-test/types/component').Component} Component */
|
||||||
/** @typedef {() => import('solid-js').JSX.Element} FrameworkComponent */
|
/** @typedef {() => import('solid-js').JSX.Element} FrameworkComponent */
|
||||||
|
|
@ -33,40 +34,54 @@ export function register(components) {
|
||||||
registry.set(name, value);
|
registry.set(name, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function createChild(child) {
|
||||||
|
return typeof child === 'string' ? child : createComponent(child);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Component} component
|
* @param {Component} component
|
||||||
*/
|
*/
|
||||||
function render(component) {
|
function createComponent(component) {
|
||||||
let componentFunc = registry.get(component.type);
|
if (typeof component === 'string')
|
||||||
if (!componentFunc) {
|
return component;
|
||||||
|
|
||||||
|
let Component = registry.get(component.type);
|
||||||
|
if (!Component) {
|
||||||
// Lookup by shorthand.
|
// Lookup by shorthand.
|
||||||
for (const [name, value] of registry) {
|
for (const [name, value] of registry) {
|
||||||
if (component.type.endsWith(`_${name}`)) {
|
if (component.type.endsWith(`_${name}`)) {
|
||||||
componentFunc = value;
|
Component = value;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!componentFunc)
|
if (!Component && component.type[0].toUpperCase() === component.type[0])
|
||||||
throw new Error(`Unregistered component: ${component.type}. Following components are registered: ${[...registry.keys()]}`);
|
throw new Error(`Unregistered component: ${component.type}. Following components are registered: ${[...registry.keys()]}`);
|
||||||
|
|
||||||
if (component.kind !== 'jsx')
|
if (component.kind !== 'jsx')
|
||||||
throw new Error('Object mount notation is not supported');
|
throw new Error('Object mount notation is not supported');
|
||||||
|
|
||||||
return createComponent(componentFunc, {
|
const children = component.children.reduce((/** @type {any[]} */ children, current) => {
|
||||||
children: component.children,
|
const child = createChild(current);
|
||||||
...component.props
|
if (typeof child !== 'string' || !!child.trim())
|
||||||
});
|
children.push(child);
|
||||||
|
return children;
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
if (!Component)
|
||||||
|
return h(component.type, component.props, children);
|
||||||
|
|
||||||
|
return solidCreateComponent(Component, { ...component.props, children });
|
||||||
}
|
}
|
||||||
|
|
||||||
const unmountKey = Symbol('disposeKey');
|
const unmountKey = Symbol('unmountKey');
|
||||||
|
|
||||||
window.playwrightMount = async (component, rootElement, hooksConfig) => {
|
window.playwrightMount = async (component, rootElement, hooksConfig) => {
|
||||||
for (const hook of /** @type {any} */(window).__pw_hooks_before_mount || [])
|
for (const hook of /** @type {any} */(window).__pw_hooks_before_mount || [])
|
||||||
await hook({ hooksConfig });
|
await hook({ hooksConfig });
|
||||||
|
|
||||||
const unmount = solidRender(() => render(component), rootElement);
|
const unmount = solidRender(() => createComponent(component), rootElement);
|
||||||
rootElement[unmountKey] = unmount;
|
rootElement[unmountKey] = unmount;
|
||||||
|
|
||||||
for (const hook of /** @type {any} */(window).__pw_hooks_after_mount || [])
|
for (const hook of /** @type {any} */(window).__pw_hooks_after_mount || [])
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,17 @@
|
||||||
|
type MultipleChildrenProps = {
|
||||||
|
children?: [any, any, any];
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function MultipleChildren(props: MultipleChildrenProps) {
|
||||||
|
return <div>
|
||||||
|
<header>
|
||||||
|
{props.children?.at(0)}
|
||||||
|
</header>
|
||||||
|
<main>
|
||||||
|
{props.children?.at(1)}
|
||||||
|
</main>
|
||||||
|
<footer>
|
||||||
|
{props.children?.at(2)}
|
||||||
|
</footer>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
import { test, expect } from '@playwright/experimental-ct-solid'
|
import { test, expect } from '@playwright/experimental-ct-solid';
|
||||||
import Button from './components/Button';
|
import Button from './components/Button';
|
||||||
import DefaultChildren from './components/DefaultChildren';
|
import DefaultChildren from './components/DefaultChildren';
|
||||||
|
import MultipleChildren from './components/MultipleChildren';
|
||||||
import MultiRoot from './components/MultiRoot';
|
import MultiRoot from './components/MultiRoot';
|
||||||
import EmptyFragment from './components/EmptyFragment';
|
import EmptyFragment from './components/EmptyFragment';
|
||||||
|
|
||||||
|
|
@ -12,46 +13,86 @@ test('render props', async ({ mount }) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('execute callback when the button is clicked', async ({ mount }) => {
|
test('execute callback when the button is clicked', async ({ mount }) => {
|
||||||
const messages: string[] = []
|
const messages: string[] = [];
|
||||||
const component = await mount(<Button title="Submit" onClick={data => {
|
const component = await mount(
|
||||||
messages.push(data)
|
<Button
|
||||||
}}></Button>)
|
title="Submit"
|
||||||
await component.click()
|
onClick={(data) => {
|
||||||
expect(messages).toEqual(['hello'])
|
messages.push(data);
|
||||||
})
|
}}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
await component.click();
|
||||||
|
expect(messages).toEqual(['hello']);
|
||||||
|
});
|
||||||
|
|
||||||
test('default child should work', async ({ mount }) => {
|
test('render a default child', async ({ mount }) => {
|
||||||
const component = await mount(<DefaultChildren>
|
const component = await mount(
|
||||||
Main Content
|
<DefaultChildren>Main Content</DefaultChildren>
|
||||||
</DefaultChildren>)
|
);
|
||||||
await expect(component).toContainText('Main Content')
|
await expect(component).toContainText('Main Content');
|
||||||
})
|
});
|
||||||
|
|
||||||
|
test('render multiple children', async ({ mount }) => {
|
||||||
|
const component = await mount(
|
||||||
|
<DefaultChildren>
|
||||||
|
<div id="one">One</div>
|
||||||
|
<div id="two">Two</div>
|
||||||
|
</DefaultChildren>
|
||||||
|
);
|
||||||
|
await expect(component.locator('#one')).toContainText('One');
|
||||||
|
await expect(component.locator('#two')).toContainText('Two');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('render named children', async ({ mount }) => {
|
||||||
|
const component = await mount(
|
||||||
|
<MultipleChildren>
|
||||||
|
<div>Header</div>
|
||||||
|
<div>Main Content</div>
|
||||||
|
<div>Footer</div>
|
||||||
|
</MultipleChildren>
|
||||||
|
);
|
||||||
|
await expect(component).toContainText('Header');
|
||||||
|
await expect(component).toContainText('Main Content');
|
||||||
|
await expect(component).toContainText('Footer');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('execute callback when a child node is clicked', async ({ mount }) => {
|
||||||
|
let clickFired = false;
|
||||||
|
const component = await mount(
|
||||||
|
<DefaultChildren>
|
||||||
|
<span onClick={() => (clickFired = true)}>Main Content</span>
|
||||||
|
</DefaultChildren>
|
||||||
|
);
|
||||||
|
await component.locator('text=Main Content').click();
|
||||||
|
expect(clickFired).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
test('run hooks', async ({ page, mount }) => {
|
test('run hooks', async ({ page, mount }) => {
|
||||||
const messages: string[] = [];
|
const messages: string[] = [];
|
||||||
page.on('console', m => messages.push(m.text()));
|
page.on('console', (m) => messages.push(m.text()));
|
||||||
await mount(<Button title="Submit" />, {
|
await mount(<Button title="Submit" />, {
|
||||||
hooksConfig: {
|
hooksConfig: {
|
||||||
route: 'A'
|
route: 'A',
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
expect(messages).toEqual(['Before mount: {\"route\":\"A\"}', 'After mount']);
|
expect(messages).toEqual(['Before mount: {"route":"A"}', 'After mount']);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('unmount', async ({ page, mount }) => {
|
test('unmount', async ({ page, mount }) => {
|
||||||
const component = await mount(<Button title="Submit" />)
|
const component = await mount(<Button title="Submit" />);
|
||||||
await expect(page.locator('#root')).toContainText('Submit')
|
await expect(page.locator('#root')).toContainText('Submit');
|
||||||
await component.unmount();
|
await component.unmount();
|
||||||
await expect(page.locator('#root')).not.toContainText('Submit');
|
await expect(page.locator('#root')).not.toContainText('Submit');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('unmount a multi root component', async ({ mount, page }) => {
|
test('unmount a multi root component', async ({ mount, page }) => {
|
||||||
const component = await mount(<MultiRoot />)
|
const component = await mount(<MultiRoot />);
|
||||||
await expect(page.locator('#root')).toContainText('root 1')
|
await expect(page.locator('#root')).toContainText('root 1');
|
||||||
await expect(page.locator('#root')).toContainText('root 2')
|
await expect(page.locator('#root')).toContainText('root 2');
|
||||||
await component.unmount()
|
await component.unmount();
|
||||||
await expect(page.locator('#root')).not.toContainText('root 1')
|
await expect(page.locator('#root')).not.toContainText('root 1');
|
||||||
await expect(page.locator('#root')).not.toContainText('root 2')
|
await expect(page.locator('#root')).not.toContainText('root 2');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('get textContent of the empty fragment', async ({ mount }) => {
|
test('get textContent of the empty fragment', async ({ mount }) => {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue