Add a routing test using proxyquire to short-circuit history handlers
* proxyquire (pq) replaces choo's sheet-router/history with a mock * pq-universal replaces pq w/ pqify in the browser * magic!
This commit is contained in:
+6
-2
@@ -5,8 +5,8 @@
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"deps": "dependency-check . && dependency-check . --extra --no-dev -i xhr",
|
||||
"test:electron": "browserify tests/**/*.js -t es2020 | tape-run",
|
||||
"test:cov": "browserify tests/**/*.js -t es2020 -p tape-istanbul/plugin | tape-run | tape-istanbul && istanbul report",
|
||||
"test:electron": "browserify tests/**/*.js -t es2020 -p proxyquire-universal | tape-run",
|
||||
"test:cov": "browserify tests/**/*.js -t es2020 -p proxyquire-universal -p tape-istanbul/plugin | tape-run | tape-istanbul && istanbul report",
|
||||
"test:server": "standard && npm run deps && NODE_ENV=test node tests/server/*",
|
||||
"test:browser": "standard && npm run deps && NODE_ENV=test zuul tests/browser/*",
|
||||
"test:browser:local": "standard && npm run deps && NODE_ENV=test zuul --local 8080 -- tests/browser/*",
|
||||
@@ -40,10 +40,14 @@
|
||||
"bundle-collapser": "^1.2.1",
|
||||
"dependency-check": "^2.5.1",
|
||||
"es2020": "^1.0.1",
|
||||
"geval": "~2.1.1",
|
||||
"insert-css": "^0.2.0",
|
||||
"istanbul": "^0.4.4",
|
||||
"karma-sauce-launcher": "^1.0.0",
|
||||
"min-document": "~2.18.0",
|
||||
"proxyquire": "~1.7.10",
|
||||
"proxyquire-universal": "~1.0.8",
|
||||
"proxyquireify": "~3.2.0",
|
||||
"server-router": "^2.1.0",
|
||||
"sheetify": "^5.0.0",
|
||||
"standard": "^7.1.0",
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
const test = require('tape')
|
||||
const Event = require('geval/event')
|
||||
const proxyquire = require('proxyquire')
|
||||
const view = require('../../html')
|
||||
|
||||
test('routing', function (t) {
|
||||
t.test('history', function (t) {
|
||||
t.plan(3)
|
||||
|
||||
const history = Event()
|
||||
const choo = proxyquire('../..', {
|
||||
'sheet-router/history': history.listen
|
||||
})
|
||||
|
||||
const app = choo()
|
||||
|
||||
app.model({
|
||||
state: {
|
||||
user: null
|
||||
},
|
||||
reducers: {
|
||||
set: (action, state) => ({user: action.id})
|
||||
},
|
||||
effects: {
|
||||
open: function (action, state, send, done) {
|
||||
t.deepEqual(action, {id: 1})
|
||||
send('set', {id: 1}, function (err) {
|
||||
if (err) return done(err)
|
||||
history.broadcast('https://foo.com/users/1')
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
app.router('/users', (route) => [
|
||||
route('/users', parentView, [
|
||||
route('/:user', childView)
|
||||
])
|
||||
])
|
||||
|
||||
const tree = app.start()
|
||||
document.body.appendChild(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`<div>${state.user}</div>`
|
||||
}
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user