2021-12-23 11:51:10 +08:00
|
|
|
/* eslint-disable react/jsx-no-constructed-context-values */
|
2023-07-28 16:17:43 +08:00
|
|
|
import path from 'path';
|
2023-11-02 15:10:56 +08:00
|
|
|
import * as React from 'react';
|
2023-01-20 10:31:27 +08:00
|
|
|
import { createCache, StyleProvider } from '@ant-design/cssinjs';
|
2024-02-05 11:45:42 +08:00
|
|
|
import { ConfigProvider } from 'antd';
|
2023-04-12 11:40:09 +08:00
|
|
|
import { globSync } from 'glob';
|
2023-11-02 15:10:56 +08:00
|
|
|
import kebabCase from 'lodash/kebabCase';
|
2022-09-09 15:51:35 +08:00
|
|
|
import { renderToString } from 'react-dom/server';
|
2023-11-02 15:10:56 +08:00
|
|
|
|
|
|
|
import { resetWarned } from '../../components/_util/warning';
|
2023-03-15 00:08:31 +08:00
|
|
|
import { render } from '../utils';
|
2022-07-25 21:04:43 +08:00
|
|
|
import { TriggerMockContext } from './demoTestContext';
|
2023-08-02 14:20:13 +08:00
|
|
|
import { excludeWarning, isSafeWarning } from './excludeWarning';
|
2023-01-20 11:03:50 +08:00
|
|
|
import rootPropsTest from './rootPropsTest';
|
2016-11-22 13:43:53 +08:00
|
|
|
|
2023-06-07 11:54:50 +08:00
|
|
|
export { rootPropsTest };
|
2018-12-01 12:24:57 +08:00
|
|
|
|
2023-06-07 21:59:21 +08:00
|
|
|
require('isomorphic-fetch');
|
|
|
|
|
2022-09-09 15:51:35 +08:00
|
|
|
export type Options = {
|
2021-12-23 11:51:10 +08:00
|
|
|
skip?: boolean | string[];
|
2022-07-25 21:04:43 +08:00
|
|
|
testingLib?: boolean;
|
2023-01-20 11:03:50 +08:00
|
|
|
testRootProps?: false | object;
|
2023-07-17 23:43:32 +08:00
|
|
|
/**
|
|
|
|
* Not check component `displayName`, check path only
|
|
|
|
*/
|
|
|
|
nameCheckPathOnly?: boolean;
|
2020-04-13 13:36:23 +08:00
|
|
|
};
|
|
|
|
|
2021-12-23 11:51:10 +08:00
|
|
|
function baseText(doInject: boolean, component: string, options: Options = {}) {
|
2024-02-05 11:45:42 +08:00
|
|
|
const files = globSync(`./components/${component}/demo/*.tsx`).filter(
|
|
|
|
(file) => !file.includes('_semantic'),
|
|
|
|
);
|
2022-11-19 13:47:33 +08:00
|
|
|
files.forEach((file) => {
|
2023-03-31 19:03:03 +08:00
|
|
|
// to compatible windows path
|
|
|
|
file = file.split(path.sep).join('/');
|
2022-12-05 12:14:05 +08:00
|
|
|
const testMethod =
|
|
|
|
options.skip === true ||
|
|
|
|
(Array.isArray(options.skip) && options.skip.some((c) => file.includes(c)))
|
|
|
|
? test.skip
|
|
|
|
: test;
|
2021-08-31 15:51:02 +08:00
|
|
|
|
2021-12-23 11:51:10 +08:00
|
|
|
// function doTest(name: string, openTrigger = false) {
|
|
|
|
testMethod(
|
|
|
|
doInject ? `renders ${file} extend context correctly` : `renders ${file} correctly`,
|
2023-06-07 21:59:21 +08:00
|
|
|
() => {
|
2023-08-02 14:20:13 +08:00
|
|
|
resetWarned();
|
|
|
|
|
2021-12-23 11:51:10 +08:00
|
|
|
const errSpy = excludeWarning();
|
|
|
|
|
2023-06-07 21:59:21 +08:00
|
|
|
Date.now = jest.fn(() => new Date('2016-11-22').getTime());
|
|
|
|
jest.useFakeTimers().setSystemTime(new Date('2016-11-22'));
|
2022-09-14 14:29:05 +08:00
|
|
|
|
2023-06-07 21:59:21 +08:00
|
|
|
let Demo = require(`../../${file}`).default; // eslint-disable-line global-require, import/no-dynamic-require
|
2021-12-23 11:51:10 +08:00
|
|
|
// Inject Trigger status unless skipped
|
2022-04-03 23:27:45 +08:00
|
|
|
Demo = typeof Demo === 'function' ? <Demo /> : Demo;
|
2021-12-23 11:51:10 +08:00
|
|
|
if (doInject) {
|
2022-04-03 23:27:45 +08:00
|
|
|
Demo = (
|
2023-03-10 11:50:12 +08:00
|
|
|
<TriggerMockContext.Provider value={{ popupVisible: true }}>
|
2022-04-03 23:27:45 +08:00
|
|
|
{Demo}
|
2021-12-23 11:51:10 +08:00
|
|
|
</TriggerMockContext.Provider>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-14 11:34:49 +08:00
|
|
|
// Inject cssinjs cache to avoid create <style /> element
|
2023-11-27 21:37:32 +08:00
|
|
|
Demo = (
|
|
|
|
<ConfigProvider theme={{ hashed: false }}>
|
|
|
|
<StyleProvider cache={createCache()}>{Demo}</StyleProvider>
|
|
|
|
</ConfigProvider>
|
|
|
|
);
|
2022-09-14 11:34:49 +08:00
|
|
|
|
2022-10-08 16:17:20 +08:00
|
|
|
// Demo Test also include `dist` test which is already uglified.
|
|
|
|
// We need test this as SSR instead.
|
2023-03-15 00:08:31 +08:00
|
|
|
if (doInject) {
|
|
|
|
const { container } = render(Demo);
|
|
|
|
expect({ type: 'demo', html: container.innerHTML }).toMatchSnapshot();
|
|
|
|
} else {
|
|
|
|
const html = renderToString(Demo);
|
|
|
|
expect({ type: 'demo', html }).toMatchSnapshot();
|
|
|
|
}
|
2022-10-08 16:17:20 +08:00
|
|
|
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.clearAllTimers();
|
2023-08-02 14:20:13 +08:00
|
|
|
|
|
|
|
// Snapshot of warning info
|
|
|
|
if (doInject) {
|
|
|
|
const errorMessageSet = new Set(errSpy.mock.calls.map((args) => args[0]));
|
|
|
|
const errorMessages = Array.from(errorMessageSet)
|
|
|
|
.filter((msg) => !isSafeWarning(msg, true))
|
|
|
|
.sort();
|
|
|
|
|
|
|
|
expect(errorMessages).toMatchSnapshot();
|
|
|
|
}
|
|
|
|
|
2022-09-14 11:34:49 +08:00
|
|
|
errSpy.mockRestore();
|
2021-12-23 11:51:10 +08:00
|
|
|
},
|
|
|
|
);
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.useRealTimers();
|
2016-11-22 13:43:53 +08:00
|
|
|
});
|
|
|
|
}
|
2021-12-23 11:51:10 +08:00
|
|
|
|
2023-07-17 23:43:32 +08:00
|
|
|
/**
|
|
|
|
* Inject Trigger to force open in test snapshots
|
|
|
|
*/
|
2021-12-23 11:51:10 +08:00
|
|
|
export function extendTest(component: string, options: Options = {}) {
|
|
|
|
baseText(true, component, options);
|
|
|
|
}
|
|
|
|
|
2023-07-17 23:43:32 +08:00
|
|
|
/**
|
|
|
|
* Test all the demo snapshots
|
|
|
|
*/
|
2021-12-23 11:51:10 +08:00
|
|
|
export default function demoTest(component: string, options: Options = {}) {
|
|
|
|
baseText(false, component, options);
|
2023-01-20 11:03:50 +08:00
|
|
|
|
2023-07-17 23:43:32 +08:00
|
|
|
// Test component name is match the kebab-case
|
|
|
|
const testName = test;
|
|
|
|
testName('component name is match the kebab-case', () => {
|
|
|
|
const kebabName = kebabCase(component);
|
|
|
|
|
|
|
|
// Path should exist
|
|
|
|
// eslint-disable-next-line global-require, import/no-dynamic-require
|
|
|
|
const { default: Component } = require(`../../components/${kebabName}`);
|
|
|
|
|
|
|
|
if (options.nameCheckPathOnly !== true) {
|
|
|
|
expect(kebabCase(Component.displayName || '')).toEqual(kebabName);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-01-20 11:03:50 +08:00
|
|
|
if (options?.testRootProps !== false) {
|
|
|
|
rootPropsTest(component, null!, {
|
|
|
|
props: options?.testRootProps,
|
|
|
|
});
|
|
|
|
}
|
2021-12-23 11:51:10 +08:00
|
|
|
}
|