2023-05-12 09:51:44 +08:00
|
|
|
import React from 'react';
|
2023-10-19 13:47:48 +08:00
|
|
|
import { render } from '@testing-library/react';
|
|
|
|
|
2017-01-27 14:27:18 +08:00
|
|
|
import Spin from '..';
|
2019-08-26 22:53:20 +08:00
|
|
|
import mountTest from '../../../tests/shared/mountTest';
|
2020-01-02 19:10:16 +08:00
|
|
|
import rtlTest from '../../../tests/shared/rtlTest';
|
2023-05-12 09:51:44 +08:00
|
|
|
import { waitFakeTimer } from '../../../tests/utils';
|
2017-01-27 14:27:18 +08:00
|
|
|
|
|
|
|
describe('Spin', () => {
|
2019-08-26 22:53:20 +08:00
|
|
|
mountTest(Spin);
|
2020-01-02 19:10:16 +08:00
|
|
|
rtlTest(Spin);
|
2019-08-26 22:53:20 +08:00
|
|
|
|
2017-01-27 14:27:18 +08:00
|
|
|
it('should only affect the spin element when set style to a nested <Spin>xx</Spin>', () => {
|
2022-07-01 11:53:49 +08:00
|
|
|
const { container } = render(
|
2017-01-27 14:27:18 +08:00
|
|
|
<Spin style={{ background: 'red' }}>
|
|
|
|
<div>content</div>
|
2018-12-07 20:02:01 +08:00
|
|
|
</Spin>,
|
2017-01-27 14:27:18 +08:00
|
|
|
);
|
2022-10-14 22:07:02 +08:00
|
|
|
expect(container.querySelector<HTMLElement>('.ant-spin-nested-loading')?.style.length).toBe(0);
|
|
|
|
expect(container.querySelector<HTMLElement>('.ant-spin')?.style.background).toBe('red');
|
2017-01-27 14:27:18 +08:00
|
|
|
});
|
2017-11-04 12:05:25 +08:00
|
|
|
|
2023-10-19 13:47:48 +08:00
|
|
|
it('should not apply nested styles when full screen', () => {
|
|
|
|
const { container } = render(
|
|
|
|
<Spin fullscreen>
|
|
|
|
<div>content</div>
|
|
|
|
</Spin>,
|
|
|
|
);
|
|
|
|
expect(container.querySelector<HTMLElement>('ant-spin-nested-loading')).toBeNull();
|
|
|
|
});
|
|
|
|
|
2018-12-07 20:02:01 +08:00
|
|
|
it("should render custom indicator when it's set", () => {
|
2017-11-04 12:05:25 +08:00
|
|
|
const customIndicator = <div className="custom-indicator" />;
|
2022-07-01 11:53:49 +08:00
|
|
|
const { asFragment } = render(<Spin indicator={customIndicator} />);
|
|
|
|
expect(asFragment().firstChild).toMatchSnapshot();
|
2017-11-04 12:05:25 +08:00
|
|
|
});
|
2018-06-08 23:13:47 +08:00
|
|
|
|
2022-10-14 22:07:02 +08:00
|
|
|
it('should be controlled by spinning', async () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.useFakeTimers();
|
2022-04-18 21:02:11 +08:00
|
|
|
const { container, rerender } = render(<Spin spinning={false} />);
|
2022-04-20 12:13:11 +08:00
|
|
|
expect(container.querySelector('.ant-spin-spinning')).toBeFalsy();
|
2022-04-18 21:02:11 +08:00
|
|
|
rerender(<Spin spinning />);
|
2022-10-14 22:07:02 +08:00
|
|
|
await waitFakeTimer();
|
2022-04-20 12:13:11 +08:00
|
|
|
expect(container.querySelector('.ant-spin-spinning')).toBeTruthy();
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.clearAllTimers();
|
|
|
|
jest.useRealTimers();
|
2018-08-10 17:04:40 +08:00
|
|
|
});
|
2019-11-27 09:40:47 +08:00
|
|
|
|
|
|
|
it('if indicator set null should not be render default indicator', () => {
|
2022-07-01 11:53:49 +08:00
|
|
|
const { asFragment } = render(<Spin indicator={null as any} />);
|
|
|
|
expect(asFragment().firstChild).toMatchSnapshot();
|
2019-11-27 09:40:47 +08:00
|
|
|
});
|
2020-02-14 19:02:53 +08:00
|
|
|
|
|
|
|
it('should support static method Spin.setDefaultIndicator', () => {
|
|
|
|
Spin.setDefaultIndicator(<em className="custom-spinner" />);
|
2022-07-01 11:53:49 +08:00
|
|
|
const { asFragment } = render(<Spin />);
|
|
|
|
expect(asFragment().firstChild).toMatchSnapshot();
|
2020-02-14 19:02:53 +08:00
|
|
|
Spin.setDefaultIndicator(null);
|
2020-02-19 13:20:43 +08:00
|
|
|
});
|
2020-11-18 11:59:29 +08:00
|
|
|
|
|
|
|
it('should render 0', () => {
|
2022-07-01 11:53:49 +08:00
|
|
|
const { container } = render(<Spin>{0}</Spin>);
|
|
|
|
expect(container.querySelector('.ant-spin-container')?.textContent).toBe('0');
|
2020-11-18 11:59:29 +08:00
|
|
|
});
|
2023-05-12 09:51:44 +08:00
|
|
|
|
|
|
|
it('warning tip without nest', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
2023-05-12 09:51:44 +08:00
|
|
|
|
|
|
|
const { container } = render(<Spin tip="Not Show" />);
|
|
|
|
expect(container.querySelector('.ant-spin-text')).toBeFalsy();
|
|
|
|
|
2024-04-08 14:04:08 +08:00
|
|
|
expect(errSpy).toHaveBeenCalledWith(
|
|
|
|
'Warning: [antd: Spin] `tip` only work in nest or fullscreen pattern.',
|
|
|
|
);
|
2024-01-19 22:20:50 +08:00
|
|
|
|
|
|
|
errSpy.mockRestore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not warn tip with fullscreen', () => {
|
|
|
|
const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
|
|
|
|
|
|
|
const { container } = render(<Spin fullscreen tip="Fullscreen" />);
|
|
|
|
expect(container.querySelector('.ant-spin-fullscreen')).toBeTruthy();
|
|
|
|
|
|
|
|
expect(errSpy).not.toHaveBeenCalled();
|
2023-05-12 09:51:44 +08:00
|
|
|
|
|
|
|
errSpy.mockRestore();
|
|
|
|
});
|
2023-12-09 15:03:48 +08:00
|
|
|
|
|
|
|
it('right style when fullscreen', () => {
|
|
|
|
const { container } = render(<Spin fullscreen spinning />);
|
|
|
|
const element = container.querySelector<HTMLDivElement>('.ant-spin.ant-spin-fullscreen');
|
|
|
|
expect(element).not.toHaveStyle({ pointerEvents: 'none' });
|
|
|
|
});
|
2017-01-27 14:27:18 +08:00
|
|
|
});
|