Skip to content

Commit de172af

Browse files
authored
Merge pull request #4093 from aryaemami59/fix-composeWithDevTools-spy
Fix `composeWithDevTools` spy
2 parents 3c50985 + 29d91c1 commit de172af

File tree

2 files changed

+14
-51
lines changed

2 files changed

+14
-51
lines changed

packages/toolkit/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
"format": "prettier --write \"(src|examples)/**/*.{ts,tsx}\" \"**/*.md\"",
101101
"format:check": "prettier --list-different \"(src|examples)/**/*.{ts,tsx}\" \"docs/*/**.md\"",
102102
"lint": "eslint src examples",
103-
"test": "vitest",
103+
"test": "vitest --run",
104104
"type-tests": "yarn tsc -p src/tests/tsconfig.typetests.json && yarn tsc -p src/query/tests/tsconfig.typetests.json",
105105
"prepack": "yarn build"
106106
},

packages/toolkit/src/tests/configureStore.test.ts

+13-50
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { vi } from 'vitest'
1+
import * as DevTools from '@internal/devtoolsExtension'
22
import type { StoreEnhancer } from '@reduxjs/toolkit'
33
import { Tuple } from '@reduxjs/toolkit'
44
import type * as Redux from 'redux'
5-
import type * as DevTools from '@internal/devtoolsExtension'
5+
import { vi } from 'vitest'
66

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')>()
99

1010
vi.spyOn(redux, 'applyMiddleware')
1111
vi.spyOn(redux, 'combineReducers')
@@ -15,45 +15,8 @@ vi.doMock('redux', async () => {
1515
return redux
1616
})
1717

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-
4918
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')
5720

5821
const redux = await import('redux')
5922

@@ -76,7 +39,7 @@ describe('configureStore', async () => {
7639
expect.any(Function)
7740
)
7841
expect(redux.applyMiddleware).toHaveBeenCalled()
79-
expect(mockDevtoolsCompose).toHaveBeenCalled() // @remap-prod-remove-line
42+
expect(composeWithDevToolsSpy).toHaveBeenCalled() // @remap-prod-remove-line
8043
})
8144
})
8245

@@ -90,7 +53,7 @@ describe('configureStore', async () => {
9053
expect(configureStore({ reducer })).toBeInstanceOf(Object)
9154
expect(redux.combineReducers).toHaveBeenCalledWith(reducer)
9255
expect(redux.applyMiddleware).toHaveBeenCalled()
93-
expect(mockDevtoolsCompose).toHaveBeenCalled() // @remap-prod-remove-line-line
56+
expect(composeWithDevToolsSpy).toHaveBeenCalled() // @remap-prod-remove-line
9457
expect(redux.createStore).toHaveBeenCalledWith(
9558
expect.any(Function),
9659
undefined,
@@ -113,7 +76,7 @@ describe('configureStore', async () => {
11376
configureStore({ middleware: () => new Tuple(), reducer })
11477
).toBeInstanceOf(Object)
11578
expect(redux.applyMiddleware).toHaveBeenCalledWith()
116-
expect(mockDevtoolsCompose).toHaveBeenCalled() // @remap-prod-remove-line-line
79+
expect(composeWithDevToolsSpy).toHaveBeenCalled() // @remap-prod-remove-line
11780
expect(redux.createStore).toHaveBeenCalledWith(
11881
reducer,
11982
undefined,
@@ -142,7 +105,7 @@ describe('configureStore', async () => {
142105
expect.any(Function), // serializableCheck
143106
expect.any(Function) // actionCreatorCheck
144107
)
145-
expect(mockDevtoolsCompose).toHaveBeenCalled() // @remap-prod-remove-line-line
108+
expect(composeWithDevToolsSpy).toHaveBeenCalled() // @remap-prod-remove-line
146109
expect(redux.createStore).toHaveBeenCalledWith(
147110
reducer,
148111
undefined,
@@ -179,7 +142,7 @@ describe('configureStore', async () => {
179142
configureStore({ middleware: () => new Tuple(thank), reducer })
180143
).toBeInstanceOf(Object)
181144
expect(redux.applyMiddleware).toHaveBeenCalledWith(thank)
182-
expect(mockDevtoolsCompose).toHaveBeenCalled() // @remap-prod-remove-line-line
145+
expect(composeWithDevToolsSpy).toHaveBeenCalled() // @remap-prod-remove-line
183146
expect(redux.createStore).toHaveBeenCalledWith(
184147
reducer,
185148
undefined,
@@ -234,7 +197,7 @@ describe('configureStore', async () => {
234197
Object
235198
)
236199
expect(redux.applyMiddleware).toHaveBeenCalled()
237-
expect(mockDevtoolsCompose).toHaveBeenCalledWith(options) // @remap-prod-remove-line
200+
expect(composeWithDevToolsSpy).toHaveBeenCalledWith(options) // @remap-prod-remove-line
238201
expect(redux.createStore).toHaveBeenCalledWith(
239202
reducer,
240203
undefined,
@@ -247,7 +210,7 @@ describe('configureStore', async () => {
247210
it('calls createStore with preloadedState', () => {
248211
expect(configureStore({ reducer })).toBeInstanceOf(Object)
249212
expect(redux.applyMiddleware).toHaveBeenCalled()
250-
expect(mockDevtoolsCompose).toHaveBeenCalled() // @remap-prod-remove-line
213+
expect(composeWithDevToolsSpy).toHaveBeenCalled() // @remap-prod-remove-line
251214
expect(redux.createStore).toHaveBeenCalledWith(
252215
reducer,
253216
undefined,
@@ -278,7 +241,7 @@ describe('configureStore', async () => {
278241
})
279242
).toBeInstanceOf(Object)
280243
expect(redux.applyMiddleware).toHaveBeenCalled()
281-
expect(mockDevtoolsCompose).toHaveBeenCalled() // @remap-prod-remove-line
244+
expect(composeWithDevToolsSpy).toHaveBeenCalled() // @remap-prod-remove-line
282245
expect(redux.createStore).toHaveBeenCalledWith(
283246
reducer,
284247
undefined,

0 commit comments

Comments
 (0)