Files
buuh/tests/browser/hooks.js
T
Ben Drucker e3f715f71b Clean up all DOM nodes when tests end
As the test suite grows it's bad for debugging to leak tons of elements
onto document.body. In order to concisely append/remove on the tests'
"end" event, we need to make sure the root tagName doesn't change. If it
does, the `tree` returned from `app.start` will no longer be the root
node and the removal fn will throw.
2016-07-04 16:19:17 -07:00

57 lines
1.5 KiB
JavaScript

const test = require('tape')
const append = require('append-child')
const choo = require('../../')
const view = require('../../html')
test('hooks', function (t) {
t.plan(9)
const app = choo({
onError: function (err) {
t.equal(err.message, 'effect error', 'onError: receives err')
},
onAction: function (action, state, name, caller, createSend) {
if (name === 'explodes') return
t.deepEqual(action, {foo: 'bar'}, 'onAction: action data')
t.equal(state.clicks, 0, 'onAction: current state: 0 clicks')
t.equal(name, 'click', 'onAction: action name')
t.equal(caller, '/', 'onAction: caller name')
t.equal(typeof createSend, 'function', 'onAction: createSend fn')
},
onState: function (action, state, prev, createSend) {
t.deepEqual(action, {foo: 'bar'}, 'onState: action data')
t.deepEqual(state.clicks, 1, 'onState: new state: 1 clicks')
t.deepEqual(prev.clicks, 0, 'onState: prev state: 0 clicks')
}
})
app.model({
state: {
clicks: 0
},
reducers: {
click: (action, state) => ({clicks: state.clicks + 1})
},
effects: {
explodes: (action, state, send, done) => {
setTimeout(() => done(new Error('effect error')), 5)
}
}
})
var sent = false
app.router((route) => [
route('/', function (state, prev, send) {
if (!sent) {
send('click', {foo: 'bar'})
send('explodes')
}
sent = true
return view`<span></span>`
})
])
const tree = app.start()
t.on('end', append(tree))
})