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

Commit21d8872

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 parentc83135b commit21d8872

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+
exportconstpersistedData={
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
functiongetItem(key:string){
88
constitem=window.localStorage.getItem(key)
99
returnitem ?JSON.parse(item) :null// can only parse strings
1010
}
1111

12-
describe('basicpersist 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+
constuser=UserStore.create()
34+
awaitpersist('user',user)
35+
expect(getSnapshot(user)).toStrictEqual(persistedData)
36+
})
37+
3038
it('shouldn\'t jsonify',async()=>{
3139
constuser=UserStore.create()
3240
awaitpersist('user',user,{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp