Skip to content

Commit 1398eae

Browse files
committed
(test): ensure snapshots are persisted properly
- add one action in order to trigger onSnapshot - add tests for no options, whitelist, and blacklist - add comment about shallow cloning snapshot being required due to non-configurable properties - this code was borrowed from a gist, so I didn't actually know why it was necessary until I tried running the test without shallow cloning
1 parent 514715e commit 1398eae

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export const persist: IArgs = (name, store, options = {}) => {
3434
const blacklistDict = arrToDict(blacklist)
3535

3636
onSnapshot(store, (_snapshot: StrToAnyMap) => {
37+
// need to shallow clone as otherwise properties are non-configurable (https://github.com/agilgur5/mst-persist/pull/21#discussion_r348105595)
3738
const snapshot = { ..._snapshot }
3839
Object.keys(snapshot).forEach((key) => {
3940
if (whitelist && !whitelistDict[key]) {

test/fixtures.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@ import { types } from 'mobx-state-tree'
33
export const UserStore = types.model('UserStore', {
44
name: 'John Doe',
55
age: 32
6-
})
6+
}).actions((self) => ({
7+
changeName(name: string) {
8+
self.name = name
9+
}
10+
}))

test/index.spec.ts

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,53 @@
11
/// <reference types="@types/jest" />
2+
import { getSnapshot } from 'mobx-state-tree'
23

34
import { persist } from '../src/index'
45
import { UserStore } from './fixtures'
56

6-
describe('initialization', () => {
7+
function getItem(key: string) {
8+
const item = window.localStorage.getItem(key)
9+
return item ? JSON.parse(item) : null // can only parse strings
10+
}
11+
12+
describe('basic persist options', () => {
713
beforeEach(() => window.localStorage.clear())
814

915
it('should persist nothing if no actions are used', async () => {
1016
const user = UserStore.create()
1117
await persist('user', user)
1218

13-
expect(window.localStorage.getItem('user')).toBe(null)
19+
expect(getItem('user')).toBe(null)
20+
})
21+
22+
it('should persist snapshot when action used', async () => {
23+
const user = UserStore.create()
24+
await persist('user', user)
25+
26+
user.changeName('Joe') // fire action to trigger onSnapshot
27+
expect(getItem('user')).toStrictEqual(getSnapshot(user))
28+
})
29+
30+
it('should whitelist', async () => {
31+
const user = UserStore.create()
32+
await persist('user', user, {
33+
whitelist: ['name']
34+
})
35+
36+
user.changeName('Joe') // fire action to trigger onSnapshot
37+
const snapshot = { ...getSnapshot(user) } // need to shallow clone as otherwise properties are non-configurable (https://github.com/agilgur5/mst-persist/pull/21#discussion_r348105595)
38+
delete snapshot['age']
39+
expect(getItem('user')).toStrictEqual(snapshot)
40+
})
41+
42+
it('should blacklist', async () => {
43+
const user = UserStore.create()
44+
await persist('user', user, {
45+
blacklist: ['age']
46+
})
47+
48+
user.changeName('Joe') // fire action to trigger onSnapshot
49+
const snapshot = { ...getSnapshot(user) } // need to shallow clone as otherwise properties are non-configurable (https://github.com/agilgur5/mst-persist/pull/21#discussion_r348105595)
50+
delete snapshot['age']
51+
expect(getItem('user')).toStrictEqual(snapshot)
1452
})
1553
})

0 commit comments

Comments
 (0)