2019-07-24 22:56:20 +08:00
|
|
|
import scrollTo from '../scrollTo';
|
|
|
|
|
|
|
|
describe('Test ScrollTo function', () => {
|
2019-08-08 20:32:06 +08:00
|
|
|
let dateNowMock;
|
|
|
|
|
|
|
|
beforeAll(() => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
dateNowMock = jest
|
|
|
|
.spyOn(Date, 'now')
|
|
|
|
.mockImplementationOnce(() => 0)
|
|
|
|
.mockImplementationOnce(() => 1000);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
dateNowMock.mockRestore();
|
|
|
|
});
|
|
|
|
|
2019-07-24 22:56:20 +08:00
|
|
|
it('test scrollTo', async () => {
|
|
|
|
const scrollToSpy = jest.spyOn(window, 'scrollTo').mockImplementation((x, y) => {
|
2019-08-06 16:23:53 +08:00
|
|
|
window.scrollY = y;
|
|
|
|
window.pageYOffset = y;
|
2019-07-24 22:56:20 +08:00
|
|
|
});
|
2019-08-08 20:32:06 +08:00
|
|
|
|
2019-07-30 12:34:12 +08:00
|
|
|
scrollTo(1000);
|
2019-08-08 20:32:06 +08:00
|
|
|
|
|
|
|
jest.runAllTimers();
|
2019-07-24 22:56:20 +08:00
|
|
|
expect(window.pageYOffset).toBe(1000);
|
2019-08-08 20:32:06 +08:00
|
|
|
|
2019-07-24 22:56:20 +08:00
|
|
|
scrollToSpy.mockRestore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('test callback - option', async () => {
|
|
|
|
const cbMock = jest.fn();
|
2019-07-30 12:49:32 +08:00
|
|
|
scrollTo(1000, {
|
2019-07-24 22:56:20 +08:00
|
|
|
callback: cbMock,
|
|
|
|
});
|
2019-08-08 20:32:06 +08:00
|
|
|
jest.runAllTimers();
|
2019-07-24 22:56:20 +08:00
|
|
|
expect(cbMock).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('test getContainer - option', async () => {
|
|
|
|
const div = document.createElement('div');
|
2019-07-30 12:34:12 +08:00
|
|
|
scrollTo(1000, {
|
2019-07-24 22:56:20 +08:00
|
|
|
getContainer: () => div,
|
|
|
|
});
|
2019-08-08 20:32:06 +08:00
|
|
|
jest.runAllTimers();
|
2019-07-24 22:56:20 +08:00
|
|
|
expect(div.scrollTop).toBe(1000);
|
|
|
|
});
|
|
|
|
});
|