|
1 | 1 | import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
2 | 2 | import type { Mock } from 'vitest' |
3 | | -import { devtools, redux } from 'zustand/middleware' |
| 3 | +import { devtools, persist, redux } from 'zustand/middleware' |
4 | 4 | import { createStore } from 'zustand/vanilla' |
5 | 5 | import type { StoreApi } from 'zustand/vanilla' |
6 | 6 |
|
@@ -196,6 +196,55 @@ describe('When state changes...', () => { |
196 | 196 | }) |
197 | 197 | }) |
198 | 198 |
|
| 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 | + |
199 | 248 | describe('When state changes with automatic setter inferring...', () => { |
200 | 249 | it("sends { type: setStateName || 'setCount`, ...rest } as the action with current state", async () => { |
201 | 250 | const options = { |
|
0 commit comments