-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathindex.test.tsx
188 lines (155 loc) · 4.9 KB
/
index.test.tsx
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* global describe, it */
import React from 'react';
import { createRoot } from 'react-dom/client';
import { ParallaxController } from 'parallax-controller';
import { render } from '@testing-library/react';
import { ParallaxProvider } from '.';
import { useParallaxController } from '../../hooks/useParallaxController';
import * as helpers from './helpers';
describe('A <ParallaxProvider>', () => {
it('to render children', () => {
const node = document.createElement('div');
const child = jest.fn();
const Child = () => {
child();
return <div />;
};
const render = () => {
const root = createRoot(node);
root.render(
<ParallaxProvider>
<Child />
</ParallaxProvider>
);
};
render();
expect(child).toBeCalled();
});
it('to pass the controller context', () => {
let parallaxController;
const ContextChecker = () => {
parallaxController = useParallaxController();
return null;
};
render(
<ParallaxProvider>
<ContextChecker />
</ParallaxProvider>
);
// Expected methods and state
expect(parallaxController).toBeInstanceOf(ParallaxController);
});
it('calls to createController only once', () => {
jest.spyOn(helpers, 'createController');
const { rerender } = render(<ParallaxProvider />);
rerender(<ParallaxProvider />);
rerender(<ParallaxProvider />);
rerender(<ParallaxProvider />);
expect(helpers.createController).toHaveBeenCalledTimes(1);
});
it('to disable parallax elements and re-enable', () => {
let parallaxController: ParallaxController | null = null;
const ContextChecker = () => {
parallaxController = useParallaxController();
if (parallaxController) {
parallaxController.disableParallaxController = jest.fn();
parallaxController.enableParallaxController = jest.fn();
}
return null;
};
const context = render(
<ParallaxProvider isDisabled>
<ContextChecker />
</ParallaxProvider>
);
expect(
// @ts-expect-error
parallaxController.disabled
).toBe(true);
context.rerender(
<ParallaxProvider>
<ContextChecker />
</ParallaxProvider>
);
expect(
// @ts-expect-error
parallaxController.enableParallaxController
).toBeCalled();
});
it('to destroy the controller when unmounting', () => {
let parallaxController: ParallaxController | null = null;
const AddDestroySpy = () => {
parallaxController = useParallaxController();
if (parallaxController) {
jest.spyOn(parallaxController, 'destroy');
}
return null;
};
const screen = render(
<ParallaxProvider>
<AddDestroySpy />
</ParallaxProvider>
);
screen.unmount();
expect(
(parallaxController as unknown as ParallaxController)?.destroy
).toBeCalled();
});
it('to update the scroll container when receiving a new container el', () => {
let parallaxController: ParallaxController | null = null;
const AddUpdateSpy = () => {
parallaxController = useParallaxController();
if (parallaxController) {
jest.spyOn(parallaxController, 'updateScrollContainer');
}
return null;
};
const el = document.createElement('div');
const screen = render(
<ParallaxProvider>
<AddUpdateSpy />
</ParallaxProvider>
);
screen.rerender(
<ParallaxProvider scrollContainer={el}>
<AddUpdateSpy />
</ParallaxProvider>
);
screen.unmount();
// @ts-expect-error
expect(parallaxController?.updateScrollContainer).toBeCalledWith(el);
});
// NOTE: I think this test can be removed
it('to always create a new instance when re-mounting', () => {
// the provider isn't guaranteed to be destroyed before re-instantiated
// in a route change.
// this test asserts the controller on the provider will still be
// available, even if the legacy global instance is destroyed.
const node1 = document.createElement('div');
const node2 = document.createElement('div');
const render = (node: HTMLDivElement) => {
let instance: ParallaxController | null = null;
const GetInstance = () => {
instance = useParallaxController();
return null;
};
const root = createRoot(node);
root.render(
<ParallaxProvider>
<GetInstance />
</ParallaxProvider>
);
return { instance, root };
};
// first instance mounted
const { instance: instance1, root: root1 } = render(node1);
expect(instance1).toBeInstanceOf(ParallaxController);
// second instance mounted
const { instance: instance2 } = render(node2);
expect(instance2).toBeInstanceOf(ParallaxController);
// unmount first instance
root1.unmount();
// this must still be defined
expect(instance2).toBeInstanceOf(ParallaxController);
});
});