Skip to content

Commit a33d4ba

Browse files
committed
parallax component test
1 parent e3e2b68 commit a33d4ba

File tree

1 file changed

+161
-0
lines changed

1 file changed

+161
-0
lines changed

__tests__/Parallax.test.js

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
import Parallax from 'components/Parallax';
4+
import ParallaxProvider from 'components/ParallaxProvider';
5+
import ParallaxController from 'libs/ParallaxController';
6+
7+
const log = global.console.log;
8+
9+
describe('Expect the <Parallax> component', () => {
10+
11+
afterEach(() => {
12+
global.console.log = log;
13+
global.ParallaxController = undefined;
14+
});
15+
16+
it('to throw if the ParallaxController is not available', () => {
17+
const node = document.createElement('div');
18+
19+
const render = () => {
20+
ReactDOM.render(
21+
<Parallax>
22+
<div />
23+
</Parallax>,
24+
node
25+
);
26+
};
27+
28+
expect(render).toThrow(
29+
'Must wrap your application\'s <Parallax /> components in a <ParallaxProvider />.'
30+
);
31+
});
32+
33+
it('to warn if the ParallaxController is found but not provided by <ParallaxProvider>', () => {
34+
const node = document.createElement('div');
35+
36+
global.console.log = jest.fn();
37+
global.ParallaxController = ParallaxController.init();
38+
39+
const render = () => {
40+
ReactDOM.render(
41+
<Parallax>
42+
<div />
43+
</Parallax>,
44+
node
45+
);
46+
};
47+
48+
render();
49+
50+
expect(global.console.log).toBeCalledWith(
51+
'Calling ParallaxController.init() has been deprecated in favor of using the <ParallaxProvider /> component. For usage details see: https://github.com/jscottsmith/react-scroll-parallax/tree/v1.1.0#usage'
52+
);
53+
});
54+
55+
it('to create an element in the controller on mount', () => {
56+
const node = document.createElement('div');
57+
58+
global.ParallaxController = ParallaxController.init();
59+
global.ParallaxController.createElement = jest.fn();
60+
const spy = global.ParallaxController.createElement;
61+
62+
const render = () => {
63+
ReactDOM.render(
64+
<ParallaxProvider>
65+
<Parallax offsetYMin={-100} offsetYMax={100}>
66+
<div />
67+
</Parallax>
68+
</ParallaxProvider>,
69+
node
70+
);
71+
};
72+
73+
render();
74+
75+
expect(spy).toBeCalledWith({
76+
elInner: expect.any(Object),
77+
elOuter: expect.any(Object),
78+
props: {
79+
disabled: false,
80+
offsetXMax: 0,
81+
offsetXMin: 0,
82+
offsetYMax: 100,
83+
offsetYMin: -100,
84+
slowerScrollRate: false,
85+
},
86+
});
87+
});
88+
89+
it('to remove an element in the controller when unmounting', () => {
90+
const node = document.createElement('div');
91+
92+
global.ParallaxController = ParallaxController.init();
93+
global.ParallaxController.removeElement = jest.fn();
94+
const spy = global.ParallaxController.removeElement;
95+
96+
let instance;
97+
const render = () => {
98+
ReactDOM.render(
99+
<ParallaxProvider>
100+
<Parallax ref={ref => instance = ref}>
101+
<div />
102+
</Parallax>
103+
</ParallaxProvider>,
104+
node
105+
);
106+
};
107+
108+
render();
109+
const element = instance.element;
110+
ReactDOM.unmountComponentAtNode(node);
111+
expect(spy).toBeCalledWith(element);
112+
});
113+
114+
it('to update an element in the controller when receiving new props and disable an element if the disable prop is true', () => {
115+
const node = document.createElement('div');
116+
117+
global.ParallaxController = ParallaxController.init();
118+
global.ParallaxController.updateElement = jest.fn();
119+
global.ParallaxController.resetElementStyles = jest.fn();
120+
121+
let instance;
122+
123+
class StateChanger extends React.Component {
124+
state = { disabled: false };
125+
126+
componentDidMount() {
127+
this.setState({ disabled: true });
128+
}
129+
130+
render() {
131+
return (
132+
<Parallax disabled={this.state.disabled} ref={ref => instance = ref} />
133+
);
134+
}
135+
}
136+
137+
const render = () => {
138+
ReactDOM.render(
139+
<ParallaxProvider>
140+
<StateChanger />
141+
</ParallaxProvider>,
142+
node
143+
);
144+
};
145+
146+
render();
147+
148+
expect(global.ParallaxController.updateElement).toBeCalledWith(instance.element, {
149+
props: {
150+
disabled: instance.props.disabled,
151+
offsetXMax: instance.props.offsetXMax,
152+
offsetXMin: instance.props.offsetXMin,
153+
offsetYMax: instance.props.offsetYMax,
154+
offsetYMin: instance.props.offsetYMin,
155+
slowerScrollRate: instance.props.slowerScrollRate,
156+
},
157+
});
158+
159+
expect(global.ParallaxController.resetElementStyles).toBeCalledWith(instance.element);
160+
});
161+
});

0 commit comments

Comments
 (0)