File size: 1,771 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import getScroll from '../getScroll';

describe('getScroll', () => {
  it('getScroll target null', () => {
    expect(getScroll(null)).toBe(0);
  });

  it('getScroll window', () => {
    const scrollToSpy = jest.spyOn(window, 'scrollTo').mockImplementation((x, y) => {
      window.pageXOffset = x;
      window.pageYOffset = y;
    });
    window.scrollTo(0, 400);
    expect(getScroll(window)).toBe(400);
    scrollToSpy.mockRestore();
  });

  it('getScroll document', () => {
    const scrollToSpy = jest.spyOn(window, 'scrollTo').mockImplementation((x, y) => {
      document.documentElement.scrollLeft = x;
      document.documentElement.scrollTop = y;
    });
    window.scrollTo(0, 400);
    expect(getScroll(document)).toBe(400);
    scrollToSpy.mockRestore();
  });

  it('getScroll div', () => {
    const div = document.createElement('div');
    const scrollToSpy = jest.spyOn(window, 'scrollTo').mockImplementation((x, y) => {
      div.scrollLeft = x;
      div.scrollTop = y;
    });
    window.scrollTo(0, 400);
    expect(getScroll(div)).toBe(400);
    scrollToSpy.mockRestore();
  });

  it('getScroll documentElement', () => {
    const div: any = {};
    const scrollToSpy = jest.spyOn(window, 'scrollTo').mockImplementation((x, y) => {
      div.scrollLeft = null;
      div.scrollTop = null;
      div.documentElement = {};
      div.documentElement.scrollLeft = x;
      div.documentElement.scrollTop = y;
    });
    window.scrollTo(0, 400);
    expect(getScroll(div)).toBe(400);
    scrollToSpy.mockRestore();
  });

  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();
  });
});