Skip to content

Commit

Permalink
(test): ensure transforms apply and do so in correct order
Browse files Browse the repository at this point in the history
- yay back to 100% code coverage now!
  - there's a transform branch missing bc there's no test case where
    there's something in storage and at least one toStorage-only
    transform
    - but covering this else branch doesn't really matter

- create some transform fixtures
- abstract out a setItem function

- should consider splitting the tests and fixtures into separate files
  soon, esp now that we have a few different test suites here
  • Loading branch information
agilgur5 committed Nov 19, 2019
1 parent ae05316 commit d3aa447
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
19 changes: 19 additions & 0 deletions test/fixtures.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { types } from 'mobx-state-tree'

import { ITransform, ITransformArgs } from '../src/index'

export const UserStoreF = types.model('UserStore', {
name: 'John Doe',
age: 32
Expand All @@ -13,3 +15,20 @@ export const persistedDataF = {
name: 'Persisted Name',
age: 35
}

function changeName (name: string) {
let changeNameTransform: ITransformArgs
changeNameTransform = function (snapshot) {
snapshot.name = name
return snapshot
}
return changeNameTransform
}

export function storeNameAsF (name: string): ITransform {
return {toStorage: changeName(name)}
}

export function retrieveNameAsF (name: string): ITransform {
return {fromStorage: changeName(name)}
}
38 changes: 35 additions & 3 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
import { getSnapshot } from 'mobx-state-tree'

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

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

function setItem(key: string, value: object) {
return window.localStorage.setItem(key, JSON.stringify(value))
}

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

Expand All @@ -28,15 +32,16 @@ describe('basic persist functionality', () => {
})

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

const user = UserStoreF.create()
await persist('user', user)

expect(getSnapshot(user)).toStrictEqual(persistedDataF)
})
})

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

it('shouldn\'t jsonify', async () => {
Expand Down Expand Up @@ -74,3 +79,30 @@ describe('persist options', () => {
expect(getItem('user')).toStrictEqual(snapshot)
})
})

describe('transforms', () => {
beforeEach(() => window.localStorage.clear())

it('should apply toStorage transforms in order', async () => {
const user = UserStoreF.create()
await persist('user', user, {
transforms: [storeNameAsF('Jack'), storeNameAsF('Joe')]
})

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

it('should apply fromStorage transforms in reverse order', async () => {
const persistedData = {...persistedDataF}
persistedData.name = 'Not Joe'
setItem('user', persistedData)

const user = UserStoreF.create()
await persist('user', user, {
transforms: [retrieveNameAsF('Joe'), retrieveNameAsF('Jack')]
})

expect(getSnapshot(user).name).toBe('Joe')
})
})

0 comments on commit d3aa447

Please sign in to comment.