-
-
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: add new suite for complex types, starting with arrays (#38)
- add `test/complex.spec.ts` - move `getItem` into `test/helpers.ts` as it's used by both - add a new line to separate test helper file imports from source code imports - distinguish fixture naming a bit to handle two test stores now - specify that the basic test is for "primitives" - add some secondary `expect`s to double-check that actions worked - docs: mention the `test` directory as example usage - used a separate test suite / test file as the basic one may continue to grow (e.g. with more options) and the complex one will definitely grow (e.g. with maps) - I try not to have overly bulky files
- Loading branch information
Showing
6 changed files
with
69 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
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 { ComplexUserStoreF, persistedComplexUserDataF } from './fixtures' | ||
|
||
describe('persist complex types', () => { | ||
beforeEach(() => window.localStorage.clear()) | ||
|
||
it('should persist an array', async () => { | ||
const user = ComplexUserStoreF.create() | ||
await persist('user', user) | ||
|
||
user.addDog('Shadow') // fire action to trigger onSnapshot | ||
expect(user.dogs).toStrictEqual(['Shadow']) | ||
expect(getItem('user')).toStrictEqual(getSnapshot(user)) | ||
}) | ||
|
||
it('should persist a whitelisted array', async () => { | ||
const user = ComplexUserStoreF.create() | ||
await persist('user', user, { | ||
whitelist: ['name', 'dogs'] | ||
}) | ||
|
||
user.addDog('Shadow') // fire action to trigger onSnapshot | ||
expect(user.dogs).toStrictEqual(['Shadow']) | ||
expect(getItem('user')).toStrictEqual(getSnapshot(user)) | ||
}) | ||
|
||
it('should load a persisted array', async () => { | ||
window.localStorage.setItem('user', JSON.stringify(persistedComplexUserDataF)) | ||
|
||
const user = ComplexUserStoreF.create() | ||
await persist('user', user) | ||
expect(getSnapshot(user)).toStrictEqual(persistedComplexUserDataF) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export function getItem(key: string) { | ||
const item = window.localStorage.getItem(key) | ||
return item ? JSON.parse(item) : null // can only parse strings | ||
} |
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