diff --git a/packages/playwright-ct-solid/index.d.ts b/packages/playwright-ct-solid/index.d.ts index 4e52d19c0d..b19433094f 100644 --- a/packages/playwright-ct-solid/index.d.ts +++ b/packages/playwright-ct-solid/index.d.ts @@ -38,7 +38,9 @@ export interface MountOptions { hooksConfig?: any; } -interface MountResult extends Locator {} +interface MountResult extends Locator { + unmount(): Promise; +} export interface ComponentFixtures { mount(component: JSX.Element, options?: MountOptions): Promise; diff --git a/packages/playwright-ct-solid/registerSource.mjs b/packages/playwright-ct-solid/registerSource.mjs index 88a40b363f..1711b6b35f 100644 --- a/packages/playwright-ct-solid/registerSource.mjs +++ b/packages/playwright-ct-solid/registerSource.mjs @@ -57,12 +57,23 @@ function render(component) { return createComponent(componentFunc, component.props); } +const unmountKey = Symbol('disposeKey'); + window.playwrightMount = async (component, rootElement, hooksConfig) => { for (const hook of /** @type {any} */(window).__pw_hooks_before_mount || []) await hook({ hooksConfig }); - solidRender(() => render(component), rootElement); + const unmount = solidRender(() => render(component), rootElement); + rootElement[unmountKey] = unmount; for (const hook of /** @type {any} */(window).__pw_hooks_after_mount || []) await hook({ hooksConfig }); }; + +window.playwrightUnmount = async rootElement => { + const unmount = rootElement[unmountKey]; + if (!unmount) + throw new Error('Component was not mounted'); + + unmount(); +}; diff --git a/tests/components/ct-solid/src/components/MultiRoot.tsx b/tests/components/ct-solid/src/components/MultiRoot.tsx new file mode 100644 index 0000000000..f29e397c0f --- /dev/null +++ b/tests/components/ct-solid/src/components/MultiRoot.tsx @@ -0,0 +1,6 @@ +export default function MultiRoot() { + return <> +
root 1
+
root 2
+ +} diff --git a/tests/components/ct-solid/src/tests.spec.tsx b/tests/components/ct-solid/src/tests.spec.tsx index f8a2769799..a660541477 100644 --- a/tests/components/ct-solid/src/tests.spec.tsx +++ b/tests/components/ct-solid/src/tests.spec.tsx @@ -1,5 +1,6 @@ import { test, expect } from '@playwright/experimental-ct-solid' import Button from './components/Button'; +import MultiRoot from './components/MultiRoot'; test.use({ viewport: { width: 500, height: 500 } }); @@ -15,4 +16,20 @@ test('callback should work', async ({ mount }) => { }}>) await component.click() expect(messages).toEqual(['hello']) -}) +}); + +test('should unmount', async ({ page, mount }) => { + const component = await mount(