-
Notifications
You must be signed in to change notification settings - Fork 428
/
scroller.spec.js
70 lines (54 loc) · 2.01 KB
/
scroller.spec.js
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
60
61
62
63
64
65
66
67
68
69
70
import scroller from './scroller';
jest.useFakeTimers();
describe('scroller', () => {
let consoleMock;
let chanceMock;
let config;
const scrollToSpy = jest.fn();
beforeEach(() => {
consoleMock = { log: jest.fn() };
chanceMock = {
natural: ({ max }) => max,
};
Object.defineProperty(window, 'scrollTo', { value: scrollToSpy, writable: true });
const documentElementProps = {
clientWidth: 10,
clientHeight: 10,
scrollWidth: 15,
scrollHeight: 15,
};
Object.keys(documentElementProps).forEach((key) => {
Object.defineProperty(document.documentElement, key, {
value: documentElementProps[key],
});
});
config = {
logger: consoleMock,
randomizer: chanceMock,
window,
};
});
it('should log the scroller', () => {
const species = scroller({ log: true })(config);
species();
expect(consoleMock.log).toHaveBeenCalledTimes(1);
expect(consoleMock.log).toHaveBeenCalledWith('gremlin', 'scroller ', 'scroll to', 5, 5);
});
it("should scroll but don't show element", () => {
const species = scroller({ showAction: false })(config);
species();
expect(scrollToSpy).toHaveBeenCalledTimes(1);
expect(scrollToSpy).toHaveBeenCalledWith(5, 5);
expect(document.getElementsByTagName('div')).toHaveLength(1);
});
it('should scroll and add new element', () => {
const species = scroller()(config);
species();
expect(scrollToSpy).toHaveBeenCalledTimes(1);
expect(scrollToSpy).toHaveBeenCalledWith(5, 5);
expect(document.getElementsByTagName('div')).toHaveLength(2);
expect(setTimeout).toHaveBeenCalledTimes(2);
expect(setTimeout).toHaveBeenNthCalledWith(1, expect.any(Function), 1000);
expect(setTimeout).toHaveBeenNthCalledWith(2, expect.any(Function), 50);
});
});