mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
fix(ct-react): do not reset mount hooks upon update (#29072)
Fixes https://github.com/microsoft/playwright/issues/29058
This commit is contained in:
parent
f3fac6f4e9
commit
d61f99034a
@ -21,7 +21,7 @@ import __pwReact from 'react';
|
|||||||
import { createRoot as __pwCreateRoot } from 'react-dom/client';
|
import { createRoot as __pwCreateRoot } from 'react-dom/client';
|
||||||
/** @typedef {import('../playwright-ct-core/types/component').JsxComponent} JsxComponent */
|
/** @typedef {import('../playwright-ct-core/types/component').JsxComponent} JsxComponent */
|
||||||
|
|
||||||
/** @type {Map<Element, import('react-dom/client').Root>} */
|
/** @type {Map<Element, { root: import('react-dom/client').Root, setRenderer: (renderer: any) => void }>} */
|
||||||
const __pwRootRegistry = new Map();
|
const __pwRootRegistry = new Map();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -48,33 +48,41 @@ function __pwRender(value) {
|
|||||||
window.playwrightMount = async (component, rootElement, hooksConfig) => {
|
window.playwrightMount = async (component, rootElement, hooksConfig) => {
|
||||||
if (!isJsxComponent(component))
|
if (!isJsxComponent(component))
|
||||||
throw new Error('Object mount notation is not supported');
|
throw new Error('Object mount notation is not supported');
|
||||||
|
|
||||||
let App = () => __pwRender(component);
|
|
||||||
for (const hook of window.__pw_hooks_before_mount || []) {
|
|
||||||
const wrapper = await hook({ App, hooksConfig });
|
|
||||||
if (wrapper)
|
|
||||||
App = () => wrapper;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (__pwRootRegistry.has(rootElement)) {
|
if (__pwRootRegistry.has(rootElement)) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
'Attempting to mount a component into an container that already has a React root'
|
'Attempting to mount a component into an container that already has a React root'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const root = __pwCreateRoot(rootElement);
|
const root = __pwCreateRoot(rootElement);
|
||||||
__pwRootRegistry.set(rootElement, root);
|
const entry = { root, setRenderer: () => undefined };
|
||||||
root.render(App());
|
__pwRootRegistry.set(rootElement, entry);
|
||||||
|
|
||||||
|
const App = () => {
|
||||||
|
/** @type {any} */
|
||||||
|
const [renderer, setRenderer] = __pwReact.useState(() => __pwRender(component));
|
||||||
|
entry.setRenderer = setRenderer;
|
||||||
|
return renderer;
|
||||||
|
};
|
||||||
|
let AppWrapper = App;
|
||||||
|
for (const hook of window.__pw_hooks_before_mount || []) {
|
||||||
|
const wrapper = await hook({ App: AppWrapper, hooksConfig });
|
||||||
|
if (wrapper)
|
||||||
|
AppWrapper = () => wrapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
root.render(__pwReact.createElement(AppWrapper));
|
||||||
|
|
||||||
for (const hook of window.__pw_hooks_after_mount || [])
|
for (const hook of window.__pw_hooks_after_mount || [])
|
||||||
await hook({ hooksConfig });
|
await hook({ hooksConfig });
|
||||||
};
|
};
|
||||||
|
|
||||||
window.playwrightUnmount = async rootElement => {
|
window.playwrightUnmount = async rootElement => {
|
||||||
const root = __pwRootRegistry.get(rootElement);
|
const entry = __pwRootRegistry.get(rootElement);
|
||||||
if (root === undefined)
|
if (!entry)
|
||||||
throw new Error('Component was not mounted');
|
throw new Error('Component was not mounted');
|
||||||
|
|
||||||
root.unmount();
|
entry.root.unmount();
|
||||||
__pwRootRegistry.delete(rootElement);
|
__pwRootRegistry.delete(rootElement);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -82,9 +90,8 @@ window.playwrightUpdate = async (rootElement, component) => {
|
|||||||
if (!isJsxComponent(component))
|
if (!isJsxComponent(component))
|
||||||
throw new Error('Object mount notation is not supported');
|
throw new Error('Object mount notation is not supported');
|
||||||
|
|
||||||
const root = __pwRootRegistry.get(rootElement);
|
const entry = __pwRootRegistry.get(rootElement);
|
||||||
if (root === undefined)
|
if (!entry)
|
||||||
throw new Error('Component was not mounted');
|
throw new Error('Component was not mounted');
|
||||||
|
entry.setRenderer(() => __pwRender(component));
|
||||||
root.render(__pwRender(component));
|
|
||||||
};
|
};
|
||||||
|
@ -21,6 +21,9 @@ import __pwReact from 'react';
|
|||||||
import __pwReactDOM from 'react-dom';
|
import __pwReactDOM from 'react-dom';
|
||||||
/** @typedef {import('../playwright-ct-core/types/component').JsxComponent} JsxComponent */
|
/** @typedef {import('../playwright-ct-core/types/component').JsxComponent} JsxComponent */
|
||||||
|
|
||||||
|
/** @type {Map<Element, { setRenderer: (renderer: any) => void }>} */
|
||||||
|
const __pwRootRegistry = new Map();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {any} component
|
* @param {any} component
|
||||||
* @returns {component is JsxComponent}
|
* @returns {component is JsxComponent}
|
||||||
@ -45,15 +48,29 @@ function __pwRender(value) {
|
|||||||
window.playwrightMount = async (component, rootElement, hooksConfig) => {
|
window.playwrightMount = async (component, rootElement, hooksConfig) => {
|
||||||
if (!isJsxComponent(component))
|
if (!isJsxComponent(component))
|
||||||
throw new Error('Object mount notation is not supported');
|
throw new Error('Object mount notation is not supported');
|
||||||
|
if (__pwRootRegistry.has(rootElement)) {
|
||||||
let App = () => __pwRender(component);
|
throw new Error(
|
||||||
for (const hook of window.__pw_hooks_before_mount || []) {
|
'Attempting to mount a component into an container that already has a React root'
|
||||||
const wrapper = await hook({ App, hooksConfig });
|
);
|
||||||
if (wrapper)
|
|
||||||
App = () => wrapper;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
__pwReactDOM.render(App(), rootElement);
|
const entry = { setRenderer: () => undefined };
|
||||||
|
__pwRootRegistry.set(rootElement, entry);
|
||||||
|
|
||||||
|
const App = () => {
|
||||||
|
/** @type {any} */
|
||||||
|
const [renderer, setRenderer] = __pwReact.useState(() => __pwRender(component));
|
||||||
|
entry.setRenderer = setRenderer;
|
||||||
|
return renderer;
|
||||||
|
};
|
||||||
|
let AppWrapper = App;
|
||||||
|
for (const hook of window.__pw_hooks_before_mount || []) {
|
||||||
|
const wrapper = await hook({ App: AppWrapper, hooksConfig });
|
||||||
|
if (wrapper)
|
||||||
|
AppWrapper = () => wrapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
__pwReactDOM.render(__pwReact.createElement(AppWrapper), rootElement);
|
||||||
|
|
||||||
for (const hook of window.__pw_hooks_after_mount || [])
|
for (const hook of window.__pw_hooks_after_mount || [])
|
||||||
await hook({ hooksConfig });
|
await hook({ hooksConfig });
|
||||||
@ -68,5 +85,8 @@ window.playwrightUpdate = async (rootElement, component) => {
|
|||||||
if (!isJsxComponent(component))
|
if (!isJsxComponent(component))
|
||||||
throw new Error('Object mount notation is not supported');
|
throw new Error('Object mount notation is not supported');
|
||||||
|
|
||||||
__pwReactDOM.render(__pwRender(component), rootElement);
|
const entry = __pwRootRegistry.get(rootElement);
|
||||||
|
if (!entry)
|
||||||
|
throw new Error('Component was not mounted');
|
||||||
|
entry.setRenderer(() => __pwRender(component));
|
||||||
};
|
};
|
||||||
|
@ -3,10 +3,11 @@ import logo from './assets/logo.svg';
|
|||||||
import LoginPage from './pages/LoginPage';
|
import LoginPage from './pages/LoginPage';
|
||||||
import DashboardPage from './pages/DashboardPage';
|
import DashboardPage from './pages/DashboardPage';
|
||||||
|
|
||||||
export default function App() {
|
export default function App({ title }: { title?: string }) {
|
||||||
return <>
|
return <>
|
||||||
<header>
|
<header>
|
||||||
<img src={logo} alt="logo" width={125} height={125} />
|
<img src={logo} alt="logo" width={125} height={125} />
|
||||||
|
{title && <h1>{title}</h1>}
|
||||||
<Link to="/">Login</Link>
|
<Link to="/">Login</Link>
|
||||||
<Link to="/dashboard">Dashboard</Link>
|
<Link to="/dashboard">Dashboard</Link>
|
||||||
</header>
|
</header>
|
||||||
|
@ -12,3 +12,15 @@ test('navigate to a page by clicking a link', async ({ page, mount }) => {
|
|||||||
await expect(component.getByRole('main')).toHaveText('Dashboard');
|
await expect(component.getByRole('main')).toHaveText('Dashboard');
|
||||||
await expect(page).toHaveURL('/dashboard');
|
await expect(page).toHaveURL('/dashboard');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('update should not reset mount hooks', async ({ page, mount }) => {
|
||||||
|
const component = await mount<HooksConfig>(<App title='before'/>, {
|
||||||
|
hooksConfig: { routing: true },
|
||||||
|
});
|
||||||
|
await expect(component.getByRole('heading')).toHaveText('before');
|
||||||
|
await expect(component.getByRole('main')).toHaveText('Login');
|
||||||
|
|
||||||
|
await component.update(<App title='after'/>);
|
||||||
|
await expect(component.getByRole('heading')).toHaveText('after');
|
||||||
|
await expect(component.getByRole('main')).toHaveText('Login');
|
||||||
|
});
|
||||||
|
@ -3,10 +3,11 @@ import logo from './assets/logo.svg';
|
|||||||
import LoginPage from './pages/LoginPage';
|
import LoginPage from './pages/LoginPage';
|
||||||
import DashboardPage from './pages/DashboardPage';
|
import DashboardPage from './pages/DashboardPage';
|
||||||
|
|
||||||
export default function App() {
|
export default function App({ title }: { title?: string }) {
|
||||||
return <>
|
return <>
|
||||||
<header>
|
<header>
|
||||||
<img src={logo} alt="logo" width={125} height={125} />
|
<img src={logo} alt="logo" width={125} height={125} />
|
||||||
|
{title && <h1>{title}</h1>}
|
||||||
<Link to="/">Login</Link>
|
<Link to="/">Login</Link>
|
||||||
<Link to="/dashboard">Dashboard</Link>
|
<Link to="/dashboard">Dashboard</Link>
|
||||||
</header>
|
</header>
|
||||||
|
@ -12,3 +12,15 @@ test('navigate to a page by clicking a link', async ({ page, mount }) => {
|
|||||||
await expect(component.getByRole('main')).toHaveText('Dashboard');
|
await expect(component.getByRole('main')).toHaveText('Dashboard');
|
||||||
await expect(page).toHaveURL('/dashboard');
|
await expect(page).toHaveURL('/dashboard');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('update should not reset mount hooks', async ({ page, mount }) => {
|
||||||
|
const component = await mount<HooksConfig>(<App title='before'/>, {
|
||||||
|
hooksConfig: { routing: true },
|
||||||
|
});
|
||||||
|
await expect(component.getByRole('heading')).toHaveText('before');
|
||||||
|
await expect(component.getByRole('main')).toHaveText('Login');
|
||||||
|
|
||||||
|
await component.update(<App title='after'/>);
|
||||||
|
await expect(component.getByRole('heading')).toHaveText('after');
|
||||||
|
await expect(component.getByRole('main')).toHaveText('Login');
|
||||||
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user