Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add v6 tests (WIP) #528

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
"bundle-collapser": "^1.2.1",
"dependency-check": "^2.8.0",
"discify": "^1.6.0",
"jsdom": "11.0.0",
"jsdom-global": "3.0.2",
"node-zopfli": "^2.0.2",
"pretty-bytes-cli": "^2.0.0",
"spok": "^0.8.1",
Expand Down
110 changes: 108 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
var tape = require('tape')
var Nanobus = require('nanobus')
require('jsdom-global')(null, { url: 'http://localhost/' })
window.localStorage = window.localStorage || {} // See: yoshuawuyts/nanotiming#7
window.requestAnimationFrame = window.requestAnimationFrame || function (cb) { process.nextTick(cb) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks a little tricky; perhaps using https://github.com/juliangruber/tape-run might make it easier by testing in an actual browser env?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh heck yeah, that'd make it easier. At least 1 other test simply wouldn't work otherwise too. Glad to hear you're good using that.


var html = require('./html')
var choo = require('./')

tape('should render on the server', function (t) {
// TODO: See shuhei/pelo#7
tape.skip('should render on the server', function (t) {
var app = choo()
app.route('/', function (state, emit) {
return html`
Expand All @@ -12,6 +17,107 @@ tape('should render on the server', function (t) {
})
var res = app.toString('/')
var exp = '<p>Hello filthy planet</p>'
t.equal(res.toString(), exp, 'result was OK')
t.equal(res, exp, 'result was OK')
t.end()
})

tape('enables history and href by defaut', function (t) {
var app = choo()
t.true(app._historyEnabled, 'history enabled')
t.true(app._hrefEnabled, 'href enabled')
t.end()
})

// TODO: I expect nanohref is cancelling because it's node
tape.skip('clicking <a> triggers pushstate', function (t) {
t.plan(1)
var app = choo()
app.route('/', function (state, emit) {
return html`
<a href="/elsewhere" onclick=${() => console.log('called yo')}>Elsewhere</a>
`
})
app.use(function (state, emitter) {
emitter.on('pushState', function () {
t.pass('pushstate emitted')
})
})
var tree = app.start()
document.body.appendChild(tree)
tree.click()
t.end()
})

tape('route handler is passed state and emit', function (t) {
t.plan(2)
var app = choo()
app.route('/', function (state, emit) {
t.equal(typeof state, 'object', 'state is an object')
t.equal(typeof emit, 'function', 'emit is a function')
return html`<div></div>`
})
app.start()
t.end()
})

// TODO: Need to pause between emits to give handlers a chance to assert
tape('state includes current route', function (t) {
t.plan(3)
var app = choo()

app.route('/', function (state, emit) {
t.equal(state.route, '/', 'matches empty route')
return html`<div>empty</div>`
})
app.route('/elsewhere', function (state, emit) {
t.equal(state.route, '/elsewhere', 'matches named route')
return html`<div>elsewhere</div>`
})
app.route('/with/:param', function (state, emit) {
t.equal(state.route, '/with/:param', 'matches route with param')
return html`<div>with param</div>`
})
app.start()

var PUSHSTATE = app.state.events.PUSHSTATE
tickSeries([
function () { app.emitter.emit(PUSHSTATE, '/elsewhere') },
function () { app.emitter.emit(PUSHSTATE, '/with/test') },
function () { t.end() }
])
})

// Execute an array of functions in sequential ticks
function tickSeries (fns) {
const fn = fns.shift()
process.nextTick(function () {
fn()
if (fns.length) {
tickSeries(fns)
}
})
}

tape('use is passed state and emitter', function (t) {
t.plan(2)
var app = choo()
app.use(function (state, emitter) {
t.equal(typeof state, 'object', 'state is an object')
t.true(emitter instanceof Nanobus, 'emitter is Nanobus instance')
})
t.end()
})

// TODO: jsdom doesn't support changing the search
tape.skip('state includes query', function (t) {
t.plan(1)
var app = choo()
app.route('/', function (state, emit) {
const expected = { foo: 'bar' }
t.deepEqual(state.query, expected, 'state includes query')
return html`<div></div>`
})
window.location.search = '?foo=bar'
app.start()
t.end()
})