Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add async migrate test #2833

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading