Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1398eae

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 parent514715e commit1398eae

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
constblacklistDict=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
constsnapshot={ ..._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
exportconstUserStore=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+
functiongetItem(key:string){
8+
constitem=window.localStorage.getItem(key)
9+
returnitem ?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
constuser=UserStore.create()
1117
awaitpersist('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+
constuser=UserStore.create()
24+
awaitpersist('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+
constuser=UserStore.create()
32+
awaitpersist('user',user,{
33+
whitelist:['name']
34+
})
35+
36+
user.changeName('Joe')// fire action to trigger onSnapshot
37+
constsnapshot={ ...getSnapshot(user)}// need to shallow clone as otherwise properties are non-configurable (https://github.com/agilgur5/mst-persist/pull/21#discussion_r348105595)
38+
deletesnapshot['age']
39+
expect(getItem('user')).toStrictEqual(snapshot)
40+
})
41+
42+
it('should blacklist',async()=>{
43+
constuser=UserStore.create()
44+
awaitpersist('user',user,{
45+
blacklist:['age']
46+
})
47+
48+
user.changeName('Joe')// fire action to trigger onSnapshot
49+
constsnapshot={ ...getSnapshot(user)}// need to shallow clone as otherwise properties are non-configurable (https://github.com/agilgur5/mst-persist/pull/21#discussion_r348105595)
50+
deletesnapshot['age']
51+
expect(getItem('user')).toStrictEqual(snapshot)
1452
})
1553
})

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp