ant-design/components/spin/__tests__/delay.test.tsx

49 lines
1.6 KiB
TypeScript
Raw Normal View History

2022-06-22 14:57:09 +08:00
import React from 'react';
// eslint-disable-next-line import/no-named-as-default
import { render } from '@testing-library/react';
import debounce from 'lodash/debounce';
2019-01-05 12:23:20 +08:00
import Spin from '..';
import { sleep } from '../../../tests/utils';
2019-01-05 12:23:20 +08:00
jest.mock('lodash/debounce');
(debounce as jest.Mock).mockImplementation((...args: any[]) =>
jest.requireActual('lodash/debounce')(...args),
);
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", () => {
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 () => {
const { container } = render(<Spin spinning delay={100} />);
2019-01-05 12:23:20 +08:00
expect(container.querySelector('.ant-spin')?.classList.contains('ant-spin-spinning')).toEqual(
false,
);
2019-01-05 12:23:20 +08:00
// use await not jest.runAllTimers()
// because of https://github.com/facebook/jest/issues/3465
await sleep(500);
2019-01-05 12:23:20 +08:00
expect(container.querySelector('.ant-spin')?.classList.contains('ant-spin-spinning')).toEqual(
true,
);
2019-01-05 12:23:20 +08:00
});
it('should cancel debounce function when unmount', async () => {
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();
});
2019-01-05 12:23:20 +08:00
});