ant-design/components/back-top/__tests__/index.test.js

30 lines
785 B
JavaScript
Raw Normal View History

2017-07-07 16:29:03 +08:00
import React from 'react';
import { mount } from 'enzyme';
import BackTop from '..';
describe('BackTop', () => {
beforeAll(() => {
jest.useFakeTimers();
});
afterAll(() => {
jest.useRealTimers();
});
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} />);
const scrollToSpy = jest.spyOn(window, 'scrollTo').mockImplementation((x, y) => {
const w = window;
w.scrollY = y;
w.pageYOffset = y;
});
window.scrollTo(0, 400);
2017-07-07 16:29:03 +08:00
// trigger scroll manually
2017-09-20 16:26:18 +08:00
wrapper.instance().handleScroll();
jest.runAllTimers();
2018-12-14 00:49:28 +08:00
wrapper.find('.ant-back-top').simulate('click');
jest.runAllTimers();
expect(window.pageYOffset).toBe(0);
scrollToSpy.mockRestore();
2017-07-07 16:29:03 +08:00
});
});