Skip to content

Commit 76f3e5d

Browse files
authored
test: fix test case (#362)
1 parent 646efc7 commit 76f3e5d

File tree

3 files changed

+54
-17
lines changed

3 files changed

+54
-17
lines changed

.husky/pre-commit

Lines changed: 0 additions & 4 deletions
This file was deleted.

tests/index.spec.tsx

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
11
/* eslint-disable react/no-render-return-value, max-classes-per-file, func-names, no-console */
2-
import { render } from '@testing-library/react';
2+
import { fireEvent, render } from '@testing-library/react';
33
import type { ReactWrapper } from 'enzyme';
44
import { mount } from 'enzyme';
5+
import { Provider } from 'rc-motion';
56
import KeyCode from 'rc-util/lib/KeyCode';
67
import React, { cloneElement, useEffect } from 'react';
78
import { act } from 'react-dom/test-utils';
89
import type { DialogProps } from '../src';
910
import Dialog from '../src';
1011

1112
describe('dialog', () => {
13+
async function runFakeTimer() {
14+
for (let i = 0; i < 100; i += 1) {
15+
await act(async () => {
16+
jest.advanceTimersByTime(100);
17+
await Promise.resolve();
18+
});
19+
}
20+
}
21+
1222
beforeEach(() => {
1323
jest.useFakeTimers();
1424
});
1525

1626
afterEach(() => {
27+
jest.clearAllTimers();
1728
jest.useRealTimers();
1829
});
1930

@@ -251,15 +262,17 @@ describe('dialog', () => {
251262
});
252263

253264
it('trap focus after shift-tabbing', () => {
254-
const wrapper = mount(<Dialog visible />, { attachTo: document.body });
255-
wrapper.find('.rc-dialog-wrap').simulate('keyDown', {
265+
render(<Dialog visible />);
266+
267+
document.querySelector<HTMLDivElement>('.rc-dialog > div').focus();
268+
269+
fireEvent.keyDown(document.querySelector('.rc-dialog-wrap'), {
256270
keyCode: KeyCode.TAB,
271+
key: 'Tab',
257272
shiftKey: true,
258273
});
259-
const sentinelEnd = document.querySelectorAll('.rc-dialog-content + div')[0];
274+
const sentinelEnd = document.querySelector('.rc-dialog-content + div');
260275
expect(document.activeElement).toBe(sentinelEnd);
261-
262-
wrapper.unmount();
263276
});
264277
});
265278

@@ -506,15 +519,32 @@ describe('dialog', () => {
506519
});
507520

508521
describe('afterOpenChange', () => {
509-
it('should trigger afterOpenChange when visible changed', () => {
522+
beforeEach(() => {
523+
jest.useFakeTimers();
524+
});
525+
526+
afterEach(() => {
527+
jest.clearAllTimers();
528+
jest.useRealTimers();
529+
});
530+
531+
it('should trigger afterOpenChange when visible changed', async () => {
510532
const afterOpenChange = jest.fn();
511533

512-
const wrapper = mount(<Dialog afterOpenChange={afterOpenChange} visible />);
513-
jest.runAllTimers();
534+
const Demo = (props: any) => (
535+
<Provider motion={false}>
536+
<Dialog afterOpenChange={afterOpenChange} {...props} />
537+
</Provider>
538+
);
514539

515-
wrapper.setProps({ visible: false });
516-
jest.runAllTimers();
540+
const { rerender } = render(<Demo visible />);
541+
await runFakeTimer();
542+
expect(afterOpenChange).toHaveBeenCalledWith(true);
543+
expect(afterOpenChange).toHaveBeenCalledTimes(1);
517544

545+
rerender(<Demo />);
546+
await runFakeTimer();
547+
expect(afterOpenChange).toHaveBeenCalledWith(false);
518548
expect(afterOpenChange).toHaveBeenCalledTimes(2);
519549
});
520550
});

tests/setup.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
/* eslint-disable no-console */
2-
global.requestAnimationFrame = cb => setTimeout(cb, 0);
2+
global.requestAnimationFrame = (cb) => {
3+
return global.setTimeout(cb, 0);
4+
};
5+
global.cancelAnimationFrame = (cb) => {
6+
return global.clearTimeout(cb, 0);
7+
};
8+
window.requestAnimationFrame = (cb) => {
9+
return window.setTimeout(cb, 0);
10+
};
11+
window.cancelAnimationFrame = (cb) => {
12+
return window.clearTimeout(cb, 0);
13+
};
314

415
const originError = console.error;
516
const ignoreList = [
617
'Rendering components directly into document.body',
718
'Warning: unmountComponentAtNode():',
819
];
920
console.error = (...args) => {
10-
if (ignoreList.some(str => args[0].includes(str))) {
21+
if (ignoreList.some((str) => args[0].includes(str))) {
1122
return;
1223
}
1324

0 commit comments

Comments
 (0)