Skip to content

Commit

Permalink
Merge pull request #4093 from aryaemami59/fix-composeWithDevTools-spy
Browse files Browse the repository at this point in the history
Fix `composeWithDevTools` spy
  • Loading branch information
EskiMojo14 committed Jan 20, 2024
2 parents 3c50985 + 29d91c1 commit de172af
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 51 deletions.
2 changes: 1 addition & 1 deletion packages/toolkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
"format": "prettier --write \"(src|examples)/**/*.{ts,tsx}\" \"**/*.md\"",
"format:check": "prettier --list-different \"(src|examples)/**/*.{ts,tsx}\" \"docs/*/**.md\"",
"lint": "eslint src examples",
"test": "vitest",
"test": "vitest --run",
"type-tests": "yarn tsc -p src/tests/tsconfig.typetests.json && yarn tsc -p src/query/tests/tsconfig.typetests.json",
"prepack": "yarn build"
},
Expand Down
63 changes: 13 additions & 50 deletions packages/toolkit/src/tests/configureStore.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { vi } from 'vitest'
import * as DevTools from '@internal/devtoolsExtension'
import type { StoreEnhancer } from '@reduxjs/toolkit'
import { Tuple } from '@reduxjs/toolkit'
import type * as Redux from 'redux'
import type * as DevTools from '@internal/devtoolsExtension'
import { vi } from 'vitest'

vi.doMock('redux', async () => {
const redux: any = await vi.importActual('redux')
vi.doMock('redux', async (importOriginal) => {
const redux = await importOriginal<typeof import('redux')>()

vi.spyOn(redux, 'applyMiddleware')
vi.spyOn(redux, 'combineReducers')
Expand All @@ -15,45 +15,8 @@ vi.doMock('redux', async () => {
return redux
})

vi.doMock('@internal/devtoolsExtension', async () => {
const devtools: typeof DevTools = await vi.importActual(
'@internal/devtoolsExtension'
)
vi.spyOn(devtools, 'composeWithDevTools') // @remap-prod-remove-line
return devtools
})

function originalReduxCompose(...funcs: Function[]) {
if (funcs.length === 0) {
// infer the argument type so it is usable in inference down the line
return <T>(arg: T) => arg
}

if (funcs.length === 1) {
return funcs[0]
}

return funcs.reduce(
(a, b) =>
(...args: any) =>
a(b(...args))
)
}

function originalComposeWithDevtools() {
if (arguments.length === 0) return undefined
if (typeof arguments[0] === 'object') return originalReduxCompose
return originalReduxCompose.apply(null, arguments as any as Function[])
}

describe('configureStore', async () => {
// RTK's internal `composeWithDevtools` function isn't publicly exported,
// so we can't mock it. However, it _does_ try to access the global extension method
// attached to `window`. So, if we mock _that_, we'll know if the enhancer ran.
const mockDevtoolsCompose = vi
.fn()
.mockImplementation(originalComposeWithDevtools)
;(window as any).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ = mockDevtoolsCompose
const composeWithDevToolsSpy = vi.spyOn(DevTools, 'composeWithDevTools')

const redux = await import('redux')

Expand All @@ -76,7 +39,7 @@ describe('configureStore', async () => {
expect.any(Function)
)
expect(redux.applyMiddleware).toHaveBeenCalled()
expect(mockDevtoolsCompose).toHaveBeenCalled() // @remap-prod-remove-line
expect(composeWithDevToolsSpy).toHaveBeenCalled() // @remap-prod-remove-line
})
})

Expand All @@ -90,7 +53,7 @@ describe('configureStore', async () => {
expect(configureStore({ reducer })).toBeInstanceOf(Object)
expect(redux.combineReducers).toHaveBeenCalledWith(reducer)
expect(redux.applyMiddleware).toHaveBeenCalled()
expect(mockDevtoolsCompose).toHaveBeenCalled() // @remap-prod-remove-line-line
expect(composeWithDevToolsSpy).toHaveBeenCalled() // @remap-prod-remove-line
expect(redux.createStore).toHaveBeenCalledWith(
expect.any(Function),
undefined,
Expand All @@ -113,7 +76,7 @@ describe('configureStore', async () => {
configureStore({ middleware: () => new Tuple(), reducer })
).toBeInstanceOf(Object)
expect(redux.applyMiddleware).toHaveBeenCalledWith()
expect(mockDevtoolsCompose).toHaveBeenCalled() // @remap-prod-remove-line-line
expect(composeWithDevToolsSpy).toHaveBeenCalled() // @remap-prod-remove-line
expect(redux.createStore).toHaveBeenCalledWith(
reducer,
undefined,
Expand Down Expand Up @@ -142,7 +105,7 @@ describe('configureStore', async () => {
expect.any(Function), // serializableCheck
expect.any(Function) // actionCreatorCheck
)
expect(mockDevtoolsCompose).toHaveBeenCalled() // @remap-prod-remove-line-line
expect(composeWithDevToolsSpy).toHaveBeenCalled() // @remap-prod-remove-line
expect(redux.createStore).toHaveBeenCalledWith(
reducer,
undefined,
Expand Down Expand Up @@ -179,7 +142,7 @@ describe('configureStore', async () => {
configureStore({ middleware: () => new Tuple(thank), reducer })
).toBeInstanceOf(Object)
expect(redux.applyMiddleware).toHaveBeenCalledWith(thank)
expect(mockDevtoolsCompose).toHaveBeenCalled() // @remap-prod-remove-line-line
expect(composeWithDevToolsSpy).toHaveBeenCalled() // @remap-prod-remove-line
expect(redux.createStore).toHaveBeenCalledWith(
reducer,
undefined,
Expand Down Expand Up @@ -234,7 +197,7 @@ describe('configureStore', async () => {
Object
)
expect(redux.applyMiddleware).toHaveBeenCalled()
expect(mockDevtoolsCompose).toHaveBeenCalledWith(options) // @remap-prod-remove-line
expect(composeWithDevToolsSpy).toHaveBeenCalledWith(options) // @remap-prod-remove-line
expect(redux.createStore).toHaveBeenCalledWith(
reducer,
undefined,
Expand All @@ -247,7 +210,7 @@ describe('configureStore', async () => {
it('calls createStore with preloadedState', () => {
expect(configureStore({ reducer })).toBeInstanceOf(Object)
expect(redux.applyMiddleware).toHaveBeenCalled()
expect(mockDevtoolsCompose).toHaveBeenCalled() // @remap-prod-remove-line
expect(composeWithDevToolsSpy).toHaveBeenCalled() // @remap-prod-remove-line
expect(redux.createStore).toHaveBeenCalledWith(
reducer,
undefined,
Expand Down Expand Up @@ -278,7 +241,7 @@ describe('configureStore', async () => {
})
).toBeInstanceOf(Object)
expect(redux.applyMiddleware).toHaveBeenCalled()
expect(mockDevtoolsCompose).toHaveBeenCalled() // @remap-prod-remove-line
expect(composeWithDevToolsSpy).toHaveBeenCalled() // @remap-prod-remove-line
expect(redux.createStore).toHaveBeenCalledWith(
reducer,
undefined,
Expand Down

0 comments on commit de172af

Please sign in to comment.