-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(test): ensure snapshots are persisted properly
- 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
Showing
3 changed files
with
46 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |