2017-07-07 16:29:03 +08:00
|
|
|
import React from 'react';
|
|
|
|
import { mount } from 'enzyme';
|
2019-08-06 17:56:13 +08:00
|
|
|
import { sleep } from '../../../tests/utils';
|
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';
|
2017-07-07 16:29:03 +08:00
|
|
|
import BackTop from '..';
|
|
|
|
|
|
|
|
describe('BackTop', () => {
|
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 () => {
|
2017-07-07 16:29:03 +08:00
|
|
|
const wrapper = mount(<BackTop visibilityHeight={-1} />);
|
2019-07-24 22:56:20 +08:00
|
|
|
const scrollToSpy = jest.spyOn(window, 'scrollTo').mockImplementation((x, 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);
|
2020-04-01 17:38:21 +08:00
|
|
|
expect(document.documentElement.scrollTop).toBe(400);
|
2017-07-07 16:29:03 +08:00
|
|
|
// trigger scroll manually
|
2017-09-20 16:26:18 +08:00
|
|
|
wrapper.instance().handleScroll();
|
2019-08-06 22:10:57 +08:00
|
|
|
await sleep();
|
2018-12-14 00:49:28 +08:00
|
|
|
wrapper.find('.ant-back-top').simulate('click');
|
2019-08-06 21:45:46 +08:00
|
|
|
await sleep(500);
|
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
|
|
|
|
|
|
|
it('support onClick', async () => {
|
|
|
|
const onClick = jest.fn();
|
|
|
|
const wrapper = mount(<BackTop onClick={onClick} visibilityHeight={-1} />);
|
|
|
|
const scrollToSpy = jest.spyOn(window, 'scrollTo').mockImplementation((x, y) => {
|
|
|
|
window.scrollY = y;
|
|
|
|
window.pageYOffset = y;
|
|
|
|
});
|
2020-04-01 17:38:21 +08:00
|
|
|
document.dispatchEvent(new Event('scroll'));
|
2019-08-22 18:04:10 +08:00
|
|
|
window.scrollTo(0, 400);
|
|
|
|
// trigger scroll manually
|
|
|
|
wrapper.instance().handleScroll();
|
|
|
|
await sleep();
|
|
|
|
wrapper.find('.ant-back-top').simulate('click');
|
|
|
|
expect(onClick).toHaveBeenCalled();
|
|
|
|
scrollToSpy.mockRestore();
|
|
|
|
});
|
2020-04-01 17:38:21 +08:00
|
|
|
|
|
|
|
it('should be able to update target', async () => {
|
|
|
|
const wrapper = mount(<BackTop />);
|
|
|
|
wrapper.instance().handleScroll = jest.fn();
|
|
|
|
const container = document.createElement('div');
|
|
|
|
wrapper.setProps({ target: () => container });
|
|
|
|
expect(wrapper.instance().handleScroll).toHaveBeenLastCalledWith({
|
|
|
|
target: container,
|
|
|
|
});
|
|
|
|
container.dispatchEvent(new Event('scroll'));
|
|
|
|
expect(wrapper.instance().handleScroll).toHaveBeenLastCalledWith(
|
|
|
|
expect.objectContaining({
|
|
|
|
currentTarget: container,
|
|
|
|
target: container,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('invalid target', async () => {
|
|
|
|
const onClick = jest.fn();
|
|
|
|
const wrapper = mount(<BackTop onClick={onClick} target={() => ({ documentElement: {} })} />);
|
|
|
|
wrapper.find('.ant-back-top').simulate('click');
|
|
|
|
expect(onClick).toHaveBeenCalled();
|
|
|
|
});
|
2017-07-07 16:29:03 +08:00
|
|
|
});
|