-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathAutoSizer.jest.js
240 lines (220 loc) · 7.59 KB
/
AutoSizer.jest.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* global Element, Event */
import * as React from 'react';
import {findDOMNode} from 'react-dom';
import {render} from '../TestUtils';
import AutoSizer from './AutoSizer';
function DefaultChildComponent({height, width, foo, bar}) {
return (
<div>{`width:${width}, height:${height}, foo:${foo}, bar:${bar}`}</div>
);
}
describe('AutoSizer', () => {
function getMarkup({
bar = 123,
ChildComponent = DefaultChildComponent,
className = undefined,
defaultHeight = undefined,
defaultWidth = undefined,
disableHeight = false,
disableWidth = false,
foo = 456,
height = 100,
onResize,
paddingBottom = 0,
paddingLeft = 0,
paddingRight = 0,
paddingTop = 0,
style = undefined,
width = 200,
} = {}) {
const wrapperStyle = {
boxSizing: 'border-box',
height,
paddingBottom,
paddingLeft,
paddingRight,
paddingTop,
width,
};
mockOffsetSize(width, height);
return (
<div style={wrapperStyle}>
<AutoSizer
className={className}
defaultHeight={defaultHeight}
defaultWidth={defaultWidth}
disableHeight={disableHeight}
disableWidth={disableWidth}
onResize={onResize}
style={style}>
{({height, width}) => (
<ChildComponent
width={disableWidth ? undefined : width}
height={disableHeight ? undefined : height}
bar={bar}
foo={foo}
/>
)}
</AutoSizer>
</div>
);
}
// AutoSizer uses offsetWidth and offsetHeight.
// Jest runs in JSDom which doesn't support measurements APIs.
function mockOffsetSize(width, height) {
Object.defineProperty(HTMLElement.prototype, 'offsetHeight', {
configurable: true,
value: height,
});
Object.defineProperty(HTMLElement.prototype, 'offsetWidth', {
configurable: true,
value: width,
});
}
it('should relay properties to ChildComponent or React child', () => {
const rendered = findDOMNode(render(getMarkup()));
expect(rendered.textContent).toContain('foo:456');
expect(rendered.textContent).toContain('bar:123');
});
it('should set the correct initial width and height of ChildComponent or React child', () => {
const rendered = findDOMNode(render(getMarkup()));
expect(rendered.textContent).toContain('height:100');
expect(rendered.textContent).toContain('width:200');
});
it('should account for padding when calculating the available width and height', () => {
const rendered = findDOMNode(
render(
getMarkup({
paddingBottom: 10,
paddingLeft: 4,
paddingRight: 4,
paddingTop: 15,
}),
),
);
expect(rendered.textContent).toContain('height:75');
expect(rendered.textContent).toContain('width:192');
});
it('should not update :width if :disableWidth is true', () => {
const rendered = findDOMNode(render(getMarkup({disableWidth: true})));
expect(rendered.textContent).toContain('height:100');
expect(rendered.textContent).toContain('width:undefined');
});
it('should not update :height if :disableHeight is true', () => {
const rendered = findDOMNode(render(getMarkup({disableHeight: true})));
expect(rendered.textContent).toContain('height:undefined');
expect(rendered.textContent).toContain('width:200');
});
async function simulateResize({element, height, width}) {
mockOffsetSize(width, height);
// Trigger detectElementResize library by faking a scroll event
// TestUtils Simulate doesn't work here in JSDom so we manually dispatch
const trigger = element.querySelector('.contract-trigger');
trigger.dispatchEvent(new Event('scroll'));
// Allow requestAnimationFrame to be invoked before continuing
await new Promise(resolve => setTimeout(resolve, 100));
}
it('should update :height after a resize event', async done => {
const rendered = findDOMNode(
render(
getMarkup({
height: 100,
width: 200,
}),
),
);
expect(rendered.textContent).toContain('height:100');
expect(rendered.textContent).toContain('width:200');
await simulateResize({element: rendered, height: 400, width: 300});
expect(rendered.textContent).toContain('height:400');
expect(rendered.textContent).toContain('width:300');
done();
});
describe('onResize and (re)render', () => {
it('should trigger when size changes', async done => {
const onResize = jest.fn();
const ChildComponent = jest
.fn()
.mockImplementation(DefaultChildComponent);
const rendered = findDOMNode(
render(
getMarkup({
ChildComponent,
height: 100,
onResize,
width: 200,
}),
),
);
ChildComponent.mockClear(); // TODO Improve initial check in version 10; see AutoSizer render()
expect(onResize).toHaveBeenCalledTimes(1);
await simulateResize({element: rendered, height: 400, width: 300});
expect(ChildComponent).toHaveBeenCalledTimes(1);
expect(onResize).toHaveBeenCalledTimes(2);
done();
});
it('should only trigger when height changes for disableWidth == true', async done => {
const onResize = jest.fn();
const ChildComponent = jest
.fn()
.mockImplementation(DefaultChildComponent);
const rendered = findDOMNode(
render(
getMarkup({
ChildComponent,
disableWidth: true,
height: 100,
onResize,
width: 200,
}),
),
);
ChildComponent.mockClear(); // TODO Improve initial check in version 10; see AutoSizer render()
expect(onResize).toHaveBeenCalledTimes(1);
await simulateResize({element: rendered, height: 100, width: 300});
expect(ChildComponent).toHaveBeenCalledTimes(0);
expect(onResize).toHaveBeenCalledTimes(1);
await simulateResize({element: rendered, height: 200, width: 300});
expect(ChildComponent).toHaveBeenCalledTimes(1);
expect(onResize).toHaveBeenCalledTimes(2);
done();
});
it('should only trigger when width changes for disableHeight == true', async done => {
const onResize = jest.fn();
const ChildComponent = jest
.fn()
.mockImplementation(DefaultChildComponent);
const rendered = findDOMNode(
render(
getMarkup({
ChildComponent,
disableHeight: true,
height: 100,
onResize,
width: 200,
}),
),
);
ChildComponent.mockClear(); // TODO Improve initial check in version 10; see AutoSizer render()
expect(onResize).toHaveBeenCalledTimes(1);
await simulateResize({element: rendered, height: 200, width: 200});
expect(ChildComponent).toHaveBeenCalledTimes(0);
expect(onResize).toHaveBeenCalledTimes(1);
await simulateResize({element: rendered, height: 200, width: 300});
expect(ChildComponent).toHaveBeenCalledTimes(1);
expect(onResize).toHaveBeenCalledTimes(2);
done();
});
});
describe('className and style', () => {
it('should use a custom :className if specified', () => {
const rendered = findDOMNode(render(getMarkup({className: 'foo'})));
expect(rendered.firstChild.className).toContain('foo');
});
it('should use a custom :style if specified', () => {
const style = {backgroundColor: 'red'};
const rendered = findDOMNode(render(getMarkup({style})));
expect(rendered.firstChild.style.backgroundColor).toEqual('red');
});
});
});