2021-12-23 11:51:10 +08:00
|
|
|
/* eslint-disable react/jsx-no-constructed-context-values */
|
|
|
|
import * as React from 'react';
|
2016-12-08 11:08:13 +08:00
|
|
|
import glob from 'glob';
|
2022-07-25 21:04:43 +08:00
|
|
|
import { render as enzymeRender } from 'enzyme';
|
2016-11-23 12:13:36 +08:00
|
|
|
import MockDate from 'mockdate';
|
2022-02-14 14:40:40 +08:00
|
|
|
import dayjs from 'dayjs';
|
2022-02-18 14:17:32 +08:00
|
|
|
import { StyleProvider, createCache } from '@ant-design/cssinjs';
|
2021-08-31 15:51:02 +08:00
|
|
|
import { excludeWarning } from './excludeWarning';
|
2022-07-25 21:04:43 +08:00
|
|
|
import { render, act } from '../utils';
|
|
|
|
import { TriggerMockContext } from './demoTestContext';
|
2016-11-22 13:43:53 +08:00
|
|
|
|
2022-07-25 21:04:43 +08:00
|
|
|
type CheerIO = ReturnType<typeof enzymeRender>;
|
2020-04-13 13:36:23 +08:00
|
|
|
type CheerIOElement = CheerIO[0];
|
2019-09-12 20:15:17 +08:00
|
|
|
// We should avoid use it in 4.0. Reopen if can not handle this.
|
|
|
|
const USE_REPLACEMENT = false;
|
2018-12-01 12:24:57 +08:00
|
|
|
const testDist = process.env.LIB_DIR === 'dist';
|
|
|
|
|
|
|
|
/**
|
2021-02-02 22:05:31 +08:00
|
|
|
* Rc component will generate id for aria usage. It's created as `test-uuid` when env === 'test'. Or
|
|
|
|
* `f7fa7a3c-a675-47bc-912e-0c45fb6a74d9`(randomly) when not test env. So we need hack of this to
|
|
|
|
* modify the `aria-controls`.
|
2018-12-01 12:24:57 +08:00
|
|
|
*/
|
2020-04-13 13:36:23 +08:00
|
|
|
function ariaConvert(wrapper: CheerIO) {
|
2019-09-12 20:15:17 +08:00
|
|
|
if (!testDist || !USE_REPLACEMENT) return wrapper;
|
2018-12-01 12:24:57 +08:00
|
|
|
|
|
|
|
const matches = new Map();
|
|
|
|
|
2020-04-13 13:36:23 +08:00
|
|
|
function process(entry: CheerIOElement) {
|
2021-02-16 20:40:01 +08:00
|
|
|
if (entry.type === 'text' || entry.type === 'comment') {
|
2020-12-08 17:56:31 +08:00
|
|
|
return;
|
|
|
|
}
|
2018-12-01 12:24:57 +08:00
|
|
|
const { attribs, children } = entry;
|
|
|
|
if (matches.has(entry)) return;
|
|
|
|
matches.set(entry, true);
|
|
|
|
|
|
|
|
// Change aria
|
|
|
|
if (attribs && attribs['aria-controls']) {
|
2019-01-31 16:07:49 +08:00
|
|
|
attribs['aria-controls'] = ''; // Remove all the aria to keep render sync in jest & jest node
|
2018-12-01 12:24:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Loop children
|
2020-12-08 17:56:31 +08:00
|
|
|
if (!children) {
|
|
|
|
return;
|
|
|
|
}
|
2018-12-01 12:24:57 +08:00
|
|
|
(Array.isArray(children) ? children : [children]).forEach(process);
|
|
|
|
}
|
|
|
|
|
2020-04-13 13:36:23 +08:00
|
|
|
wrapper.each((_, entry) => process(entry));
|
2018-12-01 12:24:57 +08:00
|
|
|
|
|
|
|
return wrapper;
|
|
|
|
}
|
|
|
|
|
2020-04-13 13:36:23 +08:00
|
|
|
type Options = {
|
2021-12-23 11:51:10 +08:00
|
|
|
skip?: boolean | string[];
|
2022-07-25 21:04:43 +08:00
|
|
|
testingLib?: 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 = {}) {
|
2016-11-22 13:43:53 +08:00
|
|
|
const files = glob.sync(`./components/${component}/demo/*.md`);
|
|
|
|
|
2018-12-07 16:17:45 +08:00
|
|
|
files.forEach(file => {
|
2016-11-23 16:14:57 +08:00
|
|
|
let testMethod = options.skip === true ? test.skip : test;
|
2016-12-08 11:08:13 +08:00
|
|
|
if (Array.isArray(options.skip) && options.skip.some(c => file.includes(c))) {
|
|
|
|
testMethod = test.skip;
|
|
|
|
}
|
2021-08-31 15:51:02 +08:00
|
|
|
|
2022-04-19 11:27:09 +08:00
|
|
|
if (!doInject) {
|
|
|
|
testMethod(`cssinjs should not warn in ${file}`, () => {
|
|
|
|
const errSpy = jest.spyOn(console, 'error');
|
|
|
|
|
2022-05-06 14:53:40 +08:00
|
|
|
MockDate.set(dayjs('2016-11-22').valueOf());
|
2022-04-19 11:27:09 +08:00
|
|
|
let Demo = require(`../.${file}`).default; // eslint-disable-line global-require, import/no-dynamic-require
|
|
|
|
// Inject Trigger status unless skipped
|
|
|
|
Demo = typeof Demo === 'function' ? <Demo /> : Demo;
|
|
|
|
|
|
|
|
// Inject cssinjs cache to avoid create <style /> element
|
|
|
|
Demo = <StyleProvider cache={createCache()}>{Demo}</StyleProvider>;
|
|
|
|
|
2022-07-29 17:30:06 +08:00
|
|
|
enzymeRender(Demo);
|
2022-04-19 11:27:09 +08:00
|
|
|
|
|
|
|
expect(errSpy).not.toHaveBeenCalledWith(expect.stringContaining('[Ant Design CSS-in-JS]'));
|
|
|
|
MockDate.reset();
|
|
|
|
|
|
|
|
errSpy.mockRestore();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-12-23 11:51:10 +08:00
|
|
|
// function doTest(name: string, openTrigger = false) {
|
|
|
|
testMethod(
|
|
|
|
doInject ? `renders ${file} extend context correctly` : `renders ${file} correctly`,
|
|
|
|
() => {
|
|
|
|
const errSpy = excludeWarning();
|
2022-07-29 15:56:11 +08:00
|
|
|
const mockDate = dayjs('2016-11-22').valueOf();
|
2021-12-23 11:51:10 +08:00
|
|
|
|
2022-07-28 20:30:41 +08:00
|
|
|
MockDate.set(mockDate);
|
2022-04-03 23:27:45 +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 = (
|
2021-12-23 11:51:10 +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-02-18 14:17:32 +08:00
|
|
|
// Inject cssinjs cache to avoid create <style /> element
|
2022-04-12 15:59:36 +08:00
|
|
|
Demo = <StyleProvider cache={createCache()}>{Demo}</StyleProvider>;
|
2022-02-18 14:17:32 +08:00
|
|
|
|
2022-07-25 21:04:43 +08:00
|
|
|
if (options?.testingLib) {
|
2022-07-28 20:30:41 +08:00
|
|
|
jest.useFakeTimers().setSystemTime(mockDate);
|
2018-12-01 12:24:57 +08:00
|
|
|
|
2022-07-25 21:04:43 +08:00
|
|
|
const { container } = render(Demo);
|
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
2018-12-01 12:24:57 +08:00
|
|
|
|
2022-07-25 21:04:43 +08:00
|
|
|
const { children } = container;
|
|
|
|
const child = children.length > 1 ? children : children[0];
|
|
|
|
expect(child).toMatchSnapshot();
|
|
|
|
|
|
|
|
jest.useRealTimers();
|
|
|
|
} else {
|
|
|
|
const wrapper = enzymeRender(Demo);
|
2021-08-31 15:51:02 +08:00
|
|
|
|
2022-07-25 21:04:43 +08:00
|
|
|
// Convert aria related content
|
|
|
|
ariaConvert(wrapper);
|
2021-08-31 15:51:02 +08:00
|
|
|
|
2022-07-25 21:04:43 +08:00
|
|
|
expect(wrapper).toMatchSnapshot();
|
|
|
|
}
|
|
|
|
|
|
|
|
MockDate.reset();
|
2021-12-23 11:51:10 +08:00
|
|
|
errSpy();
|
|
|
|
},
|
|
|
|
);
|
2016-11-22 13:43:53 +08:00
|
|
|
});
|
|
|
|
}
|
2021-12-23 11:51:10 +08:00
|
|
|
|
|
|
|
export function extendTest(component: string, options: Options = {}) {
|
|
|
|
baseText(true, component, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function demoTest(component: string, options: Options = {}) {
|
|
|
|
baseText(false, component, options);
|
|
|
|
}
|