2016-07-03 14:19:30 -07:00
|
|
|
const test = require('tape')
|
2016-07-04 09:09:43 -07:00
|
|
|
const append = require('append-child')
|
2016-07-03 14:19:30 -07:00
|
|
|
const choo = require('../../')
|
|
|
|
|
const view = require('../../html')
|
|
|
|
|
|
|
|
|
|
test('hooks', function (t) {
|
2016-07-03 15:13:59 -07:00
|
|
|
t.plan(9)
|
2016-07-03 14:19:30 -07:00
|
|
|
|
|
|
|
|
const app = choo({
|
2016-07-03 15:13:59 -07:00
|
|
|
onError: function (err) {
|
|
|
|
|
t.equal(err.message, 'effect error', 'onError: receives err')
|
2016-07-03 14:19:30 -07:00
|
|
|
},
|
|
|
|
|
onAction: function (action, state, name, caller, createSend) {
|
|
|
|
|
if (name === 'explodes') return
|
2016-07-03 15:13:59 -07:00
|
|
|
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')
|
2016-07-10 15:04:26 +02:00
|
|
|
t.equal(caller, 'view: /', 'onAction: caller name')
|
2016-07-03 15:13:59 -07:00
|
|
|
t.equal(typeof createSend, 'function', 'onAction: createSend fn')
|
2016-07-03 14:19:30 -07:00
|
|
|
},
|
2016-07-05 13:22:22 +02:00
|
|
|
onStateChange: function (action, state, prev, createSend) {
|
2016-07-03 15:13:59 -07:00
|
|
|
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')
|
2016-07-03 14:19:30 -07:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
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()
|
2016-07-04 09:09:43 -07:00
|
|
|
t.on('end', append(tree))
|
2016-07-03 14:19:30 -07:00
|
|
|
})
|