-
-
Notifications
You must be signed in to change notification settings - Fork 46.7k
/
delay.test.tsx
45 lines (36 loc) · 1.42 KB
/
delay.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import React from 'react';
import { render } from '@testing-library/react';
import { debounce } from 'throttle-debounce';
import Spin from '..';
import { waitFakeTimer } from '../../../tests/utils';
jest.mock('throttle-debounce');
(debounce as jest.Mock).mockImplementation((...args: any[]) =>
jest.requireActual('throttle-debounce').debounce(...args),
);
describe('delay spinning', () => {
it("should render with delay when it's mounted with spinning=true and delay", () => {
const { container } = render(<Spin spinning delay={500} />);
expect(container.querySelector('.ant-spin')?.classList.contains('ant-spin-spinning')).toEqual(
false,
);
});
it('should render when delay is init set', async () => {
jest.useFakeTimers();
const { container } = render(<Spin spinning delay={100} />);
expect(container.querySelector('.ant-spin-spinning')).toBeFalsy();
await waitFakeTimer();
expect(container.querySelector('.ant-spin-spinning')).toBeTruthy();
jest.clearAllTimers();
jest.useRealTimers();
});
it('should cancel debounce function when unmount', () => {
const debouncedFn = jest.fn();
const cancel = jest.fn();
(debouncedFn as any).cancel = cancel;
(debounce as jest.Mock).mockReturnValueOnce(debouncedFn);
const { unmount } = render(<Spin spinning delay={100} />);
expect(cancel).not.toHaveBeenCalled();
unmount();
expect(cancel).toHaveBeenCalled();
});
});