-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.spec.ts
76 lines (60 loc) · 2.54 KB
/
index.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { describe, it, expect, beforeEach } from 'jest-without-globals'
import { getSnapshot } from 'mobx-state-tree'
import { persist } from '../src/index'
import { getItem } from './helpers'
import { UserStoreF, persistedUserDataF } from './fixtures'
describe('basic persist functionality with primitves', () => {
beforeEach(() => window.localStorage.clear())
it('should persist nothing if no actions are used', async () => {
const user = UserStoreF.create()
await persist('user', user)
expect(getItem('user')).toBe(null)
})
it('should persist snapshot when action used', async () => {
const user = UserStoreF.create()
await persist('user', user)
user.changeName('Joe') // fire action to trigger onSnapshot
expect(user.name).toBe('Joe')
expect(getItem('user')).toStrictEqual(getSnapshot(user))
})
it('should load persisted data', async () => {
window.localStorage.setItem('user', JSON.stringify(persistedUserDataF))
const user = UserStoreF.create()
await persist('user', user)
expect(getSnapshot(user)).toStrictEqual(persistedUserDataF)
})
})
describe('persist options', () => {
beforeEach(() => window.localStorage.clear())
it('shouldn\'t jsonify', async () => {
const user = UserStoreF.create()
await persist('user', user, {
jsonify: false
})
user.changeName('Joe') // fire action to trigger onSnapshot
// if not jsonified, localStorage will store as '[object Object]'
expect(window.localStorage.getItem('user')).toBe('[object Object]')
})
it('should whitelist', async () => {
const user = UserStoreF.create()
await persist('user', user, {
whitelist: ['name', 'hasDogs']
})
user.changeName('Joe') // fire action to trigger onSnapshot
const snapshot = { ...getSnapshot(user) } // need to shallow clone as otherwise properties are non-configurable (https://github.com/agilgur5/mst-persist/pull/21#discussion_r348105595)
delete snapshot['age']
expect(snapshot.name).toBe('Joe')
expect(getItem('user')).toStrictEqual(snapshot)
})
it('should blacklist', async () => {
const user = UserStoreF.create()
await persist('user', user, {
blacklist: ['age']
})
user.changeName('Joe') // fire action to trigger onSnapshot
const snapshot = { ...getSnapshot(user) } // need to shallow clone as otherwise properties are non-configurable (https://github.com/agilgur5/mst-persist/pull/21#discussion_r348105595)
delete snapshot['age']
expect(snapshot.name).toBe('Joe')
expect(getItem('user')).toStrictEqual(snapshot)
})
})