* location: change url on location:setLocation - [ ] don't break hashing - [ ] allow not changing the url fixup! move fns around fixup! add search string * app.start: clean * walk: add * location: update arg calls * fixup! location: update * uri-wrap: fix thunking * router: update to latest sheet-router (#239) * router: update to latest sheet-router * tests: fix for latest version * tests: fix SSR * tests: fix history * tests: spruce up * docs: update example * examples: update * deps: bump sheet-router * 4.0.0-0 * chore(changelog): 4.0.0 (#211) * feat(api:) arg order (#268) * s/data, state/state, data/ * feat(api): swap arguments * fix(href): fix routing (#271) * feat(http): remove (#269) * feat(router): enable hash routing (#273) * deps: fix mount * 4.0.0-1 * feat(mount): copy {script,link} tags * 4.0.0-2 * fix(mount): forEach -> for Lol can't use forEach * fix(router): use state.location.href (#282) * fix(mount): use deep node clone * 4.0.0-3 * fix(deps): remove hash-match * 4.0.0-4 * fix(mount): return node * 4.0.0-5 * fix(router): check if a hash is a valid selector (#339) * 4.0.0-6 * fix(router): pass params on newstate (#343) * 4.0.0-7 * feat(docs): update for 4.0.0 (#320) * feat(docs): update for 4.0.0 * docs: update router example in readme (#337) * chore(changelog): update for v4 (#351)
184 lines
3.8 KiB
JavaScript
184 lines
3.8 KiB
JavaScript
const test = require('tape')
|
|
const Event = require('geval/event')
|
|
const proxyquire = require('proxyquire')
|
|
const append = require('append-child')
|
|
const view = require('../../html')
|
|
|
|
test('routing', function (t) {
|
|
t.test('history', function (t) {
|
|
t.plan(2)
|
|
|
|
const history = Event()
|
|
const choo = proxyquire('../..', {
|
|
'sheet-router/history': history.listen
|
|
})
|
|
|
|
const app = choo()
|
|
|
|
app.model({
|
|
state: {
|
|
user: null
|
|
},
|
|
reducers: {
|
|
set: (state, data) => ({user: data.id})
|
|
},
|
|
effects: {
|
|
open: function (state, data, send, done) {
|
|
t.deepEqual(data, {id: 1})
|
|
send('set', {id: 1}, function (err) {
|
|
if (err) return done(err)
|
|
history.broadcast('/users/1')
|
|
})
|
|
}
|
|
}
|
|
})
|
|
|
|
app.router({ default: '/users' }, [
|
|
['/users', parentView, [
|
|
['/:user', childView]
|
|
]]
|
|
])
|
|
|
|
const tree = app.start()
|
|
t.on('end', append(tree))
|
|
|
|
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)
|
|
return view`<button>${state.user}</button>`
|
|
}
|
|
})
|
|
|
|
// t.test('hash', function (t) {
|
|
// t.plan(1)
|
|
|
|
// resetLocation()
|
|
// const hash = Event()
|
|
// const choo = proxyquire('../..', {
|
|
// 'sheet-router/hash': hash.listen
|
|
// })
|
|
|
|
// const app = choo({hash: true})
|
|
|
|
// 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')
|
|
// })
|
|
// }
|
|
// }
|
|
// })
|
|
|
|
// app.router({ default: '/users' }, [
|
|
// ['/users', parentView, [
|
|
// ['/:user', childView]
|
|
// ]]
|
|
// ])
|
|
|
|
// const tree = app.start()
|
|
// t.on('end', append(tree))
|
|
|
|
// 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)
|
|
// return view`<p>${state.user}</p>`
|
|
// }
|
|
// })
|
|
|
|
t.test('disabling history', function (t) {
|
|
t.plan(1)
|
|
|
|
resetLocation()
|
|
const choo = proxyquire('../..', {
|
|
'sheet-router/history': () => t.fail('history listener attached')
|
|
})
|
|
|
|
const app = choo({ history: false })
|
|
|
|
app.router('/', [
|
|
['/', function () {
|
|
t.pass('rendered')
|
|
}]
|
|
])
|
|
|
|
app.start()
|
|
})
|
|
|
|
t.test('disabling href', function (t) {
|
|
t.plan(1)
|
|
|
|
const choo = proxyquire('../..', {
|
|
'sheet-router/href': () => t.fail('href listener attached')
|
|
})
|
|
|
|
const app = choo({ href: false })
|
|
app.router(['/', () => t.pass('rendered')])
|
|
app.start()
|
|
})
|
|
|
|
t.test('viewless nesting', function (t) {
|
|
t.plan(1)
|
|
|
|
const choo = require('../..')
|
|
const app = choo()
|
|
|
|
app.router({ default: '/users/123' }, [
|
|
['/users', [
|
|
['/:user', function (state) {
|
|
t.deepEqual(state.params, {user: '123'})
|
|
}]
|
|
]]
|
|
])
|
|
|
|
app.start()
|
|
})
|
|
|
|
t.test('prev.params always exists', function (t) {
|
|
t.plan(1)
|
|
|
|
const choo = require('../..')
|
|
const app = choo()
|
|
|
|
app.router('/users/123', (route) => [
|
|
route('/users', [
|
|
route('/:user', function (state, prev) {
|
|
t.ok(prev.params)
|
|
})
|
|
])
|
|
])
|
|
|
|
app.start()
|
|
})
|
|
})
|
|
|
|
function resetLocation () {
|
|
window.history.pushState({}, null, '/')
|
|
}
|