diff --git a/test/fixtures.ts b/test/fixtures.ts index ec78157..38c45c7 100644 --- a/test/fixtures.ts +++ b/test/fixtures.ts @@ -8,3 +8,8 @@ export const UserStore = types.model('UserStore', { self.name = name } })) + +export const persistedData = { + name: 'Persisted Name', + age: 35 +} diff --git a/test/index.spec.ts b/test/index.spec.ts index ffa34db..8053440 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -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 () => { @@ -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, {