2022-09-23 14:31:16 +08:00
|
|
|
import React from 'react';
|
2023-10-20 17:19:17 +08:00
|
|
|
|
2022-09-23 14:31:16 +08:00
|
|
|
import FloatButton from '..';
|
|
|
|
import mountTest from '../../../tests/shared/mountTest';
|
|
|
|
import rtlTest from '../../../tests/shared/rtlTest';
|
2022-11-24 14:59:17 +08:00
|
|
|
import { fireEvent, render, waitFakeTimer } from '../../../tests/utils';
|
2022-09-23 14:31:16 +08:00
|
|
|
|
|
|
|
const { BackTop } = FloatButton;
|
2023-10-20 17:19:17 +08:00
|
|
|
|
2022-09-23 14:31:16 +08:00
|
|
|
describe('BackTop', () => {
|
2024-05-10 16:28:25 +08:00
|
|
|
const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
|
|
|
|
2022-11-24 14:59:17 +08:00
|
|
|
beforeEach(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.useFakeTimers();
|
2022-11-24 14:59:17 +08:00
|
|
|
});
|
|
|
|
afterEach(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.useRealTimers();
|
2022-11-24 14:59:17 +08:00
|
|
|
});
|
2022-09-23 14:31:16 +08:00
|
|
|
mountTest(BackTop);
|
|
|
|
rtlTest(BackTop);
|
|
|
|
|
|
|
|
it('should scroll to top after click it', async () => {
|
2022-11-24 14:59:17 +08:00
|
|
|
const { container } = render(<BackTop />);
|
2023-06-07 21:59:21 +08:00
|
|
|
const scrollToSpy = jest.spyOn(window, 'scrollTo').mockImplementation((_, y) => {
|
2022-09-23 14:31:16 +08:00
|
|
|
window.scrollY = y;
|
|
|
|
window.pageYOffset = y;
|
|
|
|
document.documentElement.scrollTop = y;
|
|
|
|
});
|
|
|
|
window.scrollTo(0, 400);
|
2022-11-24 14:59:17 +08:00
|
|
|
await waitFakeTimer();
|
2022-09-23 14:31:16 +08:00
|
|
|
expect(document.documentElement.scrollTop).toBe(400);
|
2022-11-24 14:59:17 +08:00
|
|
|
fireEvent.click(container.querySelector<HTMLButtonElement>('.ant-float-btn')!);
|
|
|
|
await waitFakeTimer();
|
2022-09-23 14:31:16 +08:00
|
|
|
expect(document.documentElement.scrollTop).toBe(0);
|
|
|
|
scrollToSpy.mockRestore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('support onClick', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const onClick = jest.fn();
|
2022-11-24 14:59:17 +08:00
|
|
|
const { container } = render(<BackTop onClick={onClick} visibilityHeight={0} />);
|
|
|
|
fireEvent.click(container.querySelector<HTMLButtonElement>('.ant-float-btn')!);
|
2022-09-23 14:31:16 +08:00
|
|
|
expect(onClick).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2022-11-24 14:59:17 +08:00
|
|
|
it('support invalid target', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const onClick = jest.fn();
|
2022-11-24 14:59:17 +08:00
|
|
|
const { container } = render(
|
|
|
|
<BackTop onClick={onClick} visibilityHeight={0} target={undefined} />,
|
|
|
|
);
|
|
|
|
fireEvent.click(container.querySelector<HTMLButtonElement>('.ant-float-btn')!);
|
2022-09-23 14:31:16 +08:00
|
|
|
expect(onClick).toHaveBeenCalled();
|
|
|
|
});
|
2022-11-11 20:51:51 +08:00
|
|
|
|
|
|
|
it('pass style to float button', () => {
|
2022-11-24 14:59:17 +08:00
|
|
|
const { container } = render(<BackTop style={{ color: 'red' }} visibilityHeight={0} />);
|
|
|
|
expect(container.querySelector<HTMLButtonElement>('.ant-float-btn')?.style.color).toBe('red');
|
2022-11-11 20:51:51 +08:00
|
|
|
});
|
2023-10-20 17:19:17 +08:00
|
|
|
|
|
|
|
it('no error when BackTop work', () => {
|
|
|
|
render(<BackTop visibilityHeight={0} />);
|
|
|
|
expect(errSpy).not.toHaveBeenCalled();
|
|
|
|
errSpy.mockRestore();
|
|
|
|
});
|
2022-09-23 14:31:16 +08:00
|
|
|
});
|