Skip to content

Commit

Permalink
Reset and patch reject when deserialize fails. Add new test helper
Browse files Browse the repository at this point in the history
  • Loading branch information
nhunzaker committed Feb 16, 2017
1 parent c200903 commit 630ae36
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 62 deletions.
31 changes: 30 additions & 1 deletion src/addons/jest-matchers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { tag, getRegistration } from '../microcosm'
import Microcosm, { Action, tag, get, getRegistration } from '../microcosm'

expect.extend({

Expand All @@ -18,6 +18,11 @@ expect.extend({
},

toHaveStatus (action, status) {
if (action instanceof Action === false) {
throw new TypeError("toHaveStatus expects an Action. Received " +
(action ? "a " + action.constructor.name : action) + ".")
}

let operator = this.isNot ? 'not to' : 'to'
let pass = action.is(status)

Expand All @@ -27,6 +32,30 @@ expect.extend({
return `Expected action ${operator} to be"${status}".`
}
}
},

toHaveState (repo, key, value) {
if (repo instanceof Microcosm === false) {
throw new TypeError("toHaveState expects a Microcosm. Received " +
(repo ? "a " + repo.constructor.name : repo) + ".")
}

let operator = this.isNot ? 'not to' : 'to'
let pass = false
let actual = get(repo.state, key)

if (arguments.length > 2) {
pass = actual === value
} else {
pass = actual !== undefined
}

return {
pass: pass,
message: () => {
return `Expected repo state at "${key}" ${operator} be ${value}. Found ${actual}.`
}
}
}

})
24 changes: 18 additions & 6 deletions src/lifecycle.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
import tag from './tag'

export const RESET = tag(function (data, deserialize) {
function sandbox (data, deserialize) {
return (action, repo) => {
action.resolve(deserialize ? repo.deserialize(data) : data)
let payload = data

if (deserialize) {
try {
payload = repo.deserialize(data)
} catch (error) {
action.reject(error)
}
}

action.resolve(payload)
}
}

export const RESET = tag(function reset (data, deserialize) {
return sandbox(data, deserialize)
}, 'reset')

export const PATCH = tag(function (data, deserialize) {
return (action, repo) => {
action.resolve(deserialize ? repo.deserialize(data) : data)
}
export const PATCH = tag(function patch (data, deserialize) {
return sandbox(data, deserialize)
}, 'patch')

export const ADD_DOMAIN = 'addDomain'
8 changes: 4 additions & 4 deletions test/action.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ it('actions can be tested externally', function () {
})

repo.append(identity).open()
expect(repo.state.test).toBe('open')
expect(repo).toHaveState('test', 'open')

repo.append(identity).resolve()
expect(repo.state.test).toBe('done')
expect(repo).toHaveState('test', 'done')
})

it('handles listeners with no callback', function () {
Expand Down Expand Up @@ -488,7 +488,7 @@ describe('::toggle', function() {
repo.push('action', 2)
repo.push('action', 1).toggle()

expect(repo.state.count).toEqual(2)
expect(repo).toHaveState('count', 2)
})

it('it will not dispatch an action disabled in the middle', function () {
Expand All @@ -511,7 +511,7 @@ describe('::toggle', function() {

second.toggle()

expect(repo.state.count).toEqual(4)
expect(repo).toHaveState('count', 4)
})

})
6 changes: 3 additions & 3 deletions test/dispatch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ it('does not mutate base state on prior dispatches', function () {

repo.push(mutation)
expect(repo.history.size).toEqual(0)
expect(repo.state.toggled).toEqual(true)
expect(repo).toHaveState('toggled', true)

repo.push(mutation)
expect(repo.history.size).toEqual(0)
expect(repo.state.toggled).toEqual(false)
expect(repo).toHaveState('toggled', false)

repo.push(mutation)
expect(repo.history.size).toEqual(0)
expect(repo.state.toggled).toEqual(true)
expect(repo).toHaveState('toggled', true)
})
6 changes: 3 additions & 3 deletions test/domain.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('Creation modes', function () {
}
})

expect(repo.state.count).toBe(0)
expect(repo).toHaveState('count', 0)
})

it('object - original primitive is not mutated', function () {
Expand Down Expand Up @@ -39,7 +39,7 @@ describe('Creation modes', function () {

repo.addDomain('count', Counter)

expect(repo.state.count).toBe(0)
expect(repo).toHaveState('count', 0)
})

it('class - extends domain', function () {
Expand All @@ -53,7 +53,7 @@ describe('Creation modes', function () {

repo.addDomain('count', Counter)

expect(repo.state.count).toBe(0)
expect(repo).toHaveState('count', 0)
})

})
Expand Down
2 changes: 1 addition & 1 deletion test/effects.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ describe('state', function () {

const Effect = {
handler (repo) {
expect(repo.state.test).toBe(true)
expect(repo).toHaveState('test', true)
},
register() {
return {
Expand Down
96 changes: 71 additions & 25 deletions test/microcosm.test.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,93 @@
import Microcosm from '../src/microcosm'

it('it can instantiate with a starting state', function () {
class Repo extends Microcosm {
setup () {
this.addDomain('foo', {})
describe('construction', function () {

it('it can instantiate with a starting state', function () {
class Repo extends Microcosm {
setup () {
this.addDomain('foo', {})
}
}
}

const repo = new Repo({}, { foo: 'bar' })
const repo = new Repo({}, { foo: 'bar' })

expect(repo.state.foo).toEqual('bar')
})
expect(repo).toHaveState('foo', 'bar')
})

it('it can deserialize starting state', function () {
class Repo extends Microcosm {
setup () {
this.addDomain('foo', {})
it('it can deserialize starting state', function () {
class Repo extends Microcosm {
setup () {
this.addDomain('foo', {})
}
}
}

let raw = JSON.stringify({ foo: 'bar' })
let raw = JSON.stringify({ foo: 'bar' })

let repo = new Repo({}, raw, true)
let repo = new Repo({}, raw, true)

expect(repo).toHaveState('foo', 'bar')
})

expect(repo.state.foo).toEqual('bar')
})

it('reset returns to initial state', function () {
const repo = new Microcosm()
describe('::reset', function () {

repo.addDomain('test', {
getInitialState: () => false
it('reset returns to initial state', function () {
const repo = new Microcosm()

repo.addDomain('test', {
getInitialState: () => false
})

repo.patch({ test: true })

expect(repo).toHaveState('test', true)

repo.reset()

expect(repo).toHaveState('test', false)
})

repo.patch({ test: true })
it('rejects if there is a JSON parse error deserialization fails', function () {
const repo = new Microcosm()

expect(repo.state.test).toBe(true)
// This is invalid
let badPatch = repo.reset("{ test: deserialize }", true)

repo.reset()
expect(badPatch).toHaveStatus('error')
})

it('preserves state if reset fails', function () {
const repo = new Microcosm()

repo.addDomain('test', {
getInitialState: () => true
})

repo.patch({ test: false })

// This is invalid
repo.reset("{ test: deserialize }", true)

expect(repo).toHaveState('test', false)
})

expect(repo.state.test).toBe(false)
})

describe('::patch', function () {

it('rejects if there is a JSON parse error deserialization fails', function () {
const repo = new Microcosm()

// This is invalid
let badPatch = repo.patch("{ test: deserialize }", true)

expect(badPatch).toHaveStatus('error')
})

})


it('can manipulate how many transactions are merged', function () {
const repo = new Microcosm({ maxHistory: 5 })
const identity = n => n
Expand Down Expand Up @@ -88,7 +134,7 @@ it('can checkout a prior state', function () {

repo.checkout(start)

expect(repo.state.number).toEqual(1)
expect(repo).toHaveState('number', 1)
})

it('it will not emit a change if state is shallowly equal', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/mutation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ it('writes to repo state', function (done) {
})

repo.push(action, true).onDone(() => {
expect(repo.state.test).toBe(true)
expect(repo).toHaveState('test', true)
done()
})
})
2 changes: 1 addition & 1 deletion test/register.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ it('returns the same state if a handler is not provided', function () {
})

return repo.push(action).onDone(function() {
expect(repo.state.test).toEqual('test')
expect(repo).toHaveState('test', 'test')
})
})

Expand Down
8 changes: 4 additions & 4 deletions test/rollbacks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ it('does not rollforward the same actions twice', function () {
b.resolve({ id: 2 })
c.resolve({ id: 3 })

expect(repo.state.messages[0].pending).not.toBeDefined()
expect(repo.state.messages[1].pending).not.toBeDefined()
expect(repo.state.messages[2].pending).not.toBeDefined()
expect(repo).toHaveState(['messages', 0, 'pending'], undefined)
expect(repo).toHaveState(['messages', 1, 'pending'], undefined)
expect(repo).toHaveState(['messages', 2, 'pending'], undefined)

expect(repo.state.messages.length).toEqual(3)
expect(repo).toHaveState(['messages', 'length'], 3)
})

it('remembers the archive point', function () {
Expand Down
15 changes: 2 additions & 13 deletions test/serialization.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ describe('deserialize', function () {
}
})

return repo.patch({}).onDone(function() {
expect(repo.state).toEqual({ fiz: true })
return repo.patch({}, true).onDone(function() {
expect(repo).toHaveState('fiz', true)
})
})

Expand All @@ -73,17 +73,6 @@ describe('deserialize', function () {
expect(answer).toEqual({ fiz: 'BUZZ' })
})

it('rejects if there is a JSON parse error deserialization fails', function () {
const repo = new Microcosm()

// This is invalid
function badPatch () {
repo.patch("{ test: deserialize }", true)
}

expect(badPatch).toThrow('Unexpected token')
})

})

describe('parents', function () {
Expand Down

0 comments on commit 630ae36

Please sign in to comment.