Fix location missing on store init (#695)

This commit is contained in:
Carl Törnqvist
2019-04-02 09:27:06 +02:00
committed by GitHub
parent 5921d27648
commit 6a2f14c292
2 changed files with 30 additions and 7 deletions
+1 -1
View File
@@ -212,11 +212,11 @@ Choo.prototype.toString = function (location, state) {
var self = this var self = this
this._setCache(this.state) this._setCache(this.state)
this._matchRoute(location)
this._stores.forEach(function (initStore) { this._stores.forEach(function (initStore) {
initStore(self.state) initStore(self.state)
}) })
this._matchRoute(location)
var html = this._prerender(this.state) var html = this._prerender(this.state)
assert.ok(html, 'choo.toString: no valid value returned for the route ' + location) 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) assert(!Array.isArray(html), 'choo.toString: return value was an array for the route ' + location)
+29 -6
View File
@@ -194,18 +194,41 @@ tape('state should include query', function (t) {
t.end() t.end()
}) })
tape('state should include href', function (t) { tape('state should include location on render', function (t) {
t.plan(2) t.plan(6)
var app = choo() var app = choo()
app.route('/:resource/:id', function (state, emit) { app.route('/:foo', function (state, emit) {
t.ok(state.hasOwnProperty('href'), 'state has href property') t.equal(state.href, '/foo', 'state has href')
t.equal(state.href, '/users/1', 'href is users/1') 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>` return html`<div></div>`
}) })
app.toString('/users/1?page=2') // should ignore query app.toString('/foo?bar=baz')
t.end() 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 // TODO: Implement this using jsdom, as this only works when window is present
tape.skip('state should include title', function (t) {}) tape.skip('state should include title', function (t) {})