ant-design/components/_util/__tests__/scrollTo.test.ts

76 lines
1.7 KiB
TypeScript
Raw Normal View History

import { waitFakeTimer } from '../../../tests/utils';
2022-06-22 14:57:09 +08:00
import scrollTo from '../scrollTo';
describe('Test ScrollTo function', () => {
let dateNowMock: jest.SpyInstance;
beforeAll(() => {
jest.useFakeTimers();
});
beforeEach(() => {
dateNowMock = jest
.spyOn(Date, 'now')
.mockImplementationOnce(() => 0)
.mockImplementationOnce(() => 1000);
});
afterAll(() => {
jest.useRealTimers();
});
afterEach(() => {
jest.clearAllTimers();
dateNowMock.mockRestore();
});
it('test scrollTo', async () => {
const scrollToSpy = jest.spyOn(window, 'scrollTo').mockImplementation((_, y) => {
window.scrollY = y;
window.pageYOffset = y;
});
2019-07-30 12:34:12 +08:00
scrollTo(1000);
await waitFakeTimer();
expect(window.pageYOffset).toBe(1000);
scrollToSpy.mockRestore();
});
it('test callback - option', async () => {
const cbMock = jest.fn();
2019-07-30 12:49:32 +08:00
scrollTo(1000, {
callback: cbMock,
});
await waitFakeTimer();
expect(cbMock).toHaveBeenCalledTimes(1);
});
it('test getContainer - option', async () => {
const div = document.createElement('div');
2019-07-30 12:34:12 +08:00
scrollTo(1000, {
getContainer: () => div,
});
await waitFakeTimer();
expect(div.scrollTop).toBe(1000);
});
2020-05-15 14:57:12 +08:00
it('test getContainer document - option', async () => {
scrollTo(1000, {
getContainer: () => document,
});
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,
});
await waitFakeTimer();
2020-05-15 14:57:12 +08:00
expect(document.documentElement.scrollTop).toBe(1000);
});
});