Skip to content

Commit

Permalink
test: add async migrate test
Browse files Browse the repository at this point in the history
  • Loading branch information
KiwiKilian committed Nov 4, 2024
1 parent dcc4c09 commit 5d71198
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 2 deletions.
51 changes: 50 additions & 1 deletion tests/persistAsync.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ describe('persist middleware with async configuration', () => {
})
})

it('can migrate persisted state', async () => {
it('can non-async migrate persisted state', async () => {
const setItemSpy = vi.fn()
const onRehydrateStorageSpy = vi.fn()
const migrateSpy = vi.fn(() => ({ count: 99 }))
Expand Down Expand Up @@ -242,6 +242,55 @@ describe('persist middleware with async configuration', () => {
expect(onRehydrateStorageSpy).toBeCalledWith({ count: 99 }, undefined)
})

it('can async migrate persisted state', async () => {
const setItemSpy = vi.fn()
const onRehydrateStorageSpy = vi.fn()
const migrateSpy = vi.fn(() => Promise.resolve({ count: 99 }))

const storage = {
getItem: async () =>
JSON.stringify({
state: { count: 42 },
version: 12,
}),
setItem: setItemSpy,
removeItem: () => {},
}

const useBoundStore = create(
persist(() => ({ count: 0 }), {
name: 'test-storage',
version: 13,
storage: createJSONStorage(() => storage),
onRehydrateStorage: () => onRehydrateStorageSpy,
migrate: migrateSpy,
}),
)

function Counter() {
const { count } = useBoundStore()
return <div>count: {count}</div>
}

render(
<StrictMode>
<Counter />
</StrictMode>,
)

await screen.findByText('count: 0')
await screen.findByText('count: 99')
expect(migrateSpy).toBeCalledWith({ count: 42 }, 12)
expect(setItemSpy).toBeCalledWith(
'test-storage',
JSON.stringify({
state: { count: 99 },
version: 13,
}),
)
expect(onRehydrateStorageSpy).toBeCalledWith({ count: 99 }, undefined)
})

it('can merge partial persisted state', async () => {
const storage = {
getItem: async () =>
Expand Down
39 changes: 38 additions & 1 deletion tests/persistSync.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ describe('persist middleware with sync configuration', () => {
expect(onRehydrateStorageSpy2).toBeCalledWith({ count: 42 }, undefined)
})

it('can migrate persisted state', () => {
it('can non-async migrate persisted state', () => {
const setItemSpy = vi.fn()
const onRehydrateStorageSpy = vi.fn()
const migrateSpy = vi.fn(() => ({ count: 99 }))
Expand Down Expand Up @@ -168,6 +168,43 @@ describe('persist middleware with sync configuration', () => {
expect(onRehydrateStorageSpy).toBeCalledWith({ count: 99 }, undefined)
})

it('can async migrate persisted state', () => {
const setItemSpy = vi.fn()
const onRehydrateStorageSpy = vi.fn()
const migrateSpy = vi.fn(() => Promise.resolve({ count: 99 }))

const storage = {
getItem: () =>
JSON.stringify({
state: { count: 42 },
version: 12,
}),
setItem: setItemSpy,
removeItem: () => {},
}

const useBoundStore = create(
persist(() => ({ count: 0 }), {
name: 'test-storage',
version: 13,
storage: createJSONStorage(() => storage),
onRehydrateStorage: () => onRehydrateStorageSpy,
migrate: migrateSpy,
}),
)

expect(useBoundStore.getState()).toEqual({ count: 99 })
expect(migrateSpy).toBeCalledWith({ count: 42 }, 12)
expect(setItemSpy).toBeCalledWith(
'test-storage',
JSON.stringify({
state: { count: 99 },
version: 13,
}),
)
expect(onRehydrateStorageSpy).toBeCalledWith({ count: 99 }, undefined)
})

it('can correclty handle a missing migrate function', () => {
console.error = vi.fn()
const onRehydrateStorageSpy = vi.fn()
Expand Down

0 comments on commit 5d71198

Please sign in to comment.