2017-07-07 16:29:03 +08:00
|
|
|
import React from 'react';
|
2022-06-22 14:57:09 +08:00
|
|
|
import BackTop 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';
|
2022-10-21 15:10:20 +08:00
|
|
|
import { fireEvent, render, waitFakeTimer } from '../../../tests/utils';
|
2017-07-07 16:29:03 +08:00
|
|
|
|
|
|
|
describe('BackTop', () => {
|
2022-11-24 14:59:17 +08:00
|
|
|
beforeEach(() => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
});
|
|
|
|
afterEach(() => {
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
2019-08-26 22:53:20 +08:00
|
|
|
mountTest(BackTop);
|
2020-01-02 19:10:16 +08:00
|
|
|
rtlTest(BackTop);
|
2019-08-26 22:53:20 +08:00
|
|
|
|
2018-12-14 00:23:39 +08:00
|
|
|
it('should scroll to top after click it', async () => {
|
2022-11-24 14:59:17 +08:00
|
|
|
const { container } = render(<BackTop />);
|
2022-09-05 19:41:32 +08:00
|
|
|
const scrollToSpy = jest.spyOn(window, 'scrollTo').mockImplementation((_, y) => {
|
2019-08-06 21:45:46 +08:00
|
|
|
window.scrollY = y;
|
|
|
|
window.pageYOffset = y;
|
2020-04-01 17:38:21 +08:00
|
|
|
document.documentElement.scrollTop = y;
|
2019-07-24 22:56:20 +08:00
|
|
|
});
|
|
|
|
window.scrollTo(0, 400);
|
2022-11-24 14:59:17 +08:00
|
|
|
await waitFakeTimer();
|
2020-04-01 17:38:21 +08:00
|
|
|
expect(document.documentElement.scrollTop).toBe(400);
|
2022-11-24 14:59:17 +08:00
|
|
|
fireEvent.click(container.querySelector<HTMLDivElement>('.ant-back-top')!);
|
2022-10-21 15:10:20 +08:00
|
|
|
await waitFakeTimer();
|
2020-04-01 17:38:21 +08:00
|
|
|
expect(document.documentElement.scrollTop).toBe(0);
|
2019-07-24 22:56:20 +08:00
|
|
|
scrollToSpy.mockRestore();
|
2017-07-07 16:29:03 +08:00
|
|
|
});
|
2019-08-22 18:04:10 +08:00
|
|
|
|
2022-11-24 14:59:17 +08:00
|
|
|
it('support onClick', () => {
|
2019-08-22 18:04:10 +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<HTMLDivElement>('.ant-back-top')!);
|
2019-08-22 18:04:10 +08:00
|
|
|
expect(onClick).toHaveBeenCalled();
|
|
|
|
});
|
2020-04-01 17:38:21 +08:00
|
|
|
|
2022-11-24 14:59:17 +08:00
|
|
|
it('invalid target', () => {
|
2020-04-01 17:38:21 +08:00
|
|
|
const onClick = jest.fn();
|
2022-11-24 14:59:17 +08:00
|
|
|
const { container } = render(<BackTop onClick={onClick} target={undefined} />);
|
|
|
|
fireEvent.click(container.querySelector<HTMLDivElement>('.ant-back-top')!);
|
2020-04-01 17:38:21 +08:00
|
|
|
expect(onClick).toHaveBeenCalled();
|
|
|
|
});
|
2022-09-19 09:55:11 +08:00
|
|
|
it('should console Error', () => {
|
|
|
|
const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
|
|
|
render(<BackTop />);
|
|
|
|
expect(errSpy).toHaveBeenCalledWith(
|
|
|
|
'Warning: [antd: BackTop] `BackTop` is deprecated, please use `FloatButton.BackTop` instead.',
|
|
|
|
);
|
|
|
|
errSpy.mockRestore();
|
|
|
|
});
|
2017-07-07 16:29:03 +08:00
|
|
|
});
|