Content-Length: 399888 | pFad | http://github.com/agilgur5/mst-persist/commit/4f8b9f116d1645112ac2eb790d41f007916b9ff0

33 test: add new suite for complex types, starting with arrays (#38) · agilgur5/mst-persist@4f8b9f1 · GitHub
Skip to content

Commit

Permalink
test: add new suite for complex types, starting with arrays (#38)
Browse files Browse the repository at this point in the history
- 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
agilgur5 authored Jul 19, 2023
1 parent 1e2af1f commit 4f8b9f1
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 9 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ With this conditional check, your store will only be hydrated client-side.
None yet, but can take a look at [agilgur5/react-native-manga-reader-app](https://github.com/agilgur5/react-native-manga-reader-app) which uses it in production.
Can view the commit that implements it [here](https://github.com/agilgur5/react-native-manga-reader-app/pull/2/commits/286725f417d321f25d16ee3858b0e7e6b7886e77).

Can also view some of the internal [tests](test/).

## How it works

Basically just a small wrapper around MST's [`onSnapshot` and `applySnapshot`](https://github.com/mobxjs/mobx-state-tree#snapshots).
Expand Down
39 changes: 39 additions & 0 deletions test/complex.spec.ts
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)
})
})
16 changes: 15 additions & 1 deletion test/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,22 @@ export const UserStoreF = types.model('UserStore', {
}
}))

export const persistedDataF = {
export const persistedUserDataF = {
name: 'Persisted Name',
age: 35,
hasDogs: false,
}

export const ComplexUserStoreF = types.model('ComplexUserStore', {
name: 'John Doe',
dogs: types.array(types.string),
}).actions((self) => ({
addDog(dog: string) {
self.dogs.push(dog)
}
}))

export const persistedComplexUserDataF = {
name: 'John Doe',
dogs: ['Shadow', 'Sparky'],
}
4 changes: 4 additions & 0 deletions test/helpers.ts
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
}
1 change: 1 addition & 0 deletions test/index.node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { describe, it, expect } from 'jest-without-globals'

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

import { UserStoreF } from './fixtures'

describe('node usage', () => {
Expand Down
16 changes: 8 additions & 8 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@ import { describe, it, expect, beforeEach } from 'jest-without-globals'
import { getSnapshot } from 'mobx-state-tree'

import { persist } from '../src/index'
import { UserStoreF, persistedDataF } from './fixtures'

function getItem(key: string) {
const item = window.localStorage.getItem(key)
return item ? JSON.parse(item) : null // can only parse strings
}
import { getItem } from './helpers'
import { UserStoreF, persistedUserDataF } from './fixtures'

describe('basic persist functionality', () => {
describe('basic persist functionality with primitves', () => {
beforeEach(() => window.localStorage.clear())

it('should persist nothing if no actions are used', async () => {
Expand All @@ -24,15 +21,16 @@ describe('basic persist functionality', () => {
await persist('user', user)

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

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

const user = UserStoreF.create()
await persist('user', user)
expect(getSnapshot(user)).toStrictEqual(persistedDataF)
expect(getSnapshot(user)).toStrictEqual(persistedUserDataF)
})
})

Expand All @@ -59,6 +57,7 @@ describe('persist options', () => {
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(snapshot.name).toBe('Joe')
expect(getItem('user')).toStrictEqual(snapshot)
})

Expand All @@ -71,6 +70,7 @@ describe('persist options', () => {
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(snapshot.name).toBe('Joe')
expect(getItem('user')).toStrictEqual(snapshot)
})
})

0 comments on commit 4f8b9f1

Please sign in to comment.








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/agilgur5/mst-persist/commit/4f8b9f116d1645112ac2eb790d41f007916b9ff0

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy