mirror of
https://github.com/pmndrs/zustand.git
synced 2025-12-08 19:45:52 +00:00
fix(middleware): devtools work in non-browser (#693)
* fix(middleware): devtools work in non-browser * early return * add a test Co-authored-by: Devansh Jethmalani <devanshj@users.noreply.github.com>
This commit is contained in:
parent
d89e7c5f32
commit
c54d1df87b
@ -87,20 +87,20 @@ export const devtools =
|
||||
? { name: options }
|
||||
: options
|
||||
|
||||
if (typeof window === 'undefined') {
|
||||
return fn(set, get, api)
|
||||
}
|
||||
|
||||
const extensionConnector =
|
||||
(window as any).__REDUX_DEVTOOLS_EXTENSION__ ??
|
||||
(window as any).__REDUX_DEVTOOLS_EXTENSION__ ||
|
||||
(window as any).top.__REDUX_DEVTOOLS_EXTENSION__
|
||||
|
||||
if (!extensionConnector) {
|
||||
if (
|
||||
process.env.NODE_ENV === 'development' &&
|
||||
typeof window !== 'undefined'
|
||||
) {
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
console.warn(
|
||||
'[zustand devtools middleware] Please install/enable Redux devtools extension'
|
||||
)
|
||||
}
|
||||
|
||||
return fn(set, get, api)
|
||||
}
|
||||
|
||||
@ -174,7 +174,7 @@ export const devtools =
|
||||
if (!isRecording) return
|
||||
extension.send(
|
||||
nameOrAction === undefined
|
||||
? { type: devtoolsOptions.anonymousActionType ?? 'anonymous' }
|
||||
? { type: devtoolsOptions.anonymousActionType || 'anonymous' }
|
||||
: typeof nameOrAction === 'string'
|
||||
? { type: nameOrAction }
|
||||
: nameOrAction,
|
||||
|
||||
@ -74,7 +74,7 @@ describe('If there is no extension installed...', () => {
|
||||
})
|
||||
|
||||
describe('When state changes...', () => {
|
||||
it("sends { type: setStateName ?? 'anonymous` } as the action with current state", () => {
|
||||
it("sends { type: setStateName || 'anonymous` } as the action with current state", () => {
|
||||
const api = create(
|
||||
devtools(() => ({ count: 0, foo: 'bar' }), { name: 'testOptionsName' })
|
||||
)
|
||||
@ -478,3 +478,14 @@ it('works with redux middleware', () => {
|
||||
|
||||
console.warn = originalConsoleWarn
|
||||
})
|
||||
|
||||
it('works in non-browser env', () => {
|
||||
const originalWindow = global.window
|
||||
global.window = undefined as any
|
||||
|
||||
expect(() => {
|
||||
create(devtools(() => ({ count: 0 })))
|
||||
}).not.toThrow()
|
||||
|
||||
global.window = originalWindow
|
||||
})
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user