2016-07-03 17:05:03 -07:00
|
|
|
const test = require('tape')
|
|
|
|
|
const Event = require('geval/event')
|
|
|
|
|
const proxyquire = require('proxyquire')
|
2016-07-04 09:09:43 -07:00
|
|
|
const append = require('append-child')
|
2016-07-03 17:05:03 -07:00
|
|
|
const view = require('../../html')
|
|
|
|
|
|
|
|
|
|
test('routing', function (t) {
|
|
|
|
|
t.test('history', function (t) {
|
2016-12-11 19:35:29 +01:00
|
|
|
t.plan(2)
|
2016-07-03 17:05:03 -07:00
|
|
|
|
|
|
|
|
const history = Event()
|
|
|
|
|
const choo = proxyquire('../..', {
|
|
|
|
|
'sheet-router/history': history.listen
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const app = choo()
|
|
|
|
|
|
|
|
|
|
app.model({
|
|
|
|
|
state: {
|
|
|
|
|
user: null
|
|
|
|
|
},
|
|
|
|
|
reducers: {
|
2016-12-11 19:35:29 +01:00
|
|
|
set: (state, data) => ({user: data.id})
|
2016-07-03 17:05:03 -07:00
|
|
|
},
|
|
|
|
|
effects: {
|
2016-12-11 19:35:29 +01:00
|
|
|
open: function (state, data, send, done) {
|
2016-08-27 15:24:28 -06:00
|
|
|
t.deepEqual(data, {id: 1})
|
2016-07-03 17:05:03 -07:00
|
|
|
send('set', {id: 1}, function (err) {
|
|
|
|
|
if (err) return done(err)
|
2016-12-11 19:35:29 +01:00
|
|
|
history.broadcast('/users/1')
|
2016-07-03 17:05:03 -07:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2016-12-11 19:35:29 +01:00
|
|
|
app.router({ default: '/users' }, [
|
|
|
|
|
['/users', parentView, [
|
|
|
|
|
['/:user', childView]
|
|
|
|
|
]]
|
2016-07-03 17:05:03 -07:00
|
|
|
])
|
|
|
|
|
|
|
|
|
|
const tree = app.start()
|
2016-07-04 09:09:43 -07:00
|
|
|
t.on('end', append(tree))
|
2016-07-03 17:05:03 -07:00
|
|
|
|
|
|
|
|
t.equal(tree.innerHTML.trim(), 'Open')
|
|
|
|
|
tree.onclick()
|
|
|
|
|
|
|
|
|
|
function parentView (state, prev, send) {
|
|
|
|
|
return view`
|
|
|
|
|
<button onclick=${() => send('open', {id: 1})}>
|
|
|
|
|
Open
|
|
|
|
|
</button>
|
|
|
|
|
`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function childView (state, prev, send) {
|
|
|
|
|
t.equal(state.user, 1)
|
2016-07-04 09:09:43 -07:00
|
|
|
return view`<button>${state.user}</button>`
|
2016-07-03 17:05:03 -07:00
|
|
|
}
|
|
|
|
|
})
|
2016-07-03 17:13:42 -07:00
|
|
|
|
2016-12-11 19:35:29 +01:00
|
|
|
// t.test('hash', function (t) {
|
|
|
|
|
// t.plan(1)
|
2016-07-03 17:13:42 -07:00
|
|
|
|
2016-12-11 19:35:29 +01:00
|
|
|
// resetLocation()
|
|
|
|
|
// const hash = Event()
|
|
|
|
|
// const choo = proxyquire('../..', {
|
|
|
|
|
// 'sheet-router/hash': hash.listen
|
|
|
|
|
// })
|
2016-07-03 17:13:42 -07:00
|
|
|
|
2016-12-11 19:35:29 +01:00
|
|
|
// const app = choo({hash: true})
|
2016-07-03 17:13:42 -07:00
|
|
|
|
2016-12-11 19:35:29 +01:00
|
|
|
// app.model({
|
|
|
|
|
// state: {
|
|
|
|
|
// user: null
|
|
|
|
|
// },
|
|
|
|
|
// reducers: {
|
|
|
|
|
// set: (state, data) => ({user: action.id})
|
|
|
|
|
// },
|
|
|
|
|
// effects: {
|
|
|
|
|
// open: function (state, data, send, done) {
|
|
|
|
|
// send('set', {id: 1}, function (err) {
|
|
|
|
|
// if (err) return done(err)
|
|
|
|
|
// hash.broadcast('#users/1')
|
|
|
|
|
// })
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// })
|
2016-07-03 17:13:42 -07:00
|
|
|
|
2016-12-11 19:35:29 +01:00
|
|
|
// app.router({ default: '/users' }, [
|
|
|
|
|
// ['/users', parentView, [
|
|
|
|
|
// ['/:user', childView]
|
|
|
|
|
// ]]
|
|
|
|
|
// ])
|
2016-07-03 17:13:42 -07:00
|
|
|
|
2016-12-11 19:35:29 +01:00
|
|
|
// const tree = app.start()
|
|
|
|
|
// t.on('end', append(tree))
|
2016-07-03 17:13:42 -07:00
|
|
|
|
2016-12-11 19:35:29 +01:00
|
|
|
// tree.onclick()
|
2016-07-03 17:13:42 -07:00
|
|
|
|
2016-12-11 19:35:29 +01:00
|
|
|
// function parentView (state, prev, send) {
|
|
|
|
|
// return view`
|
|
|
|
|
// <button onclick=${() => send('open', {id: 1})}>
|
|
|
|
|
// Open
|
|
|
|
|
// </button>
|
|
|
|
|
// `
|
|
|
|
|
// }
|
2016-07-03 17:13:42 -07:00
|
|
|
|
2016-12-11 19:35:29 +01:00
|
|
|
// function childView (state, prev, send) {
|
|
|
|
|
// t.equal(state.user, 1)
|
|
|
|
|
// return view`<p>${state.user}</p>`
|
|
|
|
|
// }
|
|
|
|
|
// })
|
2016-07-03 17:18:30 -07:00
|
|
|
|
|
|
|
|
t.test('disabling history', function (t) {
|
|
|
|
|
t.plan(1)
|
|
|
|
|
|
2016-12-11 19:35:29 +01:00
|
|
|
resetLocation()
|
2016-07-03 17:18:30 -07:00
|
|
|
const choo = proxyquire('../..', {
|
|
|
|
|
'sheet-router/history': () => t.fail('history listener attached')
|
|
|
|
|
})
|
|
|
|
|
|
2016-12-11 19:35:29 +01:00
|
|
|
const app = choo({ history: false })
|
2016-07-03 17:18:30 -07:00
|
|
|
|
2016-12-11 19:35:29 +01:00
|
|
|
app.router('/', [
|
|
|
|
|
['/', function () {
|
2016-07-03 17:18:30 -07:00
|
|
|
t.pass('rendered')
|
2016-12-22 06:55:36 -05:00
|
|
|
return document.createElement('div')
|
2016-12-11 19:35:29 +01:00
|
|
|
}]
|
2016-07-03 17:18:30 -07:00
|
|
|
])
|
|
|
|
|
|
2016-12-11 19:35:29 +01:00
|
|
|
app.start()
|
2016-07-03 17:18:30 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.test('disabling href', function (t) {
|
|
|
|
|
t.plan(1)
|
|
|
|
|
|
|
|
|
|
const choo = proxyquire('../..', {
|
|
|
|
|
'sheet-router/href': () => t.fail('href listener attached')
|
|
|
|
|
})
|
|
|
|
|
|
2016-12-11 19:35:29 +01:00
|
|
|
const app = choo({ href: false })
|
2016-12-22 06:55:36 -05:00
|
|
|
app.router(['/', function () {
|
|
|
|
|
t.pass('rendered')
|
|
|
|
|
return document.createElement('div')
|
|
|
|
|
}])
|
2016-12-11 19:35:29 +01:00
|
|
|
app.start()
|
2016-07-03 17:18:30 -07:00
|
|
|
})
|
2016-07-03 17:23:11 -07:00
|
|
|
|
|
|
|
|
t.test('viewless nesting', function (t) {
|
|
|
|
|
t.plan(1)
|
|
|
|
|
|
|
|
|
|
const choo = require('../..')
|
|
|
|
|
const app = choo()
|
|
|
|
|
|
2016-12-11 19:35:29 +01:00
|
|
|
app.router({ default: '/users/123' }, [
|
|
|
|
|
['/users', [
|
|
|
|
|
['/:user', function (state) {
|
2016-12-22 06:55:36 -05:00
|
|
|
t.deepEqual(state.location.params, {user: '123'})
|
|
|
|
|
return document.createElement('div')
|
2016-12-11 19:35:29 +01:00
|
|
|
}]
|
|
|
|
|
]]
|
2016-07-03 17:23:11 -07:00
|
|
|
])
|
|
|
|
|
|
|
|
|
|
app.start()
|
|
|
|
|
})
|
2016-07-03 17:05:03 -07:00
|
|
|
})
|
2016-12-11 19:35:29 +01:00
|
|
|
|
|
|
|
|
function resetLocation () {
|
|
|
|
|
window.history.pushState({}, null, '/')
|
|
|
|
|
}
|