Skip to content

Commit d811d75

Browse files
committed
adding tests
1 parent 4eeb26a commit d811d75

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { ParallaxController } from 'react-scroll-parallax';
2+
3+
const addEventListener = window.addEventListener;
4+
const removeEventListener = window.removeEventListener;
5+
6+
describe('Expect the ParallaxController', () => {
7+
8+
afterEach(() => {
9+
window.addEventListener = addEventListener;
10+
window.removeEventListener = removeEventListener;
11+
});
12+
13+
it('to return an instance on init', () => {
14+
const instance = ParallaxController.init();
15+
expect(instance).toBeInstanceOf(ParallaxController);
16+
});
17+
18+
it('to return an existing instance from the window on init', () => {
19+
window.ParallaxController = 'foo';
20+
const instance = ParallaxController.init();
21+
expect(instance).toBe('foo');
22+
window.ParallaxController = undefined;
23+
});
24+
25+
it('to throw on init if there\'s no window');
26+
27+
it('to add listeners when init', () => {
28+
window.addEventListener = jest.fn();
29+
const instance = ParallaxController.init();
30+
31+
expect(window.addEventListener.mock.calls[1]).toEqual(
32+
expect.arrayContaining(['scroll', expect.any(Function), false])
33+
);
34+
expect(window.addEventListener.mock.calls[2]).toEqual(
35+
expect.arrayContaining(['resize', expect.any(Function), false])
36+
);
37+
});
38+
39+
it('to remove listeners when destroyed', () => {
40+
window.removeEventListener = jest.fn();
41+
const instance = ParallaxController.init();
42+
43+
instance.destroy();
44+
expect(window.removeEventListener.mock.calls[0]).toEqual(
45+
expect.arrayContaining(['scroll', expect.any(Function), false])
46+
);
47+
expect(window.removeEventListener.mock.calls[1]).toEqual(
48+
expect.arrayContaining(['resize', expect.any(Function), false])
49+
);
50+
expect(window.ParallaxController).toBe(null);
51+
});
52+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { testForPassiveScroll } from 'utils';
2+
3+
const addEventListener = window.addEventListener;
4+
const removeEventListener = window.removeEventListener;
5+
6+
describe('Expect the testForPassiveScroll function', () => {
7+
8+
afterEach(() => {
9+
window.addEventListener = addEventListener;
10+
window.removeEventListener = removeEventListener;
11+
});
12+
13+
it('to return a boolean', () => {
14+
const bool = testForPassiveScroll();
15+
expect(bool).toBe(false)
16+
});
17+
18+
it('to add and remove a test listener', () => {
19+
window.addEventListener = jest.fn();
20+
window.removeEventListener = jest.fn();
21+
const bool = testForPassiveScroll();
22+
expect(window.addEventListener.mock.calls[0]).toEqual(
23+
expect.arrayContaining(['test', null, expect.any(Object)])
24+
);
25+
expect(window.removeEventListener.mock.calls[0]).toEqual(
26+
expect.arrayContaining(['test', null, expect.any(Object)])
27+
);
28+
});
29+
});

0 commit comments

Comments
 (0)