2021-12-02 20:25:31 +08:00
|
|
|
import { toHaveNoViolations } from 'jest-axe';
|
2022-04-14 18:09:19 +08:00
|
|
|
import '@testing-library/jest-dom';
|
2022-08-23 23:02:23 +08:00
|
|
|
import format, { plugins } from 'pretty-format';
|
2022-10-11 21:33:29 +08:00
|
|
|
import jsdom from 'jsdom';
|
2022-11-23 20:22:38 +08:00
|
|
|
import { defaultConfig } from '../components/theme/internal';
|
2022-08-19 17:45:03 +08:00
|
|
|
|
|
|
|
// Not use dynamic hashed for test env since version will change hash dynamically.
|
|
|
|
defaultConfig.hashed = false;
|
|
|
|
|
|
|
|
if (process.env.LIB_DIR === 'dist') {
|
|
|
|
jest.mock('../dist/antd', () => {
|
|
|
|
const antd = jest.requireActual('../dist/antd');
|
|
|
|
antd.theme.defaultConfig.hashed = false;
|
|
|
|
|
|
|
|
return antd;
|
|
|
|
});
|
|
|
|
} else if (process.env.LIB_DIR === 'es') {
|
2022-11-23 20:22:38 +08:00
|
|
|
jest.mock('../es/theme/internal', () => {
|
|
|
|
const esTheme = jest.requireActual('../es/theme/internal');
|
2022-08-19 17:45:03 +08:00
|
|
|
if (esTheme.defaultConfig) {
|
|
|
|
esTheme.defaultConfig.hashed = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return esTheme;
|
|
|
|
});
|
|
|
|
}
|
2022-08-22 22:54:38 +08:00
|
|
|
|
2022-09-09 15:51:35 +08:00
|
|
|
function formatHTML(nodes: any) {
|
|
|
|
const htmlContent = format(nodes, {
|
|
|
|
plugins: [plugins.DOMCollection, plugins.DOMElement],
|
|
|
|
});
|
|
|
|
|
|
|
|
const filtered = htmlContent
|
|
|
|
.split(/[\n\r]+/)
|
2022-11-19 13:47:33 +08:00
|
|
|
.filter((line) => line.trim())
|
|
|
|
.map((line) => line.replace(/\s+$/, ''))
|
2022-09-09 15:51:35 +08:00
|
|
|
.join('\n');
|
|
|
|
|
|
|
|
return filtered;
|
|
|
|
}
|
|
|
|
|
2022-08-22 22:54:38 +08:00
|
|
|
/**
|
|
|
|
* React 17 & 18 will have different behavior in some special cases:
|
|
|
|
*
|
|
|
|
* React 17:
|
|
|
|
*
|
|
|
|
* ```html
|
|
|
|
* <span> Hello World </span>
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* React 18:
|
|
|
|
*
|
|
|
|
* ```html
|
|
|
|
* <span> Hello World </span>
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* These diff is nothing important in front end but will break in snapshot diff.
|
|
|
|
*/
|
|
|
|
expect.addSnapshotSerializer({
|
2022-11-19 13:47:33 +08:00
|
|
|
test: (element) =>
|
2022-08-22 22:54:38 +08:00
|
|
|
typeof HTMLElement !== 'undefined' &&
|
|
|
|
(element instanceof HTMLElement ||
|
|
|
|
element instanceof DocumentFragment ||
|
|
|
|
element instanceof HTMLCollection ||
|
|
|
|
(Array.isArray(element) && element[0] instanceof HTMLElement)),
|
2022-11-19 13:47:33 +08:00
|
|
|
print: (element) => formatHTML(element),
|
2022-08-22 22:54:38 +08:00
|
|
|
});
|
2020-04-06 12:05:38 +08:00
|
|
|
|
2022-10-08 16:17:20 +08:00
|
|
|
/** Demo Test only accept render as SSR to make sure align with both `server` & `client` side */
|
|
|
|
expect.addSnapshotSerializer({
|
2022-11-19 13:47:33 +08:00
|
|
|
test: (node) => node && typeof node === 'object' && node.type === 'demo' && node.html,
|
2022-10-08 16:17:20 +08:00
|
|
|
print: ({ html }) => {
|
|
|
|
const { JSDOM } = jsdom;
|
|
|
|
const { document } = new JSDOM().window;
|
|
|
|
document.body.innerHTML = html;
|
|
|
|
|
|
|
|
const children = Array.from(document.body.childNodes);
|
|
|
|
|
|
|
|
// Clean up `data-reactroot` since React 18 do not have this
|
|
|
|
children.forEach((ele: HTMLElement) => {
|
2022-10-11 14:03:17 +08:00
|
|
|
if (typeof ele.removeAttribute === 'function') {
|
2022-10-08 16:17:20 +08:00
|
|
|
ele.removeAttribute('data-reactroot');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return formatHTML(children.length > 1 ? children : children[0]);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-12-02 20:25:31 +08:00
|
|
|
expect.extend(toHaveNoViolations);
|