@@ -4,7 +4,7 @@ import qs from 'qs';
4
4
import { createSearchClient } from '../../../test/mock/createSearchClient' ;
5
5
import { createWidget } from '../../../test/mock/createWidget' ;
6
6
import { runAllMicroTasks } from '../../../test/utils/runAllMicroTasks' ;
7
- import { Router , Widget , StateMapping , RouteState } from '../../types' ;
7
+ import { Router , Widget , UiState , StateMapping , RouteState } from '../../types' ;
8
8
import historyRouter from '../routers/history' ;
9
9
import RoutingManager from '../RoutingManager' ;
10
10
import instantsearch from '../main' ;
@@ -119,7 +119,7 @@ describe('RoutingManager', () => {
119
119
test ( 'reads the state of widgets with a getWidgetState implementation' , ( ) => {
120
120
const searchClient = createSearchClient ( ) ;
121
121
const search = instantsearch ( {
122
- indexName : '' ,
122
+ indexName : 'indexName ' ,
123
123
searchClient,
124
124
} ) ;
125
125
@@ -135,7 +135,9 @@ describe('RoutingManager', () => {
135
135
search . addWidget ( widget ) ;
136
136
137
137
const actualInitialState = {
138
- query : 'query' ,
138
+ indexName : {
139
+ query : 'query' ,
140
+ } ,
139
141
} ;
140
142
141
143
search . start ( ) ;
@@ -156,7 +158,9 @@ describe('RoutingManager', () => {
156
158
searchParameters : search . mainIndex . getHelper ( ) ! . state ,
157
159
} ) ;
158
160
159
- expect ( uiStates ) . toEqual ( widgetState ) ;
161
+ expect ( uiStates ) . toEqual ( {
162
+ indexName : widgetState ,
163
+ } ) ;
160
164
161
165
expect ( widget . getWidgetState ) . toHaveBeenCalledTimes ( 1 ) ;
162
166
expect ( widget . getWidgetState ) . toHaveBeenCalledWith (
@@ -171,7 +175,7 @@ describe('RoutingManager', () => {
171
175
test ( 'Does not read UI state from widgets without an implementation of getWidgetState' , ( ) => {
172
176
const searchClient = createSearchClient ( ) ;
173
177
const search = instantsearch ( {
174
- indexName : '' ,
178
+ indexName : 'indexName ' ,
175
179
searchClient,
176
180
} ) ;
177
181
@@ -182,7 +186,9 @@ describe('RoutingManager', () => {
182
186
search . start ( ) ;
183
187
184
188
const actualInitialState = {
185
- query : 'query' ,
189
+ indexName : {
190
+ query : 'query' ,
191
+ } ,
186
192
} ;
187
193
188
194
const router = new RoutingManager ( {
@@ -201,15 +207,17 @@ describe('RoutingManager', () => {
201
207
searchParameters : search . mainIndex . getHelper ( ) ! . state ,
202
208
} ) ;
203
209
204
- expect ( uiStates ) . toEqual ( { } ) ;
210
+ expect ( uiStates ) . toEqual ( {
211
+ indexName : { } ,
212
+ } ) ;
205
213
} ) ;
206
214
} ) ;
207
215
208
216
describe ( 'getAllSearchParameters' , ( ) => {
209
217
test ( 'should get searchParameters from widget that implements getWidgetSearchParameters' , ( ) => {
210
218
const searchClient = createSearchClient ( ) ;
211
219
const search = instantsearch ( {
212
- indexName : '' ,
220
+ indexName : 'indexName ' ,
213
221
searchClient,
214
222
} ) ;
215
223
@@ -221,7 +229,9 @@ describe('RoutingManager', () => {
221
229
search . addWidget ( widget ) ;
222
230
223
231
const actualInitialState = {
224
- query : 'query' ,
232
+ indexName : {
233
+ query : 'query' ,
234
+ } ,
225
235
} ;
226
236
227
237
search . start ( ) ;
@@ -299,7 +309,7 @@ describe('RoutingManager', () => {
299
309
} ) ;
300
310
301
311
const search = instantsearch ( {
302
- indexName : 'instant_search ' ,
312
+ indexName : 'indexName ' ,
303
313
searchClient,
304
314
routing : {
305
315
router,
@@ -330,7 +340,9 @@ describe('RoutingManager', () => {
330
340
331
341
expect ( router . write ) . toHaveBeenCalledTimes ( 1 ) ;
332
342
expect ( router . write ) . toHaveBeenCalledWith ( {
333
- q : 'q' ,
343
+ indexName : {
344
+ q : 'q' ,
345
+ } ,
334
346
} ) ;
335
347
336
348
done ( ) ;
@@ -340,15 +352,15 @@ describe('RoutingManager', () => {
340
352
test ( 'should update the searchParameters on router state update' , done => {
341
353
const searchClient = createSearchClient ( ) ;
342
354
343
- let onRouterUpdateCallback : ( args : object ) => void ;
355
+ let onRouterUpdateCallback : ( args : UiState ) => void ;
344
356
const router = createFakeRouter ( {
345
357
onUpdate : fn => {
346
358
onRouterUpdateCallback = fn ;
347
359
} ,
348
360
} ) ;
349
361
350
362
const search = instantsearch ( {
351
- indexName : 'instant_search ' ,
363
+ indexName : 'indexName ' ,
352
364
searchClient,
353
365
routing : {
354
366
router,
@@ -358,9 +370,10 @@ describe('RoutingManager', () => {
358
370
const widget = {
359
371
render : jest . fn ( ) ,
360
372
getWidgetSearchParameters : jest . fn ( ( searchParameters , { uiState } ) =>
361
- searchParameters . setQuery ( uiState . q )
373
+ searchParameters . setQuery ( uiState . query )
362
374
) ,
363
375
} ;
376
+
364
377
search . addWidget ( widget ) ;
365
378
366
379
search . start ( ) ;
@@ -370,9 +383,11 @@ describe('RoutingManager', () => {
370
383
371
384
expect ( search . mainIndex . getHelper ( ) ! . state . query ) . toBeUndefined ( ) ;
372
385
373
- // this simulates a router update with a uiState of {q : 'a'}
386
+ // this simulates a router update with a uiState of {query : 'a'}
374
387
onRouterUpdateCallback ( {
375
- q : 'a' ,
388
+ indexName : {
389
+ query : 'a' ,
390
+ } ,
376
391
} ) ;
377
392
378
393
search . once ( 'render' , ( ) => {
@@ -393,14 +408,21 @@ describe('RoutingManager', () => {
393
408
394
409
const stateMapping = createFakeStateMapping ( {
395
410
stateToRoute ( uiState ) {
396
- return {
397
- query : uiState . query && uiState . query . toUpperCase ( ) ,
398
- } ;
411
+ return Object . keys ( uiState ) . reduce ( ( state , indexId ) => {
412
+ const indexState = uiState [ indexId ] ;
413
+
414
+ return {
415
+ ...state ,
416
+ [ indexId ] : {
417
+ query : indexState . query && indexState . query . toUpperCase ( ) ,
418
+ } ,
419
+ } ;
420
+ } , { } ) ;
399
421
} ,
400
422
} ) ;
401
423
402
424
const search = instantsearch ( {
403
- indexName : 'instant_search ' ,
425
+ indexName : 'indexName ' ,
404
426
searchFunction : helper => {
405
427
helper . setQuery ( 'test' ) . search ( ) ;
406
428
} ,
@@ -431,7 +453,9 @@ describe('RoutingManager', () => {
431
453
expect ( search . mainIndex . getHelper ( ) ! . state . query ) . toEqual ( 'test' ) ;
432
454
433
455
expect ( router . write ) . toHaveBeenLastCalledWith ( {
434
- query : 'TEST' ,
456
+ indexName : {
457
+ query : 'TEST' ,
458
+ } ,
435
459
} ) ;
436
460
437
461
done ( ) ;
@@ -446,7 +470,7 @@ describe('RoutingManager', () => {
446
470
} ) ;
447
471
448
472
const search = instantsearch ( {
449
- indexName : 'instant_search ' ,
473
+ indexName : 'indexName ' ,
450
474
searchClient,
451
475
routing : {
452
476
stateMapping,
@@ -469,7 +493,9 @@ describe('RoutingManager', () => {
469
493
470
494
expect ( router . write ) . toHaveBeenCalledTimes ( 1 ) ;
471
495
expect ( router . write ) . toHaveBeenLastCalledWith ( {
472
- query : 'Apple' ,
496
+ indexName : {
497
+ query : 'Apple' ,
498
+ } ,
473
499
} ) ;
474
500
475
501
await runAllMicroTasks ( ) ;
@@ -481,7 +507,9 @@ describe('RoutingManager', () => {
481
507
482
508
expect ( router . write ) . toHaveBeenCalledTimes ( 2 ) ;
483
509
expect ( router . write ) . toHaveBeenLastCalledWith ( {
484
- query : 'Apple' ,
510
+ indexName : {
511
+ query : 'Apple' ,
512
+ } ,
485
513
} ) ;
486
514
} ) ;
487
515
@@ -493,7 +521,7 @@ describe('RoutingManager', () => {
493
521
} ) ;
494
522
495
523
const search = instantsearch ( {
496
- indexName : 'instant_search ' ,
524
+ indexName : 'indexName ' ,
497
525
searchFunction ( helper ) {
498
526
// Force the value of the query
499
527
helper . setQuery ( 'Apple iPhone' ) . search ( ) ;
@@ -518,7 +546,9 @@ describe('RoutingManager', () => {
518
546
519
547
expect ( router . write ) . toHaveBeenCalledTimes ( 1 ) ;
520
548
expect ( router . write ) . toHaveBeenLastCalledWith ( {
521
- query : 'Apple iPhone' ,
549
+ indexName : {
550
+ query : 'Apple iPhone' ,
551
+ } ,
522
552
} ) ;
523
553
524
554
// Trigger getConfiguration
@@ -528,7 +558,9 @@ describe('RoutingManager', () => {
528
558
529
559
expect ( router . write ) . toHaveBeenCalledTimes ( 2 ) ;
530
560
expect ( router . write ) . toHaveBeenLastCalledWith ( {
531
- query : 'Apple iPhone' ,
561
+ indexName : {
562
+ query : 'Apple iPhone' ,
563
+ } ,
532
564
} ) ;
533
565
} ) ;
534
566
@@ -548,7 +580,7 @@ describe('RoutingManager', () => {
548
580
} ) ;
549
581
550
582
const search = instantsearch ( {
551
- indexName : 'instant_search ' ,
583
+ indexName : 'indexName ' ,
552
584
searchClient,
553
585
routing : {
554
586
router,
@@ -571,15 +603,19 @@ describe('RoutingManager', () => {
571
603
572
604
expect ( router . write ) . toHaveBeenCalledTimes ( 1 ) ;
573
605
expect ( router . write ) . toHaveBeenLastCalledWith ( {
574
- query : 'Apple' ,
606
+ indexName : {
607
+ query : 'Apple' ,
608
+ } ,
575
609
} ) ;
576
610
577
611
// Trigger an update - push a change
578
612
fakeSearchBox . refine ( 'Apple iPhone' ) ;
579
613
580
614
expect ( router . write ) . toHaveBeenCalledTimes ( 2 ) ;
581
615
expect ( router . write ) . toHaveBeenLastCalledWith ( {
582
- query : 'Apple iPhone' ,
616
+ indexName : {
617
+ query : 'Apple iPhone' ,
618
+ } ,
583
619
} ) ;
584
620
585
621
await runAllMicroTasks ( ) ;
@@ -596,7 +632,9 @@ describe('RoutingManager', () => {
596
632
597
633
expect ( router . write ) . toHaveBeenCalledTimes ( 3 ) ;
598
634
expect ( router . write ) . toHaveBeenLastCalledWith ( {
599
- query : 'Apple' ,
635
+ indexName : {
636
+ query : 'Apple' ,
637
+ } ,
600
638
} ) ;
601
639
} ) ;
602
640
} ) ;
0 commit comments