2021-12-23 11:51:10 +08:00
|
|
|
/* eslint-disable react/jsx-no-constructed-context-values */
|
|
|
|
import * as React from 'react';
|
2022-09-09 15:51:35 +08:00
|
|
|
import { renderToString } from 'react-dom/server';
|
2016-12-08 11:08:13 +08:00
|
|
|
import glob from 'glob';
|
2021-08-31 15:51:02 +08:00
|
|
|
import { excludeWarning } from './excludeWarning';
|
2022-10-08 16:17:20 +08:00
|
|
|
// import { render } from '../utils';
|
2022-07-25 21:04:43 +08:00
|
|
|
import { TriggerMockContext } from './demoTestContext';
|
2016-11-22 13:43:53 +08:00
|
|
|
|
2022-09-09 15:51:35 +08:00
|
|
|
require('isomorphic-fetch');
|
|
|
|
|
2022-10-08 16:17:20 +08:00
|
|
|
// function normalizeAriaValue(value: string | null): string {
|
|
|
|
// const defaultValue = value || '';
|
|
|
|
|
|
|
|
// return defaultValue
|
|
|
|
// .replace(/\d+/g, 'test')
|
|
|
|
// .replace(/TEST_OR_SSR/g, 'test')
|
|
|
|
// .replace(/-test-test/g, '-test');
|
|
|
|
// }
|
|
|
|
|
|
|
|
// function normalizeAria(element: Element, ariaName: string) {
|
|
|
|
// if (element.hasAttribute(ariaName)) {
|
|
|
|
// element.setAttribute(ariaName, normalizeAriaValue(element.getAttribute(ariaName)));
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// /**
|
|
|
|
// * 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`.
|
|
|
|
// */
|
|
|
|
// function ariaConvert(element: Element) {
|
|
|
|
// normalizeAria(element, 'aria-owns');
|
|
|
|
// normalizeAria(element, 'aria-controls');
|
|
|
|
// normalizeAria(element, 'aria-labelledby');
|
|
|
|
// normalizeAria(element, 'aria-activedescendant');
|
|
|
|
// normalizeAria(element, 'data-menu-id');
|
|
|
|
// normalizeAria(element, 'stroke');
|
|
|
|
// if (element.id) {
|
|
|
|
// element.id = normalizeAriaValue(element.id);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// Array.from(element.children).forEach(child => {
|
|
|
|
// ariaConvert(child);
|
|
|
|
// });
|
|
|
|
// }
|
2018-12-01 12:24:57 +08:00
|
|
|
|
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;
|
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;
|
|
|
|
}
|
2022-09-09 15:51:35 +08:00
|
|
|
Date.now = jest.fn(() => new Date('2016-11-22').getTime());
|
|
|
|
jest.useFakeTimers().setSystemTime(new Date('2016-11-22'));
|
2021-08-31 15:51:02 +08:00
|
|
|
|
2021-12-23 11:51:10 +08:00
|
|
|
testMethod(
|
|
|
|
doInject ? `renders ${file} extend context correctly` : `renders ${file} correctly`,
|
|
|
|
() => {
|
|
|
|
const errSpy = excludeWarning();
|
|
|
|
|
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-10-08 16:17:20 +08:00
|
|
|
// Demo Test also include `dist` test which is already uglified.
|
|
|
|
// We need test this as SSR instead.
|
|
|
|
const html = renderToString(Demo);
|
|
|
|
expect({
|
|
|
|
type: 'demo',
|
|
|
|
html,
|
|
|
|
}).toMatchSnapshot();
|
|
|
|
|
|
|
|
// if (typeof document === 'undefined') {
|
|
|
|
// // Server
|
|
|
|
// expect(() => {
|
|
|
|
// renderToString(Demo);
|
|
|
|
// }).not.toThrow();
|
|
|
|
// } else {
|
|
|
|
// // Client
|
|
|
|
// const { container } = render(Demo);
|
|
|
|
// ariaConvert(container);
|
|
|
|
|
|
|
|
// const { children } = container;
|
|
|
|
// const child = children.length > 1 ? Array.from(children) : children[0];
|
|
|
|
|
|
|
|
// expect(child).toMatchSnapshot();
|
|
|
|
// }
|
2022-07-25 21:04:43 +08:00
|
|
|
|
2021-12-23 11:51:10 +08:00
|
|
|
errSpy();
|
|
|
|
},
|
|
|
|
);
|
2022-09-09 15:51:35 +08:00
|
|
|
jest.useRealTimers();
|
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);
|
|
|
|
}
|