@@ -4,7 +4,13 @@ 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 , UiState , StateMapping , RouteState } from '../../types' ;
7
+ import {
8
+ Router ,
9
+ Widget ,
10
+ UiState ,
11
+ StateMapping ,
12
+ IndexUiState ,
13
+ } from '../../types' ;
8
14
import historyRouter from '../routers/history' ;
9
15
import instantsearch from '../main' ;
10
16
@@ -35,32 +41,30 @@ const createFakeStateMapping = (
35
41
...args ,
36
42
} ) ;
37
43
38
- type Entry = Record < string , unknown > ;
39
-
40
- type HistoryState = {
44
+ type HistoryState < TEntry > = {
41
45
index : number ;
42
- entries : Entry [ ] ;
43
- listeners : Array < ( value : Entry ) => void > ;
46
+ entries : TEntry [ ] ;
47
+ listeners : Array < ( value : TEntry ) => void > ;
44
48
} ;
45
49
46
- const createFakeHistory = (
50
+ const createFakeHistory = < TEntry = Record < string , unknown > > (
47
51
{
48
52
index = - 1 ,
49
53
entries = [ ] ,
50
54
listeners = [ ] ,
51
- } : HistoryState = { } as HistoryState
55
+ } : HistoryState < TEntry > = { } as HistoryState < TEntry >
52
56
) => {
53
- const state : HistoryState = {
57
+ const state : HistoryState < TEntry > = {
54
58
index,
55
59
entries,
56
60
listeners,
57
61
} ;
58
62
59
63
return {
60
- subscribe ( listener : ( entry : Entry ) => void ) {
64
+ subscribe ( listener : ( entry : TEntry ) => void ) {
61
65
state . listeners . push ( listener ) ;
62
66
} ,
63
- push ( value : Entry ) {
67
+ push ( value : TEntry ) {
64
68
state . entries . push ( value ) ;
65
69
state . index ++ ;
66
70
} ,
@@ -379,7 +383,7 @@ describe('RoutingManager', () => {
379
383
test ( 'should keep the UI state up to date on router.update' , async ( ) => {
380
384
const searchClient = createSearchClient ( ) ;
381
385
const stateMapping = createFakeStateMapping ( { } ) ;
382
- const history = createFakeHistory ( ) ;
386
+ const history = createFakeHistory < UiState > ( ) ;
383
387
const router = createFakeRouter ( {
384
388
onUpdate ( fn ) {
385
389
history . subscribe ( state => {
@@ -471,7 +475,7 @@ describe('RoutingManager', () => {
471
475
return uiState ;
472
476
} ,
473
477
} ) ;
474
- const history = createFakeHistory ( ) ;
478
+ const history = createFakeHistory < UiState > ( ) ;
475
479
const router = createFakeRouter ( {
476
480
onUpdate ( fn ) {
477
481
history . subscribe ( state => {
@@ -549,10 +553,10 @@ describe('RoutingManager', () => {
549
553
const searchClient = createSearchClient ( ) ;
550
554
const stateMapping = createFakeStateMapping ( { } ) ;
551
555
const router = historyRouter ( {
552
- windowTitle ( routeState : RouteState ) {
556
+ windowTitle ( routeState ) {
553
557
return `Searching for "${ routeState . query } "` ;
554
558
} ,
555
- } as any ) ;
559
+ } ) ;
556
560
557
561
const search = instantsearch ( {
558
562
indexName : 'instant_search' ,
@@ -596,7 +600,7 @@ describe('RoutingManager', () => {
596
600
url : createFakeUrlWithRefinements ( { length : 22 } ) ,
597
601
} ) ;
598
602
599
- const router = historyRouter ( ) ;
603
+ const router = historyRouter < IndexUiState > ( ) ;
600
604
// @ts -expect-error: This method is considered private but we still use it
601
605
// in the test after the TypeScript migration.
602
606
// In a next refactor, we can consider changing this test implementation.
@@ -605,7 +609,7 @@ describe('RoutingManager', () => {
605
609
location : window . location ,
606
610
} ) ;
607
611
608
- expect ( parsedUrl . refinementList . brand ) . toBeInstanceOf ( Array ) ;
612
+ expect ( parsedUrl . refinementList ! . brand ) . toBeInstanceOf ( Array ) ;
609
613
expect ( parsedUrl ) . toMatchInlineSnapshot ( `
610
614
Object {
611
615
"refinementList": Object {
@@ -643,7 +647,7 @@ describe('RoutingManager', () => {
643
647
url : createFakeUrlWithRefinements ( { length : 100 } ) ,
644
648
} ) ;
645
649
646
- const router = historyRouter ( ) ;
650
+ const router = historyRouter < IndexUiState > ( ) ;
647
651
// @ts -expect-error: This method is considered private but we still use it
648
652
// in the test after the TypeScript migration.
649
653
// In a next refactor, we can consider changing this test implementation.
@@ -652,13 +656,13 @@ describe('RoutingManager', () => {
652
656
location : window . location ,
653
657
} ) ;
654
658
655
- expect ( parsedUrl . refinementList . brand ) . toBeInstanceOf ( Array ) ;
659
+ expect ( parsedUrl . refinementList ! . brand ) . toBeInstanceOf ( Array ) ;
656
660
} ) ;
657
661
} ) ;
658
662
659
663
describe ( 'createURL' , ( ) => {
660
664
it ( 'returns an URL for a `routeState` with refinements' , ( ) => {
661
- const router = historyRouter ( ) ;
665
+ const router = historyRouter < IndexUiState > ( ) ;
662
666
const actual = router . createURL ( {
663
667
query : 'iPhone' ,
664
668
page : 5 ,
0 commit comments