์๋ฌ ๋ฐ์
react-router-dom๋ฅผ ์ฌ์ฉํ๋ ์ฝ๋์ playwright ํ ์คํธ ์ค ์๋์ ๊ฐ์ด useNavigate ๊ด๋ จ ์๋ฌ๊ฐ ๋ฐ์ํ์ต๋๋ค.
๋จ์ ํด๊ฒฐ
useNavigate๋ react-router-dom ์ ํ ์ผ๋ก ์์นญํด๋ณด๋ MemoryRouter ๋ก ๊ฐ์ธ์ฃผ๋ฉด ๋๋ค๊ณ ํฉ๋๋ค.
const component = await mount<HooksConfig>(
<MemoryRouter initialEntries={['/']}>
<Component />
</MemoryRouter>,
{ hooksConfig }
);
์ด๋ ๊ฒ mount ์์ ๋จ์ํ ๊ฐ์ธ๋ ์์ผ๋ก ํด๊ฒฐํ ์๋ ์๊ฒ ์ง๋ง, ์ด ๋ถ๋ถ์ ๊ณตํตํ ํ ์ ์์ต๋๋ค.
๊ณตํตํ ์๋ 1
import { MemoryRouter } from 'react-router-dom';
import { AuthContext } from '@contexts';
export const TEST_CT_USER_ID = 1;
export const testCtRender = (
children: React.ReactElement,
initialPath?: string
): React.ReactElement => {
return (
<MemoryRouter initialEntries={[initialPath ?? '/']}>
<AuthContext.Provider
value={{
user: {
id: TEST_CT_USER_ID,
},
isLoading: false,
isError: false,
logout: () => {},
}}
>
{children}
</AuthContext.Provider>
</MemoryRouter>
);
};
// ์ฌ์ฉ์ฒ
const component = await mount<HooksConfig>(
testCtRender(<Component />, '/some-path'),
{ hooksConfig }
);
์ฐ์ ๋จ์ํ render ํด์ฃผ๋ ํจ์๋ฅผ ๋ณ๋๋ก ๊ตฌ์ฑํ์ฌ ๊ณตํตํํ ์ ์์ต๋๋ค.
testCtRender ํจ์๋ MemoryRouter ๋ก ํ์ฌ path ๋ฅผ ์ธํ ํ๊ณ , ํ์ํ Context ๋ฅผ ์ ๊ณตํด์ฃผ๋ ์ญํ ์ ํฉ๋๋ค.
๊ณตํตํ ์๋ 2
import { beforeMount as beforeMount17 } from '@playwright/experimental-ct-react17/hooks';
import { beforeMount as beforeMount18 } from '@playwright/experimental-ct-react/hooks';
interface HooksConfig {
initialPath?: string;
}
function customBeforeMount(originalSetupApp: SetupAppCallback): void {
beforeMount<HooksConfig>(async ({ App, hooksConfig }) => {
const { initialPath } = hooksConfig;
return (
<MemoryRouter initialEntries={[initialPath ?? '/']}>
<AuthContext.Provider
value={{
user: {
id: TEST_CT_USER_ID,
},
isLoading: false,
isError: false,
logout: () => {},
}}
>
<App />
</AuthContext.Provider>
</MemoryRouter>
);
});
}
// playwright/index.tsx ์์ ์คํ
customBeforeMount();
playwright ์ beforeMount ๊ธฐ๋ฅ์ ์ด์ฉํ๋ฉด ๊ณตํตํจ์๋ฅผ ์ ์ํ์ง ์๊ณ ๋ ๋ชจ๋ ํ ์คํธ ์ฝ๋์ ๋๊ฐ์ ์ฝ๋๋ฅผ ์ ์ฉํ ์ ์์ต๋๋ค.
Comment