@@ -27,7 +27,7 @@ import React from 'react'
27
27
import { screen , waitFor } from '@testing-library/react'
28
28
import userEvent from '@testing-library/user-event'
29
29
30
- import type { SpecItem } from '@looker/sdk-codegen'
30
+ import type { SpecItem , SpecList } from '@looker/sdk-codegen'
31
31
import { useHistory } from 'react-router-dom'
32
32
import * as routerLocation from 'react-router-dom'
33
33
import type { Location } from 'history'
@@ -40,16 +40,38 @@ import {
40
40
import { getApixAdaptor } from '../../utils'
41
41
import { DiffScene } from './DiffScene'
42
42
43
+ // jest.mock('react-router', () => {
44
+ // const ReactRouter = jest.requireActual('react-router')
45
+ // return {
46
+ // ...ReactRouter,
47
+ // useHistory: jest.fn().mockReturnValue({ push: jest.fn(), location }),
48
+ // useLocation: jest.fn().mockReturnValue({ pathname: '/', search: '' }),
49
+ // }
50
+ // })
51
+
43
52
jest . mock ( 'react-router' , ( ) => {
53
+ const mockLocation = {
54
+ pathname : '/' ,
55
+ search : '' ,
56
+ }
44
57
const ReactRouter = jest . requireActual ( 'react-router' )
45
58
return {
46
59
...ReactRouter ,
47
- useHistory : jest . fn ( ) . mockReturnValue ( { push : jest . fn ( ) , location } ) ,
48
- useLocation : jest . fn ( ) . mockReturnValue ( { pathname : '/' , search : '' } ) ,
60
+ useHistory : jest . fn ( ) . mockReturnValue ( {
61
+ push : jest . fn ( ( location ) => {
62
+ mockLocation . pathname = location . pathname
63
+ mockLocation . search = location . search
64
+ } ) ,
65
+ location,
66
+ } ) ,
67
+ useLocation : jest . fn ( ( ) => ( {
68
+ pathname : jest . fn ( ) . mockReturnValue ( mockLocation . pathname ) ,
69
+ search : jest . fn ( ) . mockReturnValue ( mockLocation . search ) ,
70
+ } ) ) ,
49
71
}
50
72
} )
51
73
52
- const specs = getLoadedSpecs ( )
74
+ const specs = getLoadedSpecs ( ) as SpecList
53
75
class MockApixAdaptor {
54
76
async fetchSpec ( spec : SpecItem ) {
55
77
return new Promise ( ( ) => specs [ spec . key ] )
@@ -78,9 +100,10 @@ describe('DiffScene', () => {
78
100
const toggleNavigation = ( ) => false
79
101
test ( 'toggling comparison option pushes param to url' , async ( ) => {
80
102
const { push } = useHistory ( )
103
+ jest . spyOn ( reactRedux , 'useDispatch' ) . mockReturnValue ( mockDispatch )
81
104
const store = createTestStore ( {
82
105
specs : { specs, currentSpecKey : '3.1' } ,
83
- settings : { initialized : true } ,
106
+ settings : { initialized : true , diffOptions : [ ] } ,
84
107
} )
85
108
renderWithRouterAndReduxProvider (
86
109
< DiffScene specs = { specs } toggleNavigation = { toggleNavigation } /> ,
@@ -94,11 +117,15 @@ describe('DiffScene', () => {
94
117
} )
95
118
)
96
119
await waitFor ( ( ) => {
97
- expect ( push ) . toHaveBeenCalledWith ( {
120
+ expect ( push ) . toHaveBeenLastCalledWith ( {
98
121
pathname : '/' ,
99
122
search : 'opts=missing' ,
100
123
} )
101
124
} )
125
+ expect ( mockDispatch ) . toHaveBeenLastCalledWith ( {
126
+ payload : { diffOptions : [ 'missing' ] } ,
127
+ type : 'settings/setDiffOptionsAction' ,
128
+ } )
102
129
// TODO: test URL change leads to store dispatch? - change mock history push implementation to change our location
103
130
// TODO: test that toggling another will push both options to store/url
104
131
} )
0 commit comments