@@ -114,10 +114,12 @@ describe('Templates', () => {
114
114
test ( 'with default templates' , ( ) => {
115
115
const widgetWithPanel = panel ( ) ( widgetFactory ) ;
116
116
117
- widgetWithPanel ( {
117
+ const widget = widgetWithPanel ( {
118
118
container : document . createElement ( 'div' ) ,
119
119
} ) ;
120
120
121
+ widget . init ( createInitOptions ( ) ) ;
122
+
121
123
const firstRender = render . mock . calls [ 0 ] [ 0 ] as VNode <
122
124
PanelProps < typeof widgetFactory >
123
125
> ;
@@ -137,10 +139,12 @@ describe('Templates', () => {
137
139
} ,
138
140
} ) ( widgetFactory ) ;
139
141
140
- widgetWithPanel ( {
142
+ const widget = widgetWithPanel ( {
141
143
container : document . createElement ( 'div' ) ,
142
144
} ) ;
143
145
146
+ widget . init ( createInitOptions ( ) ) ;
147
+
144
148
const firstRender = render . mock . calls [ 0 ] [ 0 ] as VNode <
145
149
PanelProps < typeof widgetFactory >
146
150
> ;
@@ -156,10 +160,12 @@ describe('Templates', () => {
156
160
} ,
157
161
} ) ( widgetFactory ) ;
158
162
159
- widgetWithPanel ( {
163
+ const widget = widgetWithPanel ( {
160
164
container : document . createElement ( 'div' ) ,
161
165
} ) ;
162
166
167
+ widget . init ( createInitOptions ( ) ) ;
168
+
163
169
const firstRender = render . mock . calls [ 0 ] [ 0 ] as VNode <
164
170
PanelProps < typeof widgetFactory >
165
171
> ;
@@ -175,10 +181,12 @@ describe('Templates', () => {
175
181
} ,
176
182
} ) ( widgetFactory ) ;
177
183
178
- widgetWithPanel ( {
184
+ const widget = widgetWithPanel ( {
179
185
container : document . createElement ( 'div' ) ,
180
186
} ) ;
181
187
188
+ widget . init ( createInitOptions ( ) ) ;
189
+
182
190
const firstRender = render . mock . calls [ 0 ] [ 0 ] as VNode <
183
191
PanelProps < typeof widgetFactory >
184
192
> ;
@@ -206,32 +214,235 @@ describe('Lifecycle', () => {
206
214
container : document . createElement ( 'div' ) ,
207
215
} ) ;
208
216
209
- widgetWithPanel . init ! ( createInitOptions ( ) ) ;
210
- widgetWithPanel . render ! ( createRenderOptions ( ) ) ;
211
- widgetWithPanel . dispose ! ( createDisposeOptions ( ) ) ;
217
+ widgetWithPanel . init ( createInitOptions ( ) ) ;
218
+ widgetWithPanel . render ( createRenderOptions ( ) ) ;
219
+ widgetWithPanel . dispose ( createDisposeOptions ( ) ) ;
212
220
213
221
expect ( widget . init ) . toHaveBeenCalledTimes ( 1 ) ;
214
222
expect ( widget . render ) . toHaveBeenCalledTimes ( 1 ) ;
215
223
expect ( widget . dispose ) . toHaveBeenCalledTimes ( 1 ) ;
216
224
} ) ;
217
225
218
- test ( 'returns the `state` from the widget dispose function' , ( ) => {
219
- const nextSearchParameters = new algoliasearchHelper . SearchParameters ( {
220
- facets : [ 'brands' ] ,
226
+ describe ( 'init' , ( ) => {
227
+ test ( "calls the wrapped widget's init" , ( ) => {
228
+ const widget = {
229
+ $$type : 'mock.widget' ,
230
+ init : jest . fn ( ) ,
231
+ } ;
232
+ const widgetFactory = ( ) => widget ;
233
+
234
+ const widgetWithPanel = panel ( ) ( widgetFactory ) ( {
235
+ container : document . createElement ( 'div' ) ,
236
+ } ) ;
237
+
238
+ const initOptions = createInitOptions ( ) ;
239
+
240
+ widgetWithPanel . init ( initOptions ) ;
241
+
242
+ expect ( widget . init ) . toHaveBeenCalledTimes ( 1 ) ;
243
+ expect ( widget . init ) . toHaveBeenCalledWith ( initOptions ) ;
221
244
} ) ;
222
- const widget = {
223
- $$type : 'mock.widget' ,
224
- init : jest . fn ( ) ,
225
- dispose : jest . fn ( ( ) => nextSearchParameters ) ,
226
- } ;
227
- const widgetFactory = ( ) => widget ;
228
245
229
- const widgetWithPanel = panel ( ) ( widgetFactory ) ( {
230
- container : document . createElement ( 'div' ) ,
246
+ test ( 'does not call hidden and collapsed yet' , ( ) => {
247
+ const renderState = {
248
+ widgetParams : { } ,
249
+ swag : true ,
250
+ } ;
251
+
252
+ const widget = {
253
+ $$type : 'mock.widget' ,
254
+ render : jest . fn ( ) ,
255
+ getWidgetRenderState ( ) {
256
+ return renderState ;
257
+ } ,
258
+ } ;
259
+
260
+ const widgetFactory = ( ) => widget ;
261
+
262
+ const hiddenFn = jest . fn ( ) ;
263
+ const collapsedFn = jest . fn ( ) ;
264
+
265
+ const widgetWithPanel = panel ( {
266
+ hidden : hiddenFn ,
267
+ collapsed : collapsedFn ,
268
+ } ) ( widgetFactory ) ( {
269
+ container : document . createElement ( 'div' ) ,
270
+ } ) ;
271
+
272
+ const initOptions = createInitOptions ( ) ;
273
+
274
+ widgetWithPanel . init ( initOptions ) ;
275
+
276
+ expect ( hiddenFn ) . toHaveBeenCalledTimes ( 0 ) ;
277
+ expect ( collapsedFn ) . toHaveBeenCalledTimes ( 0 ) ;
278
+ } ) ;
279
+
280
+ test ( 'renders with render state' , ( ) => {
281
+ const renderState = {
282
+ widgetParams : { } ,
283
+ swag : true ,
284
+ } ;
285
+
286
+ const widget = {
287
+ $$type : 'mock.widget' ,
288
+ render : jest . fn ( ) ,
289
+ getWidgetRenderState ( ) {
290
+ return renderState ;
291
+ } ,
292
+ } ;
293
+
294
+ const widgetFactory = ( ) => widget ;
295
+
296
+ const widgetWithPanel = panel ( ) ( widgetFactory ) ( {
297
+ container : document . createElement ( 'div' ) ,
298
+ } ) ;
299
+
300
+ const initOptions = createInitOptions ( ) ;
301
+
302
+ widgetWithPanel . init ( initOptions ) ;
303
+
304
+ const firstRender = render . mock . calls [ 0 ] [ 0 ] as VNode <
305
+ PanelProps < typeof widgetFactory >
306
+ > ;
307
+
308
+ expect ( firstRender . props ) . toEqual (
309
+ expect . objectContaining ( {
310
+ hidden : true ,
311
+ collapsible : false ,
312
+ isCollapsed : false ,
313
+ data : {
314
+ ...renderState ,
315
+ ...initOptions ,
316
+ } ,
317
+ } )
318
+ ) ;
319
+ } ) ;
320
+ } ) ;
321
+
322
+ describe ( 'render' , ( ) => {
323
+ test ( "calls the wrapped widget's render" , ( ) => {
324
+ const widget = {
325
+ $$type : 'mock.widget' ,
326
+ render : jest . fn ( ) ,
327
+ } ;
328
+ const widgetFactory = ( ) => widget ;
329
+
330
+ const widgetWithPanel = panel ( ) ( widgetFactory ) ( {
331
+ container : document . createElement ( 'div' ) ,
332
+ } ) ;
333
+
334
+ const renderOptions = createRenderOptions ( ) ;
335
+
336
+ widgetWithPanel . render ( renderOptions ) ;
337
+
338
+ expect ( widget . render ) . toHaveBeenCalledTimes ( 1 ) ;
339
+ expect ( widget . render ) . toHaveBeenCalledWith ( renderOptions ) ;
340
+ } ) ;
341
+
342
+ test ( "calls hidden and collapsed with the wrapped widget's render state" , ( ) => {
343
+ const renderState = {
344
+ widgetParams : { } ,
345
+ swag : true ,
346
+ } ;
347
+
348
+ const widget = {
349
+ $$type : 'mock.widget' ,
350
+ render : jest . fn ( ) ,
351
+ getWidgetRenderState ( ) {
352
+ return renderState ;
353
+ } ,
354
+ } ;
355
+
356
+ const widgetFactory = ( ) => widget ;
357
+
358
+ const hiddenFn = jest . fn ( ) ;
359
+ const collapsedFn = jest . fn ( ) ;
360
+
361
+ const widgetWithPanel = panel ( {
362
+ hidden : hiddenFn ,
363
+ collapsed : collapsedFn ,
364
+ } ) ( widgetFactory ) ( {
365
+ container : document . createElement ( 'div' ) ,
366
+ } ) ;
367
+
368
+ const renderOptions = createRenderOptions ( ) ;
369
+
370
+ widgetWithPanel . render ( renderOptions ) ;
371
+
372
+ expect ( hiddenFn ) . toHaveBeenCalledTimes ( 1 ) ;
373
+ expect ( hiddenFn ) . toHaveBeenCalledWith ( {
374
+ ...renderState ,
375
+ ...renderOptions ,
376
+ } ) ;
377
+
378
+ expect ( collapsedFn ) . toHaveBeenCalledTimes ( 1 ) ;
379
+ expect ( collapsedFn ) . toHaveBeenCalledWith ( {
380
+ ...renderState ,
381
+ ...renderOptions ,
382
+ } ) ;
383
+ } ) ;
384
+
385
+ test ( 'renders with render state' , ( ) => {
386
+ const renderState = {
387
+ widgetParams : { } ,
388
+ swag : true ,
389
+ } ;
390
+
391
+ const widget = {
392
+ $$type : 'mock.widget' ,
393
+ render : jest . fn ( ) ,
394
+ getWidgetRenderState ( ) {
395
+ return renderState ;
396
+ } ,
397
+ } ;
398
+
399
+ const widgetFactory = ( ) => widget ;
400
+
401
+ const widgetWithPanel = panel ( ) ( widgetFactory ) ( {
402
+ container : document . createElement ( 'div' ) ,
403
+ } ) ;
404
+
405
+ const renderOptions = createRenderOptions ( ) ;
406
+
407
+ widgetWithPanel . render ( renderOptions ) ;
408
+
409
+ const firstRender = render . mock . calls [ 0 ] [ 0 ] as VNode <
410
+ PanelProps < typeof widgetFactory >
411
+ > ;
412
+
413
+ expect ( firstRender . props ) . toEqual (
414
+ expect . objectContaining ( {
415
+ hidden : false ,
416
+ collapsible : false ,
417
+ isCollapsed : false ,
418
+ data : {
419
+ ...renderState ,
420
+ ...renderOptions ,
421
+ } ,
422
+ } )
423
+ ) ;
231
424
} ) ;
425
+ } ) ;
232
426
233
- const nextState = widgetWithPanel . dispose ! ( createDisposeOptions ( { } ) ) ;
427
+ describe ( 'dispose' , ( ) => {
428
+ test ( "returns the state from the widget's dispose function" , ( ) => {
429
+ const nextSearchParameters = new algoliasearchHelper . SearchParameters ( {
430
+ facets : [ 'brands' ] ,
431
+ } ) ;
432
+ const widget = {
433
+ $$type : 'mock.widget' ,
434
+ init : jest . fn ( ) ,
435
+ dispose : jest . fn ( ( ) => nextSearchParameters ) ,
436
+ } ;
437
+ const widgetFactory = ( ) => widget ;
438
+
439
+ const widgetWithPanel = panel ( ) ( widgetFactory ) ( {
440
+ container : document . createElement ( 'div' ) ,
441
+ } ) ;
442
+
443
+ const nextState = widgetWithPanel . dispose ( createDisposeOptions ( ) ) ;
234
444
235
- expect ( nextState ) . toEqual ( nextSearchParameters ) ;
445
+ expect ( nextState ) . toEqual ( nextSearchParameters ) ;
446
+ } ) ;
236
447
} ) ;
237
448
} ) ;
0 commit comments