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:
Daishi Kato 2021-12-07 16:06:41 +09:00 committed by GitHub
parent d89e7c5f32
commit c54d1df87b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 8 deletions

View File

@ -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,

View File

@ -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
})