Skip to content

Commit 3909d69

Browse files
committed
Fix composeWithDevTools spy
1 parent 72f867e commit 3909d69

File tree

2 files changed

+14
-49
lines changed

2 files changed

+14
-49
lines changed

packages/toolkit/package.json

Lines changed: 1 addition & 1 deletion
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

Lines changed: 13 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
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

77
vi.doMock('redux', async () => {
88
const redux: any = await vi.importActual('redux')
@@ -15,45 +15,10 @@ 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')
20+
21+
;(window as any).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ = composeWithDevToolsSpy
5722

5823
const redux = await import('redux')
5924

@@ -76,7 +41,7 @@ describe('configureStore', async () => {
7641
expect.any(Function)
7742
)
7843
expect(redux.applyMiddleware).toHaveBeenCalled()
79-
expect(mockDevtoolsCompose).toHaveBeenCalled() // @remap-prod-remove-line
44+
expect(composeWithDevToolsSpy).toHaveBeenCalled() // @remap-prod-remove-line
8045
})
8146
})
8247

@@ -90,7 +55,7 @@ describe('configureStore', async () => {
9055
expect(configureStore({ reducer })).toBeInstanceOf(Object)
9156
expect(redux.combineReducers).toHaveBeenCalledWith(reducer)
9257
expect(redux.applyMiddleware).toHaveBeenCalled()
93-
expect(mockDevtoolsCompose).toHaveBeenCalled() // @remap-prod-remove-line-line
58+
expect(composeWithDevToolsSpy).toHaveBeenCalled() // @remap-prod-remove-line-line
9459
expect(redux.createStore).toHaveBeenCalledWith(
9560
expect.any(Function),
9661
undefined,
@@ -113,7 +78,7 @@ describe('configureStore', async () => {
11378
configureStore({ middleware: () => new Tuple(), reducer })
11479
).toBeInstanceOf(Object)
11580
expect(redux.applyMiddleware).toHaveBeenCalledWith()
116-
expect(mockDevtoolsCompose).toHaveBeenCalled() // @remap-prod-remove-line-line
81+
expect(composeWithDevToolsSpy).toHaveBeenCalled() // @remap-prod-remove-line-line
11782
expect(redux.createStore).toHaveBeenCalledWith(
11883
reducer,
11984
undefined,
@@ -142,7 +107,7 @@ describe('configureStore', async () => {
142107
expect.any(Function), // serializableCheck
143108
expect.any(Function) // actionCreatorCheck
144109
)
145-
expect(mockDevtoolsCompose).toHaveBeenCalled() // @remap-prod-remove-line-line
110+
expect(composeWithDevToolsSpy).toHaveBeenCalled() // @remap-prod-remove-line-line
146111
expect(redux.createStore).toHaveBeenCalledWith(
147112
reducer,
148113
undefined,
@@ -179,7 +144,7 @@ describe('configureStore', async () => {
179144
configureStore({ middleware: () => new Tuple(thank), reducer })
180145
).toBeInstanceOf(Object)
181146
expect(redux.applyMiddleware).toHaveBeenCalledWith(thank)
182-
expect(mockDevtoolsCompose).toHaveBeenCalled() // @remap-prod-remove-line-line
147+
expect(composeWithDevToolsSpy).toHaveBeenCalled() // @remap-prod-remove-line-line
183148
expect(redux.createStore).toHaveBeenCalledWith(
184149
reducer,
185150
undefined,
@@ -234,7 +199,7 @@ describe('configureStore', async () => {
234199
Object
235200
)
236201
expect(redux.applyMiddleware).toHaveBeenCalled()
237-
expect(mockDevtoolsCompose).toHaveBeenCalledWith(options) // @remap-prod-remove-line
202+
expect(composeWithDevToolsSpy).toHaveBeenCalledWith(options) // @remap-prod-remove-line
238203
expect(redux.createStore).toHaveBeenCalledWith(
239204
reducer,
240205
undefined,
@@ -247,7 +212,7 @@ describe('configureStore', async () => {
247212
it('calls createStore with preloadedState', () => {
248213
expect(configureStore({ reducer })).toBeInstanceOf(Object)
249214
expect(redux.applyMiddleware).toHaveBeenCalled()
250-
expect(mockDevtoolsCompose).toHaveBeenCalled() // @remap-prod-remove-line
215+
expect(composeWithDevToolsSpy).toHaveBeenCalled() // @remap-prod-remove-line
251216
expect(redux.createStore).toHaveBeenCalledWith(
252217
reducer,
253218
undefined,
@@ -278,7 +243,7 @@ describe('configureStore', async () => {
278243
})
279244
).toBeInstanceOf(Object)
280245
expect(redux.applyMiddleware).toHaveBeenCalled()
281-
expect(mockDevtoolsCompose).toHaveBeenCalled() // @remap-prod-remove-line
246+
expect(composeWithDevToolsSpy).toHaveBeenCalled() // @remap-prod-remove-line
282247
expect(redux.createStore).toHaveBeenCalledWith(
283248
reducer,
284249
undefined,

0 commit comments

Comments
 (0)