You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.7 KiB
47 lines
1.7 KiB
7 years ago
|
import React from 'react';
|
||
2 years ago
|
import BackTop from '..';
|
||
5 years ago
|
import mountTest from '../../../tests/shared/mountTest';
|
||
5 years ago
|
import rtlTest from '../../../tests/shared/rtlTest';
|
||
2 years ago
|
import { fireEvent, render, sleep } from '../../../tests/utils';
|
||
7 years ago
|
|
||
|
describe('BackTop', () => {
|
||
5 years ago
|
mountTest(BackTop);
|
||
5 years ago
|
rtlTest(BackTop);
|
||
5 years ago
|
|
||
6 years ago
|
it('should scroll to top after click it', async () => {
|
||
2 years ago
|
const { container } = render(<BackTop visibilityHeight={-1} />);
|
||
2 years ago
|
const scrollToSpy = jest.spyOn(window, 'scrollTo').mockImplementation((_, y) => {
|
||
5 years ago
|
window.scrollY = y;
|
||
|
window.pageYOffset = y;
|
||
5 years ago
|
document.documentElement.scrollTop = y;
|
||
5 years ago
|
});
|
||
|
window.scrollTo(0, 400);
|
||
5 years ago
|
expect(document.documentElement.scrollTop).toBe(400);
|
||
2 years ago
|
fireEvent.click(container.querySelector('.ant-back-top')!);
|
||
5 years ago
|
await sleep(500);
|
||
5 years ago
|
expect(document.documentElement.scrollTop).toBe(0);
|
||
5 years ago
|
scrollToSpy.mockRestore();
|
||
7 years ago
|
});
|
||
5 years ago
|
|
||
|
it('support onClick', async () => {
|
||
|
const onClick = jest.fn();
|
||
2 years ago
|
const { container } = render(<BackTop onClick={onClick} visibilityHeight={-1} />);
|
||
2 years ago
|
const scrollToSpy = jest.spyOn(window, 'scrollTo').mockImplementation((_, y) => {
|
||
5 years ago
|
window.scrollY = y;
|
||
|
window.pageYOffset = y;
|
||
|
});
|
||
5 years ago
|
document.dispatchEvent(new Event('scroll'));
|
||
5 years ago
|
window.scrollTo(0, 400);
|
||
2 years ago
|
fireEvent.click(container.querySelector('.ant-back-top')!);
|
||
5 years ago
|
expect(onClick).toHaveBeenCalled();
|
||
|
scrollToSpy.mockRestore();
|
||
|
});
|
||
5 years ago
|
|
||
|
it('invalid target', async () => {
|
||
|
const onClick = jest.fn();
|
||
2 years ago
|
const { container } = render(<BackTop onClick={onClick} visible target={undefined} />);
|
||
|
fireEvent.click(container.querySelector('.ant-back-top')!);
|
||
5 years ago
|
expect(onClick).toHaveBeenCalled();
|
||
|
});
|
||
7 years ago
|
});
|