2019-07-24 22:56:20 +08:00
|
|
|
import scrollTo from '../scrollTo';
|
|
|
|
import { sleep } from '../../../tests/utils';
|
|
|
|
|
|
|
|
describe('Test ScrollTo function', () => {
|
|
|
|
it('test scrollTo', async () => {
|
|
|
|
const scrollToSpy = jest.spyOn(window, 'scrollTo').mockImplementation((x, y) => {
|
|
|
|
const w = window;
|
|
|
|
w.scrollY = y;
|
|
|
|
w.pageYOffset = y;
|
|
|
|
});
|
2019-07-30 12:34:12 +08:00
|
|
|
scrollTo(1000);
|
2019-07-24 22:56:20 +08:00
|
|
|
await sleep(1000);
|
|
|
|
expect(window.pageYOffset).toBe(1000);
|
|
|
|
scrollToSpy.mockRestore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('test callback - option', async () => {
|
|
|
|
const cbMock = jest.fn();
|
|
|
|
scrollTo(0, 1000, {
|
|
|
|
callback: cbMock,
|
|
|
|
});
|
|
|
|
await sleep(1000);
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
await sleep(1000);
|
|
|
|
expect(div.scrollTop).toBe(1000);
|
|
|
|
});
|
|
|
|
});
|