Skip to content

Commit 21d8872

Browse files
committed
(test): ensure persisted data is loaded from storage
- this is pretty critical functionality I forgot to add a test for... thanks to code coverage for pointing this out to me!
1 parent c83135b commit 21d8872

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

test/fixtures.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,8 @@ export const UserStore = types.model('UserStore', {
88
self.name = name
99
}
1010
}))
11+
12+
export const persistedData = {
13+
name: 'Persisted Name',
14+
age: 35
15+
}

test/index.spec.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
import { getSnapshot } from 'mobx-state-tree'
33

44
import { persist } from '../src/index'
5-
import { UserStore } from './fixtures'
5+
import { UserStore, persistedData } from './fixtures'
66

77
function getItem(key: string) {
88
const item = window.localStorage.getItem(key)
99
return item ? JSON.parse(item) : null // can only parse strings
1010
}
1111

12-
describe('basic persist options', () => {
12+
describe('persist', () => {
1313
beforeEach(() => window.localStorage.clear())
1414

1515
it('should persist nothing if no actions are used', async () => {
@@ -27,6 +27,14 @@ describe('basic persist options', () => {
2727
expect(getItem('user')).toStrictEqual(getSnapshot(user))
2828
})
2929

30+
it('should load persisted data', async () => {
31+
window.localStorage.setItem('user', JSON.stringify(persistedData))
32+
33+
const user = UserStore.create()
34+
await persist('user', user)
35+
expect(getSnapshot(user)).toStrictEqual(persistedData)
36+
})
37+
3038
it('shouldn\'t jsonify', async () => {
3139
const user = UserStore.create()
3240
await persist('user', user, {

0 commit comments

Comments
 (0)