2020-04-01 17:38:21 +08:00
|
|
|
import getScroll from '../getScroll';
|
|
|
|
|
|
|
|
describe('getScroll', () => {
|
2024-03-22 21:29:19 +08:00
|
|
|
it('getScroll target null', () => {
|
2024-06-15 15:39:01 +08:00
|
|
|
expect(getScroll(null)).toBe(0);
|
2020-04-01 17:38:21 +08:00
|
|
|
});
|
2020-05-15 16:14:38 +08:00
|
|
|
|
2024-03-22 21:29:19 +08:00
|
|
|
it('getScroll window', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const scrollToSpy = jest.spyOn(window, 'scrollTo').mockImplementation((x, y) => {
|
2020-05-15 16:14:38 +08:00
|
|
|
window.pageXOffset = x;
|
|
|
|
window.pageYOffset = y;
|
|
|
|
});
|
2024-06-15 15:39:01 +08:00
|
|
|
window.scrollTo(0, 400);
|
|
|
|
expect(getScroll(window)).toBe(400);
|
2020-05-15 16:14:38 +08:00
|
|
|
scrollToSpy.mockRestore();
|
|
|
|
});
|
|
|
|
|
2024-03-22 21:29:19 +08:00
|
|
|
it('getScroll document', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const scrollToSpy = jest.spyOn(window, 'scrollTo').mockImplementation((x, y) => {
|
2020-05-15 16:14:38 +08:00
|
|
|
document.documentElement.scrollLeft = x;
|
|
|
|
document.documentElement.scrollTop = y;
|
|
|
|
});
|
2024-06-15 15:39:01 +08:00
|
|
|
window.scrollTo(0, 400);
|
|
|
|
expect(getScroll(document)).toBe(400);
|
2020-05-15 16:14:38 +08:00
|
|
|
scrollToSpy.mockRestore();
|
|
|
|
});
|
|
|
|
|
2024-03-22 21:29:19 +08:00
|
|
|
it('getScroll div', () => {
|
2020-05-15 16:14:38 +08:00
|
|
|
const div = document.createElement('div');
|
2023-06-07 21:59:21 +08:00
|
|
|
const scrollToSpy = jest.spyOn(window, 'scrollTo').mockImplementation((x, y) => {
|
2020-05-15 16:14:38 +08:00
|
|
|
div.scrollLeft = x;
|
|
|
|
div.scrollTop = y;
|
|
|
|
});
|
2024-06-15 15:39:01 +08:00
|
|
|
window.scrollTo(0, 400);
|
|
|
|
expect(getScroll(div)).toBe(400);
|
2020-05-15 16:14:38 +08:00
|
|
|
scrollToSpy.mockRestore();
|
|
|
|
});
|
|
|
|
|
2024-03-22 21:29:19 +08:00
|
|
|
it('getScroll documentElement', () => {
|
2022-08-21 23:25:00 +08:00
|
|
|
const div: any = {};
|
2023-06-07 21:59:21 +08:00
|
|
|
const scrollToSpy = jest.spyOn(window, 'scrollTo').mockImplementation((x, y) => {
|
2020-05-15 16:14:38 +08:00
|
|
|
div.scrollLeft = null;
|
|
|
|
div.scrollTop = null;
|
|
|
|
div.documentElement = {};
|
|
|
|
div.documentElement.scrollLeft = x;
|
|
|
|
div.documentElement.scrollTop = y;
|
|
|
|
});
|
2024-06-15 15:39:01 +08:00
|
|
|
window.scrollTo(0, 400);
|
|
|
|
expect(getScroll(div)).toBe(400);
|
2020-05-15 16:14:38 +08:00
|
|
|
scrollToSpy.mockRestore();
|
|
|
|
});
|
2024-08-07 20:11:57 +08:00
|
|
|
|
|
|
|
it('When window is undef, getScroll value is zero', () => {
|
|
|
|
const spy = jest.spyOn(global, 'window', 'get').mockImplementation(() => undefined as any);
|
|
|
|
expect(getScroll(null)).toBe(0);
|
|
|
|
spy.mockRestore();
|
|
|
|
});
|
2020-04-01 17:38:21 +08:00
|
|
|
});
|