2020-05-20 19:47:49 +08:00
|
|
|
import React from 'react';
|
|
|
|
// Reference: https://github.com/ant-design/ant-design/pull/24003#discussion_r427267386
|
|
|
|
// eslint-disable-next-line import/no-unresolved
|
2022-06-28 19:28:26 +08:00
|
|
|
import { createCache, extractStyle, StyleProvider } from '@ant-design/cssinjs';
|
|
|
|
import dayjs from 'dayjs';
|
2020-10-23 18:50:46 +08:00
|
|
|
import glob from 'glob';
|
2022-06-28 19:28:26 +08:00
|
|
|
import { configureToMatchImageSnapshot } from 'jest-image-snapshot';
|
2020-10-28 16:16:31 +08:00
|
|
|
import MockDate from 'mockdate';
|
2022-06-28 19:28:26 +08:00
|
|
|
import ReactDOMServer from 'react-dom/server';
|
2022-12-21 10:38:34 +08:00
|
|
|
import { App, theme, ConfigProvider } from '../../components';
|
2020-10-23 18:50:46 +08:00
|
|
|
|
|
|
|
const toMatchImageSnapshot = configureToMatchImageSnapshot({
|
|
|
|
customSnapshotsDir: `${process.cwd()}/imageSnapshots`,
|
|
|
|
customDiffDir: `${process.cwd()}/imageDiffSnapshots`,
|
|
|
|
});
|
2020-05-20 19:47:49 +08:00
|
|
|
|
|
|
|
expect.extend({ toMatchImageSnapshot });
|
|
|
|
|
|
|
|
// eslint-disable-next-line jest/no-export
|
|
|
|
export default function imageTest(component: React.ReactElement) {
|
2022-12-21 10:38:34 +08:00
|
|
|
[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();
|
|
|
|
}
|
|
|
|
};
|
2020-11-15 22:14:17 +08:00
|
|
|
|
2022-12-21 10:38:34 +08:00
|
|
|
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` });
|
2022-06-28 19:28:26 +08:00
|
|
|
|
2022-12-21 10:38:34 +08:00
|
|
|
const cache = createCache();
|
2022-12-07 17:13:39 +08:00
|
|
|
|
2022-12-21 10:38:34 +08:00
|
|
|
const element = (
|
|
|
|
<ConfigProvider theme={{ algorithm }}>
|
|
|
|
<App>
|
|
|
|
<StyleProvider cache={cache}>{component}</StyleProvider>
|
|
|
|
</App>
|
|
|
|
</ConfigProvider>
|
|
|
|
);
|
2022-12-07 17:13:39 +08:00
|
|
|
|
2022-12-21 10:38:34 +08:00
|
|
|
const html = ReactDOMServer.renderToString(element);
|
|
|
|
const styleStr = extractStyle(cache);
|
2022-06-28 19:28:26 +08:00
|
|
|
|
2022-12-21 10:38:34 +08:00
|
|
|
await page.evaluate(
|
|
|
|
(innerHTML, ssrStyle) => {
|
|
|
|
document.querySelector('#root')!.innerHTML = innerHTML;
|
2022-06-28 19:28:26 +08:00
|
|
|
|
2022-12-21 10:38:34 +08:00
|
|
|
const head = document.querySelector('head')!;
|
|
|
|
head.innerHTML += ssrStyle;
|
|
|
|
},
|
|
|
|
html,
|
|
|
|
styleStr,
|
|
|
|
);
|
2020-05-20 19:47:49 +08:00
|
|
|
|
2022-12-21 10:38:34 +08:00
|
|
|
const image = await page.screenshot();
|
2020-05-20 19:47:49 +08:00
|
|
|
|
2022-12-21 10:38:34 +08:00
|
|
|
expect(image).toMatchImageSnapshot();
|
2020-11-13 19:35:46 +08:00
|
|
|
|
2022-12-21 10:38:34 +08:00
|
|
|
MockDate.reset();
|
|
|
|
page.removeListener('request', onRequestHandle);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
2020-05-20 19:47:49 +08:00
|
|
|
}
|
2020-10-23 18:50:46 +08:00
|
|
|
|
|
|
|
type Options = {
|
|
|
|
skip?: boolean | string[];
|
|
|
|
};
|
|
|
|
|
|
|
|
// eslint-disable-next-line jest/no-export
|
|
|
|
export function imageDemoTest(component: string, options: Options = {}) {
|
2022-12-05 12:14:05 +08:00
|
|
|
let describeMethod = options.skip === true ? describe.skip : describe;
|
2022-11-09 12:28:04 +08:00
|
|
|
const files = glob.sync(`./components/${component}/demo/*.tsx`);
|
2020-10-23 18:50:46 +08:00
|
|
|
|
2022-11-19 13:47:33 +08:00
|
|
|
files.forEach((file) => {
|
|
|
|
if (Array.isArray(options.skip) && options.skip.some((c) => file.includes(c))) {
|
2022-12-05 12:14:05 +08:00
|
|
|
describeMethod = describe.skip;
|
|
|
|
} else {
|
|
|
|
describeMethod = describe;
|
2020-10-23 18:50:46 +08:00
|
|
|
}
|
2022-12-05 12:14:05 +08:00
|
|
|
describeMethod(`Test ${file} image`, () => {
|
2020-10-23 18:50:46 +08:00
|
|
|
// eslint-disable-next-line global-require,import/no-dynamic-require
|
2022-04-03 23:27:45 +08:00
|
|
|
let Demo = require(`../.${file}`).default;
|
|
|
|
if (typeof Demo === 'function') {
|
|
|
|
Demo = <Demo />;
|
|
|
|
}
|
|
|
|
imageTest(Demo);
|
2020-10-23 18:50:46 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|