@@ -8,8 +8,12 @@ import {
8
8
} from '../../../../test/mock/createWidget' ;
9
9
import { wait } from '../../../../test/utils/wait' ;
10
10
import { SearchParameters , SearchResults } from 'algoliasearch-helper' ;
11
- import { createMultiSearchResponse } from '../../../../test/mock/createAPIResponse' ;
11
+ import {
12
+ createMultiSearchResponse ,
13
+ createSingleSearchResponse ,
14
+ } from '../../../../test/mock/createAPIResponse' ;
12
15
import connectHierarchicalMenu from '../../hierarchical-menu/connectHierarchicalMenu' ;
16
+ import { DynamicWidgetsConnectorParams } from '../connectDynamicWidgets' ;
13
17
14
18
expect . addSnapshotSerializer ( widgetSnapshotSerializer ) ;
15
19
@@ -38,30 +42,6 @@ describe('connectDynamicWidgets', () => {
38
42
See documentation: https://www.algolia.com/doc/api-reference/widgets/dynamic-widgets/js/#connector"
39
43
` ) ;
40
44
} ) ;
41
-
42
- it ( 'correct usage' , ( ) => {
43
- expect ( ( ) =>
44
- // @ts -expect-error
45
- EXPERIMENTAL_connectDynamicWidgets ( ( ) => { } ) ( {
46
- widgets : [ ] ,
47
- } )
48
- ) . toThrowErrorMatchingInlineSnapshot ( `
49
- "the \`transformItems\` option is required to be a function.
50
-
51
- See documentation: https://www.algolia.com/doc/api-reference/widgets/dynamic-widgets/js/#connector"
52
- ` ) ;
53
- } ) ;
54
-
55
- it ( 'transformItems' , ( ) => {
56
- expect ( ( ) =>
57
- EXPERIMENTAL_connectDynamicWidgets ( ( ) => { } ) ( {
58
- widgets : [ ] ,
59
- transformItems ( items , { results } ) {
60
- return items . map ( item => item + results . nbHits ) ;
61
- } ,
62
- } )
63
- ) . not . toThrow ( ) ;
64
- } ) ;
65
45
} ) ;
66
46
67
47
describe ( 'init' , ( ) => {
@@ -552,6 +532,37 @@ describe('connectDynamicWidgets', () => {
552
532
} ) ;
553
533
} ) ;
554
534
535
+ it ( 'returns widgetParams and attributesToRender (with results)' , ( ) => {
536
+ const widgetParams = {
537
+ widgets : [
538
+ connectMenu ( ( ) => { } ) ( { attribute : 'test1' } ) ,
539
+ connectHierarchicalMenu ( ( ) => { } ) ( { attributes : [ 'test2' , 'test3' ] } ) ,
540
+ ] ,
541
+ } ;
542
+ const dynamicWidgets = EXPERIMENTAL_connectDynamicWidgets ( ( ) => { } ) (
543
+ widgetParams
544
+ ) ;
545
+
546
+ expect (
547
+ dynamicWidgets . getWidgetRenderState (
548
+ createRenderOptions ( {
549
+ results : new SearchResults ( new SearchParameters ( ) , [
550
+ createSingleSearchResponse ( {
551
+ renderingContent : {
552
+ facetOrdering : {
553
+ facet : { order : [ 'test1' , 'test2' ] } ,
554
+ } ,
555
+ } ,
556
+ } ) ,
557
+ ] ) ,
558
+ } )
559
+ )
560
+ ) . toEqual ( {
561
+ attributesToRender : [ 'test1' , 'test2' ] ,
562
+ widgetParams,
563
+ } ) ;
564
+ } ) ;
565
+
555
566
it ( 'returns widgetParams and the result of transformItems render' , ( ) => {
556
567
const widgetParams = {
557
568
transformItems ( ) {
@@ -573,6 +584,42 @@ describe('connectDynamicWidgets', () => {
573
584
widgetParams,
574
585
} ) ;
575
586
} ) ;
587
+
588
+ it ( 'returns widgetParams and the result of transformItems render (using result)' , ( ) => {
589
+ const widgetParams : DynamicWidgetsConnectorParams = {
590
+ transformItems ( items ) {
591
+ return items . sort ( ( a , b ) => b . localeCompare ( a ) ) ;
592
+ } ,
593
+ widgets : [
594
+ connectMenu ( ( ) => { } ) ( { attribute : 'test1' } ) ,
595
+ connectHierarchicalMenu ( ( ) => { } ) ( { attributes : [ 'test2' , 'test3' ] } ) ,
596
+ ] ,
597
+ } ;
598
+ const dynamicWidgets = EXPERIMENTAL_connectDynamicWidgets ( ( ) => { } ) (
599
+ widgetParams
600
+ ) ;
601
+
602
+ expect (
603
+ dynamicWidgets . getWidgetRenderState (
604
+ createRenderOptions (
605
+ createRenderOptions ( {
606
+ results : new SearchResults ( new SearchParameters ( ) , [
607
+ createSingleSearchResponse ( {
608
+ renderingContent : {
609
+ facetOrdering : {
610
+ facet : { order : [ 'test1' , 'test2' ] } ,
611
+ } ,
612
+ } ,
613
+ } ) ,
614
+ ] ) ,
615
+ } )
616
+ )
617
+ )
618
+ ) . toEqual ( {
619
+ attributesToRender : [ 'test2' , 'test1' ] ,
620
+ widgetParams,
621
+ } ) ;
622
+ } ) ;
576
623
} ) ;
577
624
578
625
describe ( 'getRenderState' , ( ) => {
0 commit comments