1
- import { vi } from 'vitest '
1
+ import * as DevTools from '@internal/devtoolsExtension '
2
2
import type { StoreEnhancer } from '@reduxjs/toolkit'
3
3
import { Tuple } from '@reduxjs/toolkit'
4
4
import type * as Redux from 'redux'
5
- import type * as DevTools from '@internal/devtoolsExtension '
5
+ import { vi } from 'vitest '
6
6
7
- vi . doMock ( 'redux' , async ( ) => {
8
- const redux : any = await vi . importActual ( 'redux' )
7
+ vi . doMock ( 'redux' , async ( importOriginal ) => {
8
+ const redux = await importOriginal < typeof import ( 'redux' ) > ( )
9
9
10
10
vi . spyOn ( redux , 'applyMiddleware' )
11
11
vi . spyOn ( redux , 'combineReducers' )
@@ -15,45 +15,8 @@ vi.doMock('redux', async () => {
15
15
return redux
16
16
} )
17
17
18
- vi . doMock ( '@internal/devtoolsExtension' , async ( ) => {
19
- const devtools : typeof DevTools = await vi . importActual (
20
- '@internal/devtoolsExtension'
21
- )
22
- vi . spyOn ( devtools , 'composeWithDevTools' ) // @remap -prod-remove-line
23
- return devtools
24
- } )
25
-
26
- function originalReduxCompose ( ...funcs : Function [ ] ) {
27
- if ( funcs . length === 0 ) {
28
- // infer the argument type so it is usable in inference down the line
29
- return < T > ( arg : T ) => arg
30
- }
31
-
32
- if ( funcs . length === 1 ) {
33
- return funcs [ 0 ]
34
- }
35
-
36
- return funcs . reduce (
37
- ( a , b ) =>
38
- ( ...args : any ) =>
39
- a ( b ( ...args ) )
40
- )
41
- }
42
-
43
- function originalComposeWithDevtools ( ) {
44
- if ( arguments . length === 0 ) return undefined
45
- if ( typeof arguments [ 0 ] === 'object' ) return originalReduxCompose
46
- return originalReduxCompose . apply ( null , arguments as any as Function [ ] )
47
- }
48
-
49
18
describe ( 'configureStore' , async ( ) => {
50
- // RTK's internal `composeWithDevtools` function isn't publicly exported,
51
- // so we can't mock it. However, it _does_ try to access the global extension method
52
- // attached to `window`. So, if we mock _that_, we'll know if the enhancer ran.
53
- const mockDevtoolsCompose = vi
54
- . fn ( )
55
- . mockImplementation ( originalComposeWithDevtools )
56
- ; ( window as any ) . __REDUX_DEVTOOLS_EXTENSION_COMPOSE__ = mockDevtoolsCompose
19
+ const composeWithDevToolsSpy = vi . spyOn ( DevTools , 'composeWithDevTools' )
57
20
58
21
const redux = await import ( 'redux' )
59
22
@@ -76,7 +39,7 @@ describe('configureStore', async () => {
76
39
expect . any ( Function )
77
40
)
78
41
expect ( redux . applyMiddleware ) . toHaveBeenCalled ( )
79
- expect ( mockDevtoolsCompose ) . toHaveBeenCalled ( ) // @remap -prod-remove-line
42
+ expect ( composeWithDevToolsSpy ) . toHaveBeenCalled ( ) // @remap -prod-remove-line
80
43
} )
81
44
} )
82
45
@@ -90,7 +53,7 @@ describe('configureStore', async () => {
90
53
expect ( configureStore ( { reducer } ) ) . toBeInstanceOf ( Object )
91
54
expect ( redux . combineReducers ) . toHaveBeenCalledWith ( reducer )
92
55
expect ( redux . applyMiddleware ) . toHaveBeenCalled ( )
93
- expect ( mockDevtoolsCompose ) . toHaveBeenCalled ( ) // @remap -prod-remove-line -line
56
+ expect ( composeWithDevToolsSpy ) . toHaveBeenCalled ( ) // @remap -prod-remove-line
94
57
expect ( redux . createStore ) . toHaveBeenCalledWith (
95
58
expect . any ( Function ) ,
96
59
undefined ,
@@ -113,7 +76,7 @@ describe('configureStore', async () => {
113
76
configureStore ( { middleware : ( ) => new Tuple ( ) , reducer } )
114
77
) . toBeInstanceOf ( Object )
115
78
expect ( redux . applyMiddleware ) . toHaveBeenCalledWith ( )
116
- expect ( mockDevtoolsCompose ) . toHaveBeenCalled ( ) // @remap -prod-remove-line -line
79
+ expect ( composeWithDevToolsSpy ) . toHaveBeenCalled ( ) // @remap -prod-remove-line
117
80
expect ( redux . createStore ) . toHaveBeenCalledWith (
118
81
reducer ,
119
82
undefined ,
@@ -142,7 +105,7 @@ describe('configureStore', async () => {
142
105
expect . any ( Function ) , // serializableCheck
143
106
expect . any ( Function ) // actionCreatorCheck
144
107
)
145
- expect ( mockDevtoolsCompose ) . toHaveBeenCalled ( ) // @remap -prod-remove-line -line
108
+ expect ( composeWithDevToolsSpy ) . toHaveBeenCalled ( ) // @remap -prod-remove-line
146
109
expect ( redux . createStore ) . toHaveBeenCalledWith (
147
110
reducer ,
148
111
undefined ,
@@ -179,7 +142,7 @@ describe('configureStore', async () => {
179
142
configureStore ( { middleware : ( ) => new Tuple ( thank ) , reducer } )
180
143
) . toBeInstanceOf ( Object )
181
144
expect ( redux . applyMiddleware ) . toHaveBeenCalledWith ( thank )
182
- expect ( mockDevtoolsCompose ) . toHaveBeenCalled ( ) // @remap -prod-remove-line -line
145
+ expect ( composeWithDevToolsSpy ) . toHaveBeenCalled ( ) // @remap -prod-remove-line
183
146
expect ( redux . createStore ) . toHaveBeenCalledWith (
184
147
reducer ,
185
148
undefined ,
@@ -234,7 +197,7 @@ describe('configureStore', async () => {
234
197
Object
235
198
)
236
199
expect ( redux . applyMiddleware ) . toHaveBeenCalled ( )
237
- expect ( mockDevtoolsCompose ) . toHaveBeenCalledWith ( options ) // @remap -prod-remove-line
200
+ expect ( composeWithDevToolsSpy ) . toHaveBeenCalledWith ( options ) // @remap -prod-remove-line
238
201
expect ( redux . createStore ) . toHaveBeenCalledWith (
239
202
reducer ,
240
203
undefined ,
@@ -247,7 +210,7 @@ describe('configureStore', async () => {
247
210
it ( 'calls createStore with preloadedState' , ( ) => {
248
211
expect ( configureStore ( { reducer } ) ) . toBeInstanceOf ( Object )
249
212
expect ( redux . applyMiddleware ) . toHaveBeenCalled ( )
250
- expect ( mockDevtoolsCompose ) . toHaveBeenCalled ( ) // @remap -prod-remove-line
213
+ expect ( composeWithDevToolsSpy ) . toHaveBeenCalled ( ) // @remap -prod-remove-line
251
214
expect ( redux . createStore ) . toHaveBeenCalledWith (
252
215
reducer ,
253
216
undefined ,
@@ -278,7 +241,7 @@ describe('configureStore', async () => {
278
241
} )
279
242
) . toBeInstanceOf ( Object )
280
243
expect ( redux . applyMiddleware ) . toHaveBeenCalled ( )
281
- expect ( mockDevtoolsCompose ) . toHaveBeenCalled ( ) // @remap -prod-remove-line
244
+ expect ( composeWithDevToolsSpy ) . toHaveBeenCalled ( ) // @remap -prod-remove-line
282
245
expect ( redux . createStore ) . toHaveBeenCalledWith (
283
246
reducer ,
284
247
undefined ,
0 commit comments