Skip to content

Commit da2706c

Browse files
dai-shiGodsdar
andauthored
fix(persist): preserve external devtools action arguments (#3592)
Co-authored-by: Godsdar <[email protected]>
1 parent a1fea2e commit da2706c

2 files changed

Lines changed: 52 additions & 3 deletions

File tree

‎src/middleware/persist.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,8 @@ const persistImpl: PersistImpl = (config, baseOptions) => (set, get, api) => {
228228

229229
const savedSetState = api.setState
230230

231-
api.setState = (state, replace) => {
232-
savedSetState(state, replace as any)
231+
api.setState = (...args) => {
232+
savedSetState(...(args as Parameters<typeof savedSetState>))
233233
return setItem()
234234
}
235235

‎tests/devtools.test.tsx‎

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
22
import type { Mock } from 'vitest'
3-
import { devtools, redux } from 'zustand/middleware'
3+
import { devtools, persist, redux } from 'zustand/middleware'
44
import { createStore } from 'zustand/vanilla'
55
import type { StoreApi } from 'zustand/vanilla'
66

@@ -196,6 +196,55 @@ describe('When state changes...', () => {
196196
})
197197
})
198198

199+
it('preserves external action names and payloads with persist', () => {
200+
const setItem = vi.fn(() => 'stored')
201+
const api = createStore(
202+
devtools(
203+
persist(() => ({ count: 0, foo: 'bar' }), {
204+
name: 'test-storage',
205+
storage: { getItem: () => null, setItem, removeItem: () => {} },
206+
skipHydration: true,
207+
}),
208+
{ name: 'persist-test', store: 'test-store', enabled: true },
209+
),
210+
)
211+
const [connection] = getNamedConnectionApis('persist-test')
212+
213+
expect(api.setState({ count: 10 }, undefined, 'increment')).toBe('stored')
214+
expect(connection.send).toHaveBeenLastCalledWith(
215+
{ type: 'test-store/increment' },
216+
{ 'test-store': { count: 10, foo: 'bar' } },
217+
)
218+
expect(setItem).toHaveBeenLastCalledWith('test-storage', {
219+
state: { count: 10, foo: 'bar' },
220+
version: 0,
221+
})
222+
223+
api.setState((state) => ({ count: state.count + 1 }), false, {
224+
type: 'increment',
225+
payload: 1,
226+
})
227+
expect(connection.send).toHaveBeenLastCalledWith(
228+
{ type: 'test-store/increment', payload: 1 },
229+
{ 'test-store': { count: 11, foo: 'bar' } },
230+
)
231+
expect(setItem).toHaveBeenLastCalledWith('test-storage', {
232+
state: { count: 11, foo: 'bar' },
233+
version: 0,
234+
})
235+
236+
api.setState({ count: 0, foo: 'baz' }, true, 'reset')
237+
expect(connection.send).toHaveBeenLastCalledWith(
238+
{ type: 'test-store/reset' },
239+
{ 'test-store': { count: 0, foo: 'baz' } },
240+
)
241+
expect(setItem).toHaveBeenLastCalledWith('test-storage', {
242+
state: { count: 0, foo: 'baz' },
243+
version: 0,
244+
})
245+
api.devtools.cleanup()
246+
})
247+
199248
describe('When state changes with automatic setter inferring...', () => {
200249
it("sends { type: setStateName || 'setCount`, ...rest } as the action with current state", async () => {
201250
const options = {

0 commit comments

Comments
 (0)