Skip to content

Commit

Permalink
test: add algorithm image test (ant-design#39673)
Browse files Browse the repository at this point in the history
* test: add algorithm image test

* chore: add reset.css
  • Loading branch information
MadCcc authored Dec 21, 2022
1 parent a692dc1 commit 2fc3cee
Showing 1 changed file with 43 additions and 37 deletions.
80 changes: 43 additions & 37 deletions tests/shared/imageTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import glob from 'glob';
import { configureToMatchImageSnapshot } from 'jest-image-snapshot';
import MockDate from 'mockdate';
import ReactDOMServer from 'react-dom/server';
import { App } from '../../components';
import { App, theme, ConfigProvider } from '../../components';

const toMatchImageSnapshot = configureToMatchImageSnapshot({
customSnapshotsDir: `${process.cwd()}/imageSnapshots`,
Expand All @@ -18,51 +18,57 @@ expect.extend({ toMatchImageSnapshot });

// eslint-disable-next-line jest/no-export
export default function imageTest(component: React.ReactElement) {
it('component image screenshot should correct', async () => {
await jestPuppeteer.resetPage();
await page.setRequestInterception(true);
const onRequestHandle = (request: any) => {
if (['image'].includes(request.resourceType())) {
request.abort();
} else {
request.continue();
}
};
[theme.defaultAlgorithm, theme.darkAlgorithm, theme.compactAlgorithm].forEach(
(algorithm, index) => {
it(`component image screenshot should correct${index || ''}`, async () => {
await jestPuppeteer.resetPage();
await page.setRequestInterception(true);
const onRequestHandle = (request: any) => {
if (['image'].includes(request.resourceType())) {
request.abort();
} else {
request.continue();
}
};

MockDate.set(dayjs('2016-11-22').valueOf());
page.on('request', onRequestHandle);
await page.goto(`file://${process.cwd()}/tests/index.html`);
await page.addStyleTag({ path: `${process.cwd()}/dist/reset.css` });
MockDate.set(dayjs('2016-11-22').valueOf());
page.on('request', onRequestHandle);
await page.goto(`file://${process.cwd()}/tests/index.html`);
await page.addStyleTag({ path: `${process.cwd()}/dist/reset.css` });

const cache = createCache();
const cache = createCache();

const element = (
<App>
<StyleProvider cache={cache}>{component}</StyleProvider>
</App>
);
const element = (
<ConfigProvider theme={{ algorithm }}>
<App>
<StyleProvider cache={cache}>{component}</StyleProvider>
</App>
</ConfigProvider>
);

const html = ReactDOMServer.renderToString(element);
const styleStr = extractStyle(cache);
const html = ReactDOMServer.renderToString(element);
const styleStr = extractStyle(cache);

await page.evaluate(
(innerHTML, ssrStyle) => {
document.querySelector('#root')!.innerHTML = innerHTML;
await page.evaluate(
(innerHTML, ssrStyle) => {
document.querySelector('#root')!.innerHTML = innerHTML;

const head = document.querySelector('head')!;
head.innerHTML += ssrStyle;
},
html,
styleStr,
);
const head = document.querySelector('head')!;
head.innerHTML += ssrStyle;
},
html,
styleStr,
);

const image = await page.screenshot();
const image = await page.screenshot();

expect(image).toMatchImageSnapshot();
expect(image).toMatchImageSnapshot();

MockDate.reset();
page.removeListener('request', onRequestHandle);
});
MockDate.reset();
page.removeListener('request', onRequestHandle);
});
},
);
}

type Options = {
Expand Down

0 comments on commit 2fc3cee

Please sign in to comment.