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

Commit4f8b9f1

Browse files
authored
test: add new suite for complex types, starting with arrays (#38)
- add `test/complex.spec.ts`- move `getItem` into `test/helpers.ts` as it's used by both - add a new line to separate test helper file imports from source code imports- distinguish fixture naming a bit to handle two test stores now- specify that the basic test is for "primitives"- add some secondary `expect`s to double-check that actions worked- docs: mention the `test` directory as example usage- used a separate test suite / test file as the basic one may continue to grow (e.g. with more options) and the complex one will definitely grow (e.g. with maps) - I try not to have overly bulky files
1 parent1e2af1f commit4f8b9f1

File tree

6 files changed

+69
-9
lines changed

6 files changed

+69
-9
lines changed

‎README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ With this conditional check, your store will only be hydrated client-side.
8686
None yet, but can take a look at[agilgur5/react-native-manga-reader-app](https://github.com/agilgur5/react-native-manga-reader-app) which uses it in production.
8787
Can view the commit that implements it[here](https://github.com/agilgur5/react-native-manga-reader-app/pull/2/commits/286725f417d321f25d16ee3858b0e7e6b7886e77).
8888

89+
Can also view some of the internal[tests](test/).
90+
8991
##How it works
9092

9193
Basically just a small wrapper around MST's[`onSnapshot` and`applySnapshot`](https://github.com/mobxjs/mobx-state-tree#snapshots).

‎test/complex.spec.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import{describe,it,expect,beforeEach}from'jest-without-globals'
2+
import{getSnapshot}from'mobx-state-tree'
3+
4+
import{persist}from'../src/index'
5+
6+
import{getItem}from'./helpers'
7+
import{ComplexUserStoreF,persistedComplexUserDataF}from'./fixtures'
8+
9+
describe('persist complex types',()=>{
10+
beforeEach(()=>window.localStorage.clear())
11+
12+
it('should persist an array',async()=>{
13+
constuser=ComplexUserStoreF.create()
14+
awaitpersist('user',user)
15+
16+
user.addDog('Shadow')// fire action to trigger onSnapshot
17+
expect(user.dogs).toStrictEqual(['Shadow'])
18+
expect(getItem('user')).toStrictEqual(getSnapshot(user))
19+
})
20+
21+
it('should persist a whitelisted array',async()=>{
22+
constuser=ComplexUserStoreF.create()
23+
awaitpersist('user',user,{
24+
whitelist:['name','dogs']
25+
})
26+
27+
user.addDog('Shadow')// fire action to trigger onSnapshot
28+
expect(user.dogs).toStrictEqual(['Shadow'])
29+
expect(getItem('user')).toStrictEqual(getSnapshot(user))
30+
})
31+
32+
it('should load a persisted array',async()=>{
33+
window.localStorage.setItem('user',JSON.stringify(persistedComplexUserDataF))
34+
35+
constuser=ComplexUserStoreF.create()
36+
awaitpersist('user',user)
37+
expect(getSnapshot(user)).toStrictEqual(persistedComplexUserDataF)
38+
})
39+
})

‎test/fixtures.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,22 @@ export const UserStoreF = types.model('UserStore', {
1010
}
1111
}))
1212

13-
exportconstpersistedDataF={
13+
exportconstpersistedUserDataF={
1414
name:'Persisted Name',
1515
age:35,
1616
hasDogs:false,
1717
}
18+
19+
exportconstComplexUserStoreF=types.model('ComplexUserStore',{
20+
name:'John Doe',
21+
dogs:types.array(types.string),
22+
}).actions((self)=>({
23+
addDog(dog:string){
24+
self.dogs.push(dog)
25+
}
26+
}))
27+
28+
exportconstpersistedComplexUserDataF={
29+
name:'John Doe',
30+
dogs:['Shadow','Sparky'],
31+
}

‎test/helpers.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
exportfunctiongetItem(key:string){
2+
constitem=window.localStorage.getItem(key)
3+
returnitem ?JSON.parse(item) :null// can only parse strings
4+
}

‎test/index.node.spec.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import{describe,it,expect}from'jest-without-globals'
77

88
import{persist}from'../src/index'
9+
910
import{UserStoreF}from'./fixtures'
1011

1112
describe('node usage',()=>{

‎test/index.spec.ts‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@ import { describe, it, expect, beforeEach } from 'jest-without-globals'
22
import{getSnapshot}from'mobx-state-tree'
33

44
import{persist}from'../src/index'
5-
import{UserStoreF,persistedDataF}from'./fixtures'
65

7-
functiongetItem(key:string){
8-
constitem=window.localStorage.getItem(key)
9-
returnitem ?JSON.parse(item) :null// can only parse strings
10-
}
6+
import{getItem}from'./helpers'
7+
import{UserStoreF,persistedUserDataF}from'./fixtures'
118

12-
describe('basic persist functionality',()=>{
9+
describe('basic persist functionality with primitves',()=>{
1310
beforeEach(()=>window.localStorage.clear())
1411

1512
it('should persist nothing if no actions are used',async()=>{
@@ -24,15 +21,16 @@ describe('basic persist functionality', () => {
2421
awaitpersist('user',user)
2522

2623
user.changeName('Joe')// fire action to trigger onSnapshot
24+
expect(user.name).toBe('Joe')
2725
expect(getItem('user')).toStrictEqual(getSnapshot(user))
2826
})
2927

3028
it('should load persisted data',async()=>{
31-
window.localStorage.setItem('user',JSON.stringify(persistedDataF))
29+
window.localStorage.setItem('user',JSON.stringify(persistedUserDataF))
3230

3331
constuser=UserStoreF.create()
3432
awaitpersist('user',user)
35-
expect(getSnapshot(user)).toStrictEqual(persistedDataF)
33+
expect(getSnapshot(user)).toStrictEqual(persistedUserDataF)
3634
})
3735
})
3836

@@ -59,6 +57,7 @@ describe('persist options', () => {
5957
user.changeName('Joe')// fire action to trigger onSnapshot
6058
constsnapshot={ ...getSnapshot(user)}// need to shallow clone as otherwise properties are non-configurable (https://github.com/agilgur5/mst-persist/pull/21#discussion_r348105595)
6159
deletesnapshot['age']
60+
expect(snapshot.name).toBe('Joe')
6261
expect(getItem('user')).toStrictEqual(snapshot)
6362
})
6463

@@ -71,6 +70,7 @@ describe('persist options', () => {
7170
user.changeName('Joe')// fire action to trigger onSnapshot
7271
constsnapshot={ ...getSnapshot(user)}// need to shallow clone as otherwise properties are non-configurable (https://github.com/agilgur5/mst-persist/pull/21#discussion_r348105595)
7372
deletesnapshot['age']
73+
expect(snapshot.name).toBe('Joe')
7474
expect(getItem('user')).toStrictEqual(snapshot)
7575
})
7676
})

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp