2022-06-22 14:57:09 +08:00
|
|
|
import React from 'react';
|
2022-04-20 12:13:11 +08:00
|
|
|
import { render } from '@testing-library/react';
|
2022-12-16 15:02:32 +08:00
|
|
|
import { debounce } from 'throttle-debounce';
|
2019-01-05 12:23:20 +08:00
|
|
|
import Spin from '..';
|
2022-09-27 17:39:02 +08:00
|
|
|
import { waitFakeTimer } from '../../../tests/utils';
|
2019-01-05 12:23:20 +08:00
|
|
|
|
2022-12-16 15:02:32 +08:00
|
|
|
jest.mock('throttle-debounce');
|
2022-07-01 11:53:49 +08:00
|
|
|
(debounce as jest.Mock).mockImplementation((...args: any[]) =>
|
2022-12-16 15:02:32 +08:00
|
|
|
jest.requireActual('throttle-debounce').debounce(...args),
|
2022-07-01 11:53:49 +08:00
|
|
|
);
|
2022-04-20 12:13:11 +08:00
|
|
|
|
2019-01-05 12:23:20 +08:00
|
|
|
describe('delay spinning', () => {
|
2019-01-13 21:18:38 +08:00
|
|
|
it("should render with delay when it's mounted with spinning=true and delay", () => {
|
2022-07-01 11:53:49 +08:00
|
|
|
const { container } = render(<Spin spinning delay={500} />);
|
|
|
|
expect(container.querySelector('.ant-spin')?.classList.contains('ant-spin-spinning')).toEqual(
|
|
|
|
false,
|
|
|
|
);
|
2019-01-05 12:23:20 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should render when delay is init set', async () => {
|
2022-09-27 17:39:02 +08:00
|
|
|
jest.useFakeTimers();
|
2022-07-01 11:53:49 +08:00
|
|
|
const { container } = render(<Spin spinning delay={100} />);
|
2019-01-05 12:23:20 +08:00
|
|
|
|
2022-09-27 17:39:02 +08:00
|
|
|
expect(container.querySelector('.ant-spin-spinning')).toBeFalsy();
|
2019-01-05 12:23:20 +08:00
|
|
|
|
2022-09-27 17:39:02 +08:00
|
|
|
await waitFakeTimer();
|
2019-01-05 12:23:20 +08:00
|
|
|
|
2022-09-27 17:39:02 +08:00
|
|
|
expect(container.querySelector('.ant-spin-spinning')).toBeTruthy();
|
|
|
|
|
|
|
|
jest.clearAllTimers();
|
|
|
|
jest.useRealTimers();
|
2019-01-05 12:23:20 +08:00
|
|
|
});
|
2019-01-07 14:21:57 +08:00
|
|
|
|
2022-10-14 22:07:02 +08:00
|
|
|
it('should cancel debounce function when unmount', () => {
|
2022-04-20 12:13:11 +08:00
|
|
|
const debouncedFn = jest.fn();
|
|
|
|
const cancel = jest.fn();
|
2022-07-01 11:53:49 +08:00
|
|
|
(debouncedFn as any).cancel = cancel;
|
|
|
|
(debounce as jest.Mock).mockReturnValueOnce(debouncedFn);
|
2022-04-20 12:13:11 +08:00
|
|
|
const { unmount } = render(<Spin spinning delay={100} />);
|
2022-07-01 11:53:49 +08:00
|
|
|
|
2022-04-20 12:13:11 +08:00
|
|
|
expect(cancel).not.toHaveBeenCalled();
|
|
|
|
unmount();
|
|
|
|
expect(cancel).toHaveBeenCalled();
|
2019-01-07 14:21:57 +08:00
|
|
|
});
|
2023-02-07 23:46:10 +08:00
|
|
|
|
|
|
|
it('should close immediately', async () => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
const { container, rerender } = render(<Spin spinning delay={500} />);
|
|
|
|
|
|
|
|
await waitFakeTimer();
|
|
|
|
expect(container.querySelector('.ant-spin-spinning')).toBeTruthy();
|
|
|
|
|
|
|
|
rerender(<Spin spinning={false} delay={500} />);
|
|
|
|
expect(container.querySelector('.ant-spin-spinning')).toBeFalsy();
|
|
|
|
});
|
2019-01-05 12:23:20 +08:00
|
|
|
});
|