amis2/__tests__/helper.tsx
吴多益 ed69ac05aa
chore: 补充部分组件的单测,行覆盖率到 56% (#3567)
* 补充部分单测

* 补充部分单元测试

* 补充部分组件的单元测试
2022-02-11 19:36:45 +08:00

41 lines
1.1 KiB
TypeScript

import {RenderOptions} from '../src/factory';
// jest.useFakeTimers 会修改 global 的 setTimeout 所以需要把原始的记录下来。
const timerFn = setTimeout;
export function wait(duration: number, fn?: Function) {
return new Promise<void>(resolve => {
timerFn(() => {
fn && fn();
resolve();
}, duration);
});
}
export function makeEnv(env?: Partial<RenderOptions>): RenderOptions {
return {
session: 'test-case',
isCancel: () => false,
notify: (msg: string) => null,
jumpTo: (to: string) => console.info('Now should jump to ' + to),
alert: msg => console.info(`Alert: ${msg}`),
...env
};
}
export const createMockMediaMatcher =
(matchesOrMapOfMatches: any) => (qs: any) => ({
matches:
typeof matchesOrMapOfMatches === 'object'
? matchesOrMapOfMatches[qs]
: matchesOrMapOfMatches,
media: '',
addListener: () => {},
addEventListener: () => {},
removeEventListener: () => {},
onchange: () => {},
removeListener: () => {},
dispatchEvent: () => {
return true;
}
});