@@ -8,6 +8,9 @@ import { SearchParameters, SearchResults } from 'algoliasearch-helper';
8
8
import { createMultiSearchResponse } from '../../../../test/mock/createAPIResponse' ;
9
9
import { wait } from '../../../../test/utils/wait' ;
10
10
import { widgetSnapshotSerializer } from '../../../../test/utils/widgetSnapshotSerializer' ;
11
+ import refinementList from '../../refinement-list/refinement-list' ;
12
+ import { createSearchClient } from '../../../../test/mock/createSearchClient' ;
13
+ import instantsearch from '../../..' ;
11
14
12
15
expect . addSnapshotSerializer ( widgetSnapshotSerializer ) ;
13
16
@@ -37,6 +40,15 @@ describe('dynamicWidgets()', () => {
37
40
` ) ;
38
41
} ) ;
39
42
43
+ test ( 'widgets can be empty' , ( ) => {
44
+ expect ( ( ) =>
45
+ EXPERIMENTAL_dynamicWidgets ( {
46
+ container : document . createElement ( 'div' ) ,
47
+ widgets : [ ] ,
48
+ } )
49
+ ) . not . toThrowError ( ) ;
50
+ } ) ;
51
+
40
52
test ( 'widgets is required to be callbacks' , ( ) => {
41
53
expect ( ( ) =>
42
54
EXPERIMENTAL_dynamicWidgets ( {
@@ -199,10 +211,13 @@ describe('dynamicWidgets()', () => {
199
211
} ) ;
200
212
201
213
it ( 'renders the widgets returned by transformItems' , async ( ) => {
202
- const instantSearchInstance = createInstantSearch ( ) ;
214
+ const instantSearchInstance = instantsearch ( {
215
+ indexName : '' ,
216
+ searchClient : createSearchClient ( ) ,
217
+ } ) ;
203
218
const rootContainer = document . createElement ( 'div' ) ;
204
219
205
- const indexWidget = index ( { indexName : 'test' } ) . addWidgets ( [
220
+ instantSearchInstance . addWidgets ( [
206
221
EXPERIMENTAL_dynamicWidgets ( {
207
222
container : rootContainer ,
208
223
transformItems ( ) {
@@ -237,18 +252,7 @@ describe('dynamicWidgets()', () => {
237
252
} ) ,
238
253
] ) ;
239
254
240
- indexWidget . init ( createInitOptions ( { instantSearchInstance } ) ) ;
241
-
242
- // set results to the relevant index, so it renders all children
243
- instantSearchInstance . mainHelper ! . derivedHelpers [ 0 ] . lastResults =
244
- new SearchResults (
245
- indexWidget . getWidgetSearchParameters ( new SearchParameters ( ) , {
246
- uiState : { } ,
247
- } ) ,
248
- createMultiSearchResponse ( { } ) . results
249
- ) ;
250
-
251
- indexWidget . render ( createRenderOptions ( { instantSearchInstance } ) ) ;
255
+ instantSearchInstance . start ( ) ;
252
256
253
257
await wait ( 0 ) ;
254
258
@@ -279,14 +283,20 @@ describe('dynamicWidgets()', () => {
279
283
} ) ;
280
284
281
285
it ( 'updates the position of widgets returned by transformItems' , async ( ) => {
282
- const instantSearchInstance = createInstantSearch ( ) ;
286
+ const instantSearchInstance = instantsearch ( {
287
+ indexName : '' ,
288
+ searchClient : createSearchClient ( ) ,
289
+ } ) ;
290
+ instantSearchInstance . start ( ) ;
283
291
const rootContainer = document . createElement ( 'div' ) ;
284
292
285
- const indexWidget = index ( { indexName : 'test' } ) . addWidgets ( [
293
+ let ordering = [ 'test1' , 'test4' ] ;
294
+
295
+ instantSearchInstance . addWidgets ( [
286
296
EXPERIMENTAL_dynamicWidgets ( {
287
297
container : rootContainer ,
288
- transformItems ( _items , { results } ) {
289
- return results . userData [ 0 ] . MOCK_facetOrder ;
298
+ transformItems ( ) {
299
+ return ordering ;
290
300
} ,
291
301
widgets : [
292
302
( container ) =>
@@ -317,21 +327,6 @@ describe('dynamicWidgets()', () => {
317
327
} ) ,
318
328
] ) ;
319
329
320
- indexWidget . init ( createInitOptions ( { instantSearchInstance } ) ) ;
321
-
322
- // set results to the relevant index, so it renders all children
323
- instantSearchInstance . mainHelper ! . derivedHelpers [ 0 ] . lastResults =
324
- new SearchResults (
325
- indexWidget . getWidgetSearchParameters ( new SearchParameters ( ) , {
326
- uiState : { } ,
327
- } ) ,
328
- createMultiSearchResponse ( {
329
- userData : [ { MOCK_facetOrder : [ 'test1' , 'test4' ] } ] ,
330
- } ) . results
331
- ) ;
332
-
333
- indexWidget . render ( createRenderOptions ( { instantSearchInstance } ) ) ;
334
-
335
330
await wait ( 0 ) ;
336
331
337
332
expect ( rootContainer ) . toMatchInlineSnapshot ( `
@@ -363,18 +358,10 @@ describe('dynamicWidgets()', () => {
363
358
</div>
364
359
` ) ;
365
360
366
- // set results to the relevant index, so it renders all children
367
- instantSearchInstance . mainHelper ! . derivedHelpers [ 0 ] . lastResults =
368
- new SearchResults (
369
- indexWidget . getWidgetSearchParameters ( new SearchParameters ( ) , {
370
- uiState : { } ,
371
- } ) ,
372
- createMultiSearchResponse ( {
373
- userData : [ { MOCK_facetOrder : [ 'test4' , 'test1' ] } ] ,
374
- } ) . results
375
- ) ;
361
+ ordering = [ 'test4' , 'test1' ] ;
376
362
377
- indexWidget . render ( createRenderOptions ( { instantSearchInstance } ) ) ;
363
+ instantSearchInstance . scheduleRender ( ) ;
364
+ await wait ( 0 ) ;
378
365
379
366
expect ( rootContainer ) . toMatchInlineSnapshot ( `
380
367
<div>
@@ -416,6 +403,12 @@ describe('dynamicWidgets()', () => {
416
403
transformItems ( _items , { results } ) {
417
404
return results . userData [ 0 ] . MOCK_facetOrder ;
418
405
} ,
406
+ fallbackWidget : ( { container, attribute } ) =>
407
+ refinementList ( {
408
+ attribute,
409
+ container,
410
+ cssClasses : { root : attribute } ,
411
+ } ) ,
419
412
widgets : [
420
413
( container ) =>
421
414
menu ( {
@@ -454,7 +447,7 @@ describe('dynamicWidgets()', () => {
454
447
uiState : { } ,
455
448
} ) ,
456
449
createMultiSearchResponse ( {
457
- userData : [ { MOCK_facetOrder : [ 'test1' , 'test4' ] } ] ,
450
+ userData : [ { MOCK_facetOrder : [ 'test1' , 'test4' , 'test5' ] } ] ,
458
451
} ) . results
459
452
) ;
460
453
@@ -475,6 +468,10 @@ describe('dynamicWidgets()', () => {
475
468
$$widgetType: ais.menu
476
469
attribute: test4
477
470
},
471
+ Widget(ais.refinementList) {
472
+ $$widgetType: ais.refinementList
473
+ attribute: test5
474
+ },
478
475
]
479
476
` ) ;
480
477
@@ -486,58 +483,53 @@ describe('dynamicWidgets()', () => {
486
483
} ) ;
487
484
488
485
it ( 'removes dom on dispose' , async ( ) => {
489
- const instantSearchInstance = createInstantSearch ( ) ;
486
+ const instantSearchInstance = instantsearch ( {
487
+ indexName : '' ,
488
+ searchClient : createSearchClient ( ) ,
489
+ } ) ;
490
+ instantSearchInstance . start ( ) ;
490
491
const rootContainer = document . createElement ( 'div' ) ;
491
492
492
- const indexWidget = index ( { indexName : 'test' } ) . addWidgets ( [
493
- EXPERIMENTAL_dynamicWidgets ( {
494
- container : rootContainer ,
495
- transformItems ( _items , { results } ) {
496
- return results . userData [ 0 ] . MOCK_facetOrder ;
497
- } ,
498
- widgets : [
499
- ( container ) =>
500
- menu ( {
501
- attribute : 'test1' ,
502
- container,
503
- cssClasses : { root : 'test1' } ,
504
- } ) ,
505
- ( container ) =>
506
- menu ( {
507
- attribute : 'test2' ,
508
- container,
509
- cssClasses : { root : 'test2' } ,
510
- } ) ,
511
- ( container ) =>
512
- menu ( {
513
- attribute : 'test3' ,
514
- container,
515
- cssClasses : { root : 'test3' } ,
516
- } ) ,
517
- ( container ) =>
518
- menu ( {
519
- attribute : 'test4' ,
520
- container,
521
- cssClasses : { root : 'test4' } ,
522
- } ) ,
523
- ] ,
524
- } ) ,
525
- ] ) ;
526
-
527
- indexWidget . init ( createInitOptions ( { instantSearchInstance } ) ) ;
528
-
529
- // set results to the relevant index, so it renders all children
530
- instantSearchInstance . mainHelper ! . derivedHelpers [ 0 ] . lastResults =
531
- new SearchResults (
532
- indexWidget . getWidgetSearchParameters ( new SearchParameters ( ) , {
533
- uiState : { } ,
493
+ const dynamicWidget = EXPERIMENTAL_dynamicWidgets ( {
494
+ container : rootContainer ,
495
+ transformItems ( ) {
496
+ return [ 'test1' , 'test5' , 'test4' ] ;
497
+ } ,
498
+ fallbackWidget : ( { container, attribute } ) =>
499
+ refinementList ( {
500
+ attribute,
501
+ container,
502
+ cssClasses : { root : attribute } ,
534
503
} ) ,
535
- createMultiSearchResponse ( {
536
- userData : [ { MOCK_facetOrder : [ 'test1' , 'test4' ] } ] ,
537
- } ) . results
538
- ) ;
504
+ widgets : [
505
+ ( container ) =>
506
+ menu ( {
507
+ attribute : 'test1' ,
508
+ container,
509
+ cssClasses : { root : 'test1' } ,
510
+ } ) ,
511
+ ( container ) =>
512
+ menu ( {
513
+ attribute : 'test2' ,
514
+ container,
515
+ cssClasses : { root : 'test2' } ,
516
+ } ) ,
517
+ ( container ) =>
518
+ menu ( {
519
+ attribute : 'test3' ,
520
+ container,
521
+ cssClasses : { root : 'test3' } ,
522
+ } ) ,
523
+ ( container ) =>
524
+ menu ( {
525
+ attribute : 'test4' ,
526
+ container,
527
+ cssClasses : { root : 'test4' } ,
528
+ } ) ,
529
+ ] ,
530
+ } ) ;
539
531
540
- indexWidget . render ( createRenderOptions ( { instantSearchInstance } ) ) ;
532
+ instantSearchInstance . addWidgets ( [ dynamicWidget ] ) ;
541
533
542
534
await wait ( 0 ) ;
543
535
@@ -559,6 +551,13 @@ describe('dynamicWidgets()', () => {
559
551
class="ais-Menu test1 ais-Menu--noRefinement"
560
552
/>
561
553
</div>
554
+ <div
555
+ class="ais-DynamicWidgets-widget"
556
+ >
557
+ <div
558
+ class="ais-RefinementList test5 ais-RefinementList--noRefinement"
559
+ />
560
+ </div>
562
561
<div
563
562
class="ais-DynamicWidgets-widget"
564
563
>
@@ -570,9 +569,7 @@ describe('dynamicWidgets()', () => {
570
569
</div>
571
570
` ) ;
572
571
573
- const dynamicWidget = indexWidget . getWidgets ( ) [ 0 ] ;
574
-
575
- indexWidget . removeWidgets ( [ dynamicWidget ] ) ;
572
+ instantSearchInstance . removeWidgets ( [ dynamicWidget ] ) ;
576
573
577
574
expect ( rootContainer ) . toMatchInlineSnapshot ( `<div />` ) ;
578
575
} ) ;
0 commit comments