2022-09-23 14:31:16 +08:00
|
|
|
import React from 'react';
|
|
|
|
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;
|
|
|
|
describe('BackTop', () => {
|
2022-11-24 14:59:17 +08:00
|
|
|
beforeEach(() => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
});
|
|
|
|
afterEach(() => {
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
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 />);
|
2022-09-23 14:31:16 +08:00
|
|
|
const scrollToSpy = jest.spyOn(window, 'scrollTo').mockImplementation((_, y) => {
|
|
|
|
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', () => {
|
|
|
|
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', () => {
|
2022-09-23 14:31:16 +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
|
|
|
});
|
2022-09-23 14:31:16 +08:00
|
|
|
});
|