2022-10-12 22:46:16 +08:00
|
|
|
import { waitFakeTimer } from '../../../tests/utils';
|
2022-06-22 14:57:09 +08:00
|
|
|
import scrollTo from '../scrollTo';
|
2019-07-24 22:56:20 +08:00
|
|
|
|
|
|
|
describe('Test ScrollTo function', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const dateNowMock = jest.spyOn(Date, 'now');
|
2019-08-08 20:32:06 +08:00
|
|
|
|
2022-10-12 22:46:16 +08:00
|
|
|
beforeAll(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.useFakeTimers();
|
2022-10-12 22:46:16 +08:00
|
|
|
});
|
|
|
|
|
2019-08-08 20:32:06 +08:00
|
|
|
beforeEach(() => {
|
2022-10-19 11:50:18 +08:00
|
|
|
dateNowMock.mockReturnValueOnce(0).mockReturnValueOnce(1000);
|
2019-08-08 20:32:06 +08:00
|
|
|
});
|
|
|
|
|
2022-10-12 22:46:16 +08:00
|
|
|
afterAll(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.useRealTimers();
|
2022-10-12 22:46:16 +08:00
|
|
|
});
|
|
|
|
|
2019-08-08 20:32:06 +08:00
|
|
|
afterEach(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.clearAllTimers();
|
2022-10-19 11:50:18 +08:00
|
|
|
dateNowMock.mockClear();
|
2019-08-08 20:32:06 +08:00
|
|
|
});
|
|
|
|
|
2019-07-24 22:56:20 +08:00
|
|
|
it('test scrollTo', async () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const scrollToSpy = jest.spyOn(window, 'scrollTo').mockImplementation((_, 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);
|
2022-10-12 22:46:16 +08:00
|
|
|
await waitFakeTimer();
|
2019-08-08 20:32:06 +08:00
|
|
|
|
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 () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const cbMock = jest.fn();
|
2019-07-30 12:49:32 +08:00
|
|
|
scrollTo(1000, {
|
2019-07-24 22:56:20 +08:00
|
|
|
callback: cbMock,
|
|
|
|
});
|
2022-10-12 22:46:16 +08:00
|
|
|
await waitFakeTimer();
|
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,
|
|
|
|
});
|
2022-10-12 22:46:16 +08:00
|
|
|
await waitFakeTimer();
|
2019-07-24 22:56:20 +08:00
|
|
|
expect(div.scrollTop).toBe(1000);
|
|
|
|
});
|
2020-05-15 14:57:12 +08:00
|
|
|
|
|
|
|
it('test getContainer document - option', async () => {
|
|
|
|
scrollTo(1000, {
|
|
|
|
getContainer: () => document,
|
|
|
|
});
|
2022-10-12 22:46:16 +08:00
|
|
|
await waitFakeTimer();
|
2020-05-15 14:57:12 +08:00
|
|
|
expect(document.documentElement.scrollTop).toBe(1000);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('test duration - option', async () => {
|
|
|
|
scrollTo(1000, {
|
|
|
|
duration: 1100,
|
|
|
|
getContainer: () => document,
|
|
|
|
});
|
2022-10-12 22:46:16 +08:00
|
|
|
await waitFakeTimer();
|
2020-05-15 14:57:12 +08:00
|
|
|
expect(document.documentElement.scrollTop).toBe(1000);
|
|
|
|
});
|
2019-07-24 22:56:20 +08:00
|
|
|
});
|