Last active
May 27, 2021 20:18
-
-
Save barbogast/b6bf86f4bacb1da3e42e1f428c805eae to your computer and use it in GitHub Desktop.
Test for sync storage hydration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import create from '../src/index' | |
import { persist } from '../src/middleware' | |
const consoleError = console.error | |
afterEach(() => { | |
console.error = consoleError | |
}) | |
it('can rehydrate state', () => { | |
let postRehydrationCallbackCallCount = 0 | |
const storage = { | |
getItem: (name: string) => | |
JSON.stringify({ | |
state: { count: 42, name }, | |
version: 0, | |
}), | |
setItem: () => {}, | |
} | |
const useStore = create( | |
persist( | |
() => ({ | |
count: 0, | |
name: 'empty', | |
}), | |
{ | |
name: 'test-storage', | |
getStorage: () => storage, | |
onRehydrateStorage: () => (state, error) => { | |
postRehydrationCallbackCallCount++ | |
expect(error).toBeUndefined() | |
expect(state?.count).toBe(42) | |
expect(state?.name).toBe('test-storage') | |
}, | |
} | |
) | |
) | |
expect(useStore.getState()).toEqual({ count: 42, name: 'test-storage' }) | |
expect(postRehydrationCallbackCallCount).toBe(1) | |
}) | |
it('can throw rehydrate error', () => { | |
let postRehydrationCallbackCallCount = 0 | |
const storage = { | |
getItem: () => { | |
throw new Error('getItem error') | |
}, | |
setItem: () => {}, | |
} | |
create( | |
persist(() => ({ count: 0 }), { | |
name: 'test-storage', | |
getStorage: () => storage, | |
onRehydrateStorage: () => (_, e) => { | |
postRehydrationCallbackCallCount++ | |
expect(e?.message).toBe('getItem error') | |
}, | |
}) | |
) | |
expect(postRehydrationCallbackCallCount).toBe(1) | |
}) | |
it('can persist state', () => { | |
let setItemCallCount = 0 | |
const storage = { | |
getItem: () => null, | |
setItem: (name: string, value: string) => { | |
setItemCallCount++ | |
expect(name).toBe('test-storage') | |
expect(value).toBe( | |
JSON.stringify({ | |
state: { count: 42 }, | |
version: 0, | |
}) | |
) | |
}, | |
} | |
const useStore = create<any>( | |
persist(() => ({ count: 0 }), { | |
name: 'test-storage', | |
getStorage: () => storage, | |
onRehydrateStorage: () => (_, error) => { | |
expect(error).toBeUndefined() | |
}, | |
}) | |
) | |
expect(useStore.getState()).toEqual({ count: 0 }) | |
useStore.setState({ count: 42 }) | |
expect(useStore.getState()).toEqual({ count: 42 }) | |
expect(setItemCallCount).toBe(1) | |
}) | |
it('can migrate persisted state', () => { | |
let migrateCallCount = 0 | |
let setItemCallCount = 0 | |
const storage = { | |
getItem: () => | |
JSON.stringify({ | |
state: { count: 42 }, | |
version: 12, | |
}), | |
setItem: (_: string, value: string) => { | |
setItemCallCount++ | |
expect(value).toBe( | |
JSON.stringify({ | |
state: { count: 99 }, | |
version: 13, | |
}) | |
) | |
}, | |
} | |
const useStore = create( | |
persist(() => ({ count: 0 }), { | |
name: 'test-storage', | |
version: 13, | |
getStorage: () => storage, | |
onRehydrateStorage: () => (_, error) => { | |
expect(error).toBeUndefined() | |
}, | |
migrate: (state, version) => { | |
migrateCallCount++ | |
expect(state.count).toBe(42) | |
expect(version).toBe(12) | |
return { count: 99 } | |
}, | |
}) | |
) | |
expect(useStore.getState()).toEqual({ count: 99 }) | |
expect(migrateCallCount).toBe(1) | |
expect(setItemCallCount).toBe(1) | |
}) | |
it('can throw migrate error', () => { | |
let postRehydrationCallbackCallCount = 0 | |
const storage = { | |
getItem: () => | |
JSON.stringify({ | |
state: {}, | |
version: 12, | |
}), | |
setItem: () => {}, | |
} | |
const useStore = create( | |
persist(() => ({ count: 0 }), { | |
name: 'test-storage', | |
version: 13, | |
getStorage: () => storage, | |
migrate: () => { | |
throw new Error('migrate error') | |
}, | |
onRehydrateStorage: () => (_, e) => { | |
postRehydrationCallbackCallCount++ | |
expect(e?.message).toBe('migrate error') | |
}, | |
}) | |
) | |
expect(useStore.getState()).toEqual({ count: 0 }) | |
expect(postRehydrationCallbackCallCount).toBe(1) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment