You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
109 lines
3.1 KiB
109 lines
3.1 KiB
3 years ago
|
/* eslint-disable react/jsx-no-constructed-context-values */
|
||
|
import * as React from 'react';
|
||
8 years ago
|
import glob from 'glob';
|
||
8 years ago
|
import { render } from 'enzyme';
|
||
8 years ago
|
import MockDate from 'mockdate';
|
||
7 years ago
|
import moment from 'moment';
|
||
3 years ago
|
import { TriggerProps } from 'rc-trigger';
|
||
3 years ago
|
import { excludeWarning } from './excludeWarning';
|
||
8 years ago
|
|
||
3 years ago
|
export const TriggerMockContext = React.createContext<Partial<TriggerProps> | undefined>(undefined);
|
||
|
|
||
5 years ago
|
type CheerIO = ReturnType<typeof render>;
|
||
|
type CheerIOElement = CheerIO[0];
|
||
5 years ago
|
// We should avoid use it in 4.0. Reopen if can not handle this.
|
||
|
const USE_REPLACEMENT = false;
|
||
6 years ago
|
const testDist = process.env.LIB_DIR === 'dist';
|
||
|
|
||
|
/**
|
||
4 years ago
|
* 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`.
|
||
6 years ago
|
*/
|
||
5 years ago
|
function ariaConvert(wrapper: CheerIO) {
|
||
5 years ago
|
if (!testDist || !USE_REPLACEMENT) return wrapper;
|
||
6 years ago
|
|
||
|
const matches = new Map();
|
||
|
|
||
5 years ago
|
function process(entry: CheerIOElement) {
|
||
4 years ago
|
if (entry.type === 'text' || entry.type === 'comment') {
|
||
4 years ago
|
return;
|
||
|
}
|
||
6 years ago
|
const { attribs, children } = entry;
|
||
|
if (matches.has(entry)) return;
|
||
|
matches.set(entry, true);
|
||
|
|
||
|
// Change aria
|
||
|
if (attribs && attribs['aria-controls']) {
|
||
6 years ago
|
attribs['aria-controls'] = ''; // Remove all the aria to keep render sync in jest & jest node
|
||
6 years ago
|
}
|
||
|
|
||
|
// Loop children
|
||
4 years ago
|
if (!children) {
|
||
|
return;
|
||
|
}
|
||
6 years ago
|
(Array.isArray(children) ? children : [children]).forEach(process);
|
||
|
}
|
||
|
|
||
5 years ago
|
wrapper.each((_, entry) => process(entry));
|
||
6 years ago
|
|
||
|
return wrapper;
|
||
|
}
|
||
|
|
||
5 years ago
|
type Options = {
|
||
3 years ago
|
skip?: boolean | string[];
|
||
5 years ago
|
};
|
||
|
|
||
3 years ago
|
function baseText(doInject: boolean, component: string, options: Options = {}) {
|
||
8 years ago
|
const files = glob.sync(`./components/${component}/demo/*.md`);
|
||
|
|
||
6 years ago
|
files.forEach(file => {
|
||
8 years ago
|
let testMethod = options.skip === true ? test.skip : test;
|
||
8 years ago
|
if (Array.isArray(options.skip) && options.skip.some(c => file.includes(c))) {
|
||
|
testMethod = test.skip;
|
||
|
}
|
||
3 years ago
|
|
||
3 years ago
|
// function doTest(name: string, openTrigger = false) {
|
||
|
testMethod(
|
||
|
doInject ? `renders ${file} extend context correctly` : `renders ${file} correctly`,
|
||
|
() => {
|
||
|
const errSpy = excludeWarning();
|
||
|
|
||
|
MockDate.set(moment('2016-11-22').valueOf());
|
||
|
let demo = require(`../.${file}`).default; // eslint-disable-line global-require, import/no-dynamic-require
|
||
|
|
||
|
// Inject Trigger status unless skipped
|
||
|
if (doInject) {
|
||
|
demo = (
|
||
|
<TriggerMockContext.Provider
|
||
|
value={{
|
||
|
popupVisible: true,
|
||
|
}}
|
||
|
>
|
||
|
{demo}
|
||
|
</TriggerMockContext.Provider>
|
||
|
);
|
||
|
}
|
||
|
|
||
|
const wrapper = render(demo);
|
||
6 years ago
|
|
||
3 years ago
|
// Convert aria related content
|
||
|
ariaConvert(wrapper);
|
||
6 years ago
|
|
||
3 years ago
|
expect(wrapper).toMatchSnapshot();
|
||
|
MockDate.reset();
|
||
3 years ago
|
|
||
3 years ago
|
errSpy();
|
||
|
},
|
||
|
);
|
||
8 years ago
|
});
|
||
|
}
|
||
3 years ago
|
|
||
|
export function extendTest(component: string, options: Options = {}) {
|
||
|
baseText(true, component, options);
|
||
|
}
|
||
|
|
||
|
export default function demoTest(component: string, options: Options = {}) {
|
||
|
baseText(false, component, options);
|
||
|
}
|