Skip to content

Commit

Permalink
(test): ensure persisted data is loaded from storage
Browse files Browse the repository at this point in the history
- this is pretty critical functionality I forgot to add a test for...
  thanks to code coverage for pointing this out to me!
  • Loading branch information
agilgur5 committed Nov 19, 2019
1 parent c83135b commit 21d8872
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
5 changes: 5 additions & 0 deletions test/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ export const UserStore = types.model('UserStore', {
self.name = name
}
}))

export const persistedData = {
name: 'Persisted Name',
age: 35
}
12 changes: 10 additions & 2 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
import { getSnapshot } from 'mobx-state-tree'

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

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

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

it('should persist nothing if no actions are used', async () => {
Expand All @@ -27,6 +27,14 @@ describe('basic persist options', () => {
expect(getItem('user')).toStrictEqual(getSnapshot(user))
})

it('should load persisted data', async () => {
window.localStorage.setItem('user', JSON.stringify(persistedData))

const user = UserStore.create()
await persist('user', user)
expect(getSnapshot(user)).toStrictEqual(persistedData)
})

it('shouldn\'t jsonify', async () => {
const user = UserStore.create()
await persist('user', user, {
Expand Down

0 comments on commit 21d8872

Please sign in to comment.