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

Fix location missing on store init #695

Merged
merged 1 commit into from
Apr 2, 2019
Merged
Show file tree
Hide file tree
Changes from all 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: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,11 @@ Choo.prototype.toString = function (location, state) {

var self = this
this._setCache(this.state)
this._matchRoute(location)
this._stores.forEach(function (initStore) {
initStore(self.state)
})

this._matchRoute(location)
var html = this._prerender(this.state)
assert.ok(html, 'choo.toString: no valid value returned for the route ' + location)
assert(!Array.isArray(html), 'choo.toString: return value was an array for the route ' + location)
Expand Down
35 changes: 29 additions & 6 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,18 +194,41 @@ tape('state should include query', function (t) {
t.end()
})

tape('state should include href', function (t) {
t.plan(2)
tape('state should include location on render', function (t) {
t.plan(6)
var app = choo()
app.route('/:resource/:id', function (state, emit) {
t.ok(state.hasOwnProperty('href'), 'state has href property')
t.equal(state.href, '/users/1', 'href is users/1')
app.route('/:foo', function (state, emit) {
t.equal(state.href, '/foo', 'state has href')
t.equal(state.route, ':foo', 'state has route')
t.ok(state.hasOwnProperty('params'), 'state has params')
t.deepEqual(state.params, { foo: 'foo' }, 'params match')
t.ok(state.hasOwnProperty('query'), 'state has query')
t.deepEqual(state.query, { bar: 'baz' }, 'query match')
return html`<div></div>`
})
app.toString('/users/1?page=2') // should ignore query
app.toString('/foo?bar=baz')
t.end()
})

tape('state should include location on store init', function (t) {
t.plan(6)
var app = choo()
app.use(store)
app.route('/:foo', function (state, emit) {
return html`<div></div>`
})
app.toString('/foo?bar=baz')

function store (state, emit) {
t.equal(state.href, '/foo', 'state has href')
t.equal(state.route, ':foo', 'state has route')
t.ok(state.hasOwnProperty('params'), 'state has params')
t.deepEqual(state.params, { foo: 'foo' }, 'params match')
t.ok(state.hasOwnProperty('query'), 'state has query')
t.deepEqual(state.query, { bar: 'baz' }, 'query match')
}
})

// TODO: Implement this using jsdom, as this only works when window is present
tape.skip('state should include title', function (t) {})

Expand Down