@@ -24,6 +24,7 @@ const html = `
24
24
describeWithDOM ( `Given the SizeMe library` , ( ) => {
25
25
let SizeMe ;
26
26
let resizeDetectorMock ;
27
+ const placeholderHtml = `<div style="width: 100%; height: 100%; position: relative;"></div>` ;
27
28
28
29
beforeEach ( ( ) => {
29
30
SizeMe = require ( `../src/index.js` ) . default ;
@@ -65,12 +66,8 @@ describeWithDOM(`Given the SizeMe library`, () => {
65
66
describe ( `And the resize detector` , ( ) => {
66
67
describe ( `When mounting and unmounting the placeholder component` , ( ) => {
67
68
it ( `Then the resizeDetector registration and deregistration should be called` , ( ) => {
68
- const refreshRate = 20000 ;
69
-
70
- const SizeAwareComponent = SizeMe ( { refreshRate } ) (
71
- function ( { size : { width, height } } ) {
72
- return < div > { width } x { height } </ div > ;
73
- }
69
+ const SizeAwareComponent = SizeMe ( ) (
70
+ ( { size : { width, height } } ) => < div > { width } x { height } </ div >
74
71
) ;
75
72
76
73
const mounted = mount ( < SizeAwareComponent /> ) ;
@@ -89,10 +86,8 @@ describeWithDOM(`Given the SizeMe library`, () => {
89
86
it ( `Then the resizeDetector registration and deregistration should be called` , ( done ) => {
90
87
const refreshRate = 30 ;
91
88
92
- const SizeAwareComponent = SizeMe ( { refreshRate } ) (
93
- function ( { size : { width, height } } ) {
94
- return < div > { width } x { height } </ div > ;
95
- }
89
+ const SizeAwareComponent = SizeMe ( ) (
90
+ ( { size : { width, height } } ) => < div > { width } x { height } </ div >
96
91
) ;
97
92
98
93
const mounted = mount ( < SizeAwareComponent /> ) ;
@@ -112,6 +107,7 @@ describeWithDOM(`Given the SizeMe library`, () => {
112
107
// Wait for the render callback
113
108
setTimeout ( ( ) => {
114
109
// Our actual component should have mounted now
110
+ expect ( mounted . text ( ) ) . to . equal ( `100 x 100` ) ;
115
111
expect ( resizeDetectorMock . listenTo . callCount ) . to . equal ( 2 ) ;
116
112
expect ( resizeDetectorMock . removeAllListeners . callCount ) . to . equal ( 1 ) ;
117
113
@@ -132,43 +128,47 @@ describeWithDOM(`Given the SizeMe library`, () => {
132
128
it ( `Then the resizeDetector should be called appropriately` , ( done ) => {
133
129
const refreshRate = 16 ;
134
130
135
- const SizeAwareComponent = SizeMe ( { refreshRate } ) (
136
- function ( { size : { width, height } } ) {
137
- return < div > { width } x { height } </ div > ;
138
- }
131
+ const SizeAwareComponent = SizeMe ( ) (
132
+ ( { size : { width, height } } ) => < div > { width } x { height } </ div >
139
133
) ;
140
134
141
- mount ( < SizeAwareComponent /> ) ;
135
+ const mounted = mount ( < SizeAwareComponent /> ) ;
142
136
143
- // Wait for the render callback
137
+ // The placeholder is mounted.
138
+ expect ( resizeDetectorMock . listenTo . callCount ) . to . equal ( 1 ) ;
139
+ expect ( resizeDetectorMock . removeAllListeners . callCount ) . to . equal ( 0 ) ;
140
+
141
+ // Get the callback for size changes
142
+ const checkIfSizeChangedCallback = resizeDetectorMock . listenTo . args [ 0 ] [ 1 ] ;
143
+ checkIfSizeChangedCallback ( {
144
+ getBoundingClientRect : ( ) => ( { width : 100 , height : 100 } )
145
+ } ) ;
146
+
147
+ // Ok we fired a fake dom update, now we need to wait for it to
148
+ // be processed.
144
149
setTimeout ( ( ) => {
145
- // Our actual component should have mounted now
146
- expect ( resizeDetectorMock . listenTo . callCount ) . to . equal ( 1 ) ;
147
- expect ( resizeDetectorMock . removeAllListeners . callCount ) . to . equal ( 0 ) ;
150
+ // Our actual component should be mounted.
151
+ expect ( mounted . text ( ) ) . to . equal ( `100 x 100` ) ;
152
+ expect ( resizeDetectorMock . listenTo . callCount ) . to . equal ( 2 ) ;
153
+ expect ( resizeDetectorMock . removeAllListeners . callCount ) . to . equal ( 1 ) ;
148
154
149
- // Get the callback for size changes
150
- const checkIfSizeChangedCallback = resizeDetectorMock . listenTo . args [ 0 ] [ 1 ] ;
155
+ // Fire another size change.
151
156
checkIfSizeChangedCallback ( {
152
- getBoundingClientRect : ( ) => ( {
153
- width : 100 ,
154
- height : 100
155
- } )
157
+ getBoundingClientRect : ( ) => ( { width : 200 , height : 200 } )
156
158
} ) ;
157
159
158
- // Ok we fired a fake dom update, now we need to wait for it to
159
- // be processed.
160
160
setTimeout ( ( ) => {
161
- expect ( resizeDetectorMock . listenTo . callCount ) . to . equal ( 2 ) ;
162
- expect ( resizeDetectorMock . removeAllListeners . callCount ) . to . equal ( 1 ) ;
161
+ expect ( mounted . text ( ) ) . to . equal ( `200 x 200` ) ;
162
+ expect ( resizeDetectorMock . listenTo . callCount ) . to . equal ( 3 ) ;
163
+ expect ( resizeDetectorMock . removeAllListeners . callCount ) . to . equal ( 2 ) ;
163
164
165
+ // Fire another size change, but with no change in size.
164
166
checkIfSizeChangedCallback ( {
165
- getBoundingClientRect : ( ) => ( {
166
- width : 200 ,
167
- height : 100
168
- } )
167
+ getBoundingClientRect : ( ) => ( { width : 200 , height : 200 } )
169
168
} ) ;
170
169
171
170
setTimeout ( ( ) => {
171
+ expect ( mounted . text ( ) ) . to . equal ( `200 x 200` ) ;
172
172
expect ( resizeDetectorMock . listenTo . callCount ) . to . equal ( 3 ) ;
173
173
expect ( resizeDetectorMock . removeAllListeners . callCount ) . to . equal ( 2 ) ;
174
174
@@ -184,93 +184,67 @@ describeWithDOM(`Given the SizeMe library`, () => {
184
184
describe ( `And no className or style has been provided` , ( ) => {
185
185
it ( `Then it should render the default placeholder` , ( ) => {
186
186
const SizeAwareComponent = SizeMe ( ) (
187
- function ( { size : { width, height } } ) {
188
- return < div > { width } x { height } </ div > ;
189
- }
187
+ ( { size : { width, height } } ) => < div > { width } x { height } </ div >
190
188
) ;
191
189
192
190
const mounted = mount ( < SizeAwareComponent /> ) ;
193
191
194
- const actual = mounted . html ( ) ;
195
- const expected = `<div style="width: 100%; height: 100%; position: relative;"></div>` ;
196
-
197
- expect ( actual ) . to . equal ( expected ) ;
192
+ expect ( mounted . html ( ) )
193
+ . to . equal ( placeholderHtml ) ;
198
194
} ) ;
199
195
} ) ;
200
196
201
197
describe ( `And only a className has been provided` , ( ) => {
202
198
it ( `Then it should render a placeholder with the className` , ( ) => {
203
199
const SizeAwareComponent = SizeMe ( ) (
204
- function ( { size : { width, height } } ) {
205
- return < div > { width } x { height } </ div > ;
206
- }
200
+ ( { size : { width, height } } ) => < div > { width } x { height } </ div >
207
201
) ;
208
202
209
203
const mounted = mount ( < SizeAwareComponent className = { `foo` } /> ) ;
210
204
211
- const actual = mounted . html ( ) ;
212
- const expected = `<div class="foo"></div>` ;
213
-
214
- expect ( actual ) . to . equal ( expected ) ;
205
+ expect ( mounted . html ( ) )
206
+ . to . equal ( `<div class="foo"></div>` ) ;
215
207
} ) ;
216
208
} ) ;
217
209
218
210
describe ( `And only a style has been provided` , ( ) => {
219
211
it ( `Then it should render a placeholder with the style` , ( ) => {
220
212
const SizeAwareComponent = SizeMe ( ) (
221
- function ( { size : { width, height } } ) {
222
- return < div > { width } x { height } </ div > ;
223
- }
213
+ ( { size : { width, height } } ) => < div > { width } x { height } </ div >
224
214
) ;
225
215
226
216
const mounted = mount ( < SizeAwareComponent style = { { height : `20px` } } /> ) ;
227
217
228
- const actual = mounted . html ( ) ;
229
- const expected = `<div style="height: 20px;"></div>` ;
230
-
231
- expect ( actual ) . to . equal ( expected ) ;
218
+ expect ( mounted . html ( ) )
219
+ . to . equal ( `<div style="height: 20px;"></div>` ) ;
232
220
} ) ;
233
221
} ) ;
234
222
235
223
describe ( `And a className and style have been provided` , ( ) => {
236
224
it ( `Then it should render a placeholder with both` , ( ) => {
237
225
const SizeAwareComponent = SizeMe ( ) (
238
- function ( { size : { width, height } } ) {
239
- return < div > { width } x { height } </ div > ;
240
- }
226
+ ( { size : { width, height } } ) => < div > { width } x { height } </ div >
241
227
) ;
242
228
243
229
const mounted = mount (
244
- < SizeAwareComponent
245
- style = { { height : `20px` } }
246
- className = { `foo` }
247
- />
230
+ < SizeAwareComponent style = { { height : `20px` } } className = { `foo` } />
248
231
) ;
249
232
250
- const actual = mounted . html ( ) ;
251
- const expected = `<div class="foo" style="height: 20px;"></div>` ;
252
-
253
- expect ( actual ) . to . equal ( expected ) ;
233
+ expect ( mounted . html ( ) )
234
+ . to . equal ( `<div class="foo" style="height: 20px;"></div>` ) ;
254
235
} ) ;
255
236
} ) ;
256
237
257
238
describe ( `And the size event has occurred` , ( ) => {
258
239
it ( `Then the actual size aware component should render` , ( done ) => {
259
240
const refreshRate = 30 ;
260
241
261
- const SizeAwareComponent = SizeMe ( {
262
- refreshRate
263
- } ) (
264
- function ( { size : { width, height } } ) {
265
- return < div > { width } x { height } </ div > ;
266
- }
242
+ const SizeAwareComponent = SizeMe ( { refreshRate } ) (
243
+ ( { size : { width, height } } ) => < div > { width } x { height } </ div >
267
244
) ;
268
245
269
246
const mounted = mount ( < SizeAwareComponent /> ) ;
270
247
271
- const placeholderHtml =
272
- `<div style="width: 100%; height: 100%; position: relative;"></div>` ;
273
-
274
248
// Initial render should be as expected.
275
249
expect ( mounted . html ( ) ) . to . equal ( placeholderHtml ) ;
276
250
@@ -279,25 +253,54 @@ describeWithDOM(`Given the SizeMe library`, () => {
279
253
expect ( mounted . html ( ) ) . to . equal ( placeholderHtml ) ;
280
254
} , refreshRate - 5 ) ;
281
255
256
+ // Get the callback for size changes.
257
+ const checkIfSizeChangedCallback = resizeDetectorMock . listenTo . args [ 0 ] [ 1 ] ;
258
+ checkIfSizeChangedCallback ( {
259
+ getBoundingClientRect : ( ) => ( { width : 100 , height : 100 } )
260
+ } ) ;
261
+
282
262
// Wait till refresh rate gets hit
283
263
setTimeout ( ( ) => {
284
- // Get the callback for size changes.
285
- const checkIfSizeChangedCallback = resizeDetectorMock . listenTo . args [ 0 ] [ 1 ] ;
286
- checkIfSizeChangedCallback ( {
287
- getBoundingClientRect : ( ) => ( {
288
- width : 100 ,
289
- height : 100
290
- } )
291
- } ) ;
264
+ // Update should have occurred by now.
265
+ expect ( mounted . text ( ) ) . to . equal ( `100 x 100` ) ;
292
266
293
- setTimeout ( ( ) => {
294
- // Update should have occurred by now.
295
- expect ( mounted . text ( ) ) . to . equal ( `100 x 100` ) ;
296
-
297
- done ( ) ;
298
- } , refreshRate ) ;
267
+ done ( ) ;
299
268
} , refreshRate + 5 ) ;
300
269
} ) ;
301
270
} ) ;
271
+
272
+ describe ( `And it receives new non-size props` , ( ) => {
273
+ it ( `Then the new props should be passed into the component` , ( done ) => {
274
+ const refreshRate = 16 ;
275
+
276
+ const SizeAwareComponent = SizeMe ( { refreshRate } ) (
277
+ function ( { size : { width, height } , otherProp } ) {
278
+ return < div > { width } x { height } & { otherProp } </ div > ;
279
+ }
280
+ ) ;
281
+
282
+ const mounted = mount ( < SizeAwareComponent otherProp = "foo" /> ) ;
283
+
284
+ // Get the callback for size changes.
285
+ const checkIfSizeChangedCallback = resizeDetectorMock . listenTo . args [ 0 ] [ 1 ] ;
286
+ checkIfSizeChangedCallback ( {
287
+ getBoundingClientRect : ( ) => ( { width : 100 , height : 100 } )
288
+ } ) ;
289
+
290
+ // Wait for refresh to hit.
291
+ setTimeout ( ( ) => {
292
+ // Output should contain foo.
293
+ expect ( mounted . text ( ) ) . to . equal ( `100 x 100 & foo` ) ;
294
+
295
+ // Update the other prop.
296
+ mounted . setProps ( { otherProp : `bar` } ) ;
297
+
298
+ // Output should contain foo.
299
+ expect ( mounted . text ( ) ) . to . equal ( `100 x 100 & bar` ) ;
300
+
301
+ done ( ) ;
302
+ } , refreshRate + 10 ) ;
303
+ } ) ;
304
+ } ) ;
302
305
} ) ;
303
306
} , html ) ;
0 commit comments