2016-12-08 11:08:13 +08:00
|
|
|
import glob from 'glob';
|
2016-11-22 13:43:53 +08:00
|
|
|
import { render } from 'enzyme';
|
2016-11-23 12:13:36 +08:00
|
|
|
import MockDate from 'mockdate';
|
2018-03-21 12:27:41 +08:00
|
|
|
import moment from 'moment';
|
2016-11-22 13:43:53 +08:00
|
|
|
|
2020-04-13 13:36:23 +08:00
|
|
|
type CheerIO = ReturnType<typeof render>;
|
|
|
|
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';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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`.
|
|
|
|
*/
|
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) {
|
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
|
|
|
|
if (!children) return;
|
|
|
|
(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 = {
|
|
|
|
skip?: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default function demoTest(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;
|
|
|
|
}
|
2016-11-22 13:43:53 +08:00
|
|
|
testMethod(`renders ${file} correctly`, () => {
|
2020-05-21 23:27:02 +08:00
|
|
|
MockDate.set(moment('2016-11-22').valueOf());
|
2018-01-13 19:11:53 +08:00
|
|
|
const demo = require(`../.${file}`).default; // eslint-disable-line global-require, import/no-dynamic-require
|
2016-11-22 13:43:53 +08:00
|
|
|
const wrapper = render(demo);
|
2018-12-01 12:24:57 +08:00
|
|
|
|
|
|
|
// Convert aria related content
|
|
|
|
ariaConvert(wrapper);
|
|
|
|
|
2017-04-02 18:12:05 +08:00
|
|
|
expect(wrapper).toMatchSnapshot();
|
2016-11-23 12:13:36 +08:00
|
|
|
MockDate.reset();
|
2016-11-22 13:43:53 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|