2020-04-01 17:38:21 +08:00
|
|
|
import getScroll from '../getScroll';
|
|
|
|
|
|
|
|
describe('getScroll', () => {
|
2020-05-15 16:14:38 +08:00
|
|
|
it('getScroll target null', async () => {
|
2020-04-01 17:38:21 +08:00
|
|
|
expect(getScroll(null, true)).toBe(0);
|
|
|
|
expect(getScroll(null, false)).toBe(0);
|
|
|
|
});
|
2020-05-15 16:14:38 +08:00
|
|
|
|
|
|
|
it('getScroll window', async () => {
|
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;
|
|
|
|
});
|
|
|
|
window.scrollTo(200, 400);
|
|
|
|
expect(getScroll(window, true)).toBe(400);
|
|
|
|
expect(getScroll(window, false)).toBe(200);
|
|
|
|
scrollToSpy.mockRestore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('getScroll document', async () => {
|
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;
|
|
|
|
});
|
|
|
|
window.scrollTo(200, 400);
|
|
|
|
expect(getScroll(document, true)).toBe(400);
|
|
|
|
expect(getScroll(document, false)).toBe(200);
|
|
|
|
scrollToSpy.mockRestore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('getScroll div', async () => {
|
|
|
|
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;
|
|
|
|
});
|
|
|
|
window.scrollTo(200, 400);
|
|
|
|
expect(getScroll(div, true)).toBe(400);
|
|
|
|
expect(getScroll(div, false)).toBe(200);
|
|
|
|
scrollToSpy.mockRestore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('getScroll documentElement', async () => {
|
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;
|
|
|
|
});
|
|
|
|
window.scrollTo(200, 400);
|
|
|
|
expect(getScroll(div, true)).toBe(400);
|
|
|
|
expect(getScroll(div, false)).toBe(200);
|
|
|
|
scrollToSpy.mockRestore();
|
|
|
|
});
|
2020-04-01 17:38:21 +08:00
|
|
|
});
|