Skip to content

Commit

Permalink
(test): ensure snapshots are persisted properly
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
agilgur5 committed Nov 19, 2019
1 parent 514715e commit 1398eae
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const persist: IArgs = (name, store, options = {}) => {
const blacklistDict = arrToDict(blacklist)

onSnapshot(store, (_snapshot: StrToAnyMap) => {
// need to shallow clone as otherwise properties are non-configurable (https://github.com/agilgur5/mst-persist/pull/21#discussion_r348105595)
const snapshot = { ..._snapshot }
Object.keys(snapshot).forEach((key) => {
if (whitelist && !whitelistDict[key]) {
Expand Down
6 changes: 5 additions & 1 deletion test/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ import { types } from 'mobx-state-tree'
export const UserStore = types.model('UserStore', {
name: 'John Doe',
age: 32
})
}).actions((self) => ({
changeName(name: string) {
self.name = name
}
}))
42 changes: 40 additions & 2 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,53 @@
/// <reference types="@types/jest" />
import { getSnapshot } from 'mobx-state-tree'

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

describe('initialization', () => {
function getItem(key: string) {
const item = window.localStorage.getItem(key)
return item ? JSON.parse(item) : null // can only parse strings
}

describe('basic persist options', () => {
beforeEach(() => window.localStorage.clear())

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

expect(window.localStorage.getItem('user')).toBe(null)
expect(getItem('user')).toBe(null)
})

it('should persist snapshot when action used', async () => {
const user = UserStore.create()
await persist('user', user)

user.changeName('Joe') // fire action to trigger onSnapshot
expect(getItem('user')).toStrictEqual(getSnapshot(user))
})

it('should whitelist', async () => {
const user = UserStore.create()
await persist('user', user, {
whitelist: ['name']
})

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(getItem('user')).toStrictEqual(snapshot)
})

it('should blacklist', async () => {
const user = UserStore.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(getItem('user')).toStrictEqual(snapshot)
})
})

0 comments on commit 1398eae

Please sign in to comment.