diff --git a/index.js b/index.js index f21bd913..86dfb433 100644 --- a/index.js +++ b/index.js @@ -137,12 +137,13 @@ Choo.prototype.start = function () { } } + this._matchRoute() + this._setCache(this.state) this._stores.forEach(function (initStore) { initStore(self.state) }) - this._matchRoute() this._tree = this._prerender(this.state) assert.ok(this._tree, 'choo.start: no valid DOM node returned for location ' + this.state.href) @@ -211,12 +212,14 @@ Choo.prototype.toString = function (location, state) { assert.equal(typeof this.state, 'object', 'choo.toString: state should be type object') var self = this + + this._matchRoute(location) + this._setCache(this.state) 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) diff --git a/test.js b/test.js index 22399a8b..91876f57 100644 --- a/test.js +++ b/test.js @@ -169,15 +169,21 @@ tape('state should include events', function (t) { }) tape('state should include params', function (t) { - t.plan(4) + t.plan(8) var app = choo() app.route('/:resource/:id/*', function (state, emit) { - t.ok(state.hasOwnProperty('params'), 'state has params property') - t.equal(state.params.resource, 'users', 'resources param is users') - t.equal(state.params.id, '1', 'id param is 1') - t.equal(state.params.wildcard, 'docs/foo.txt', 'wildcard captures what remains') + t.ok(state.hasOwnProperty('params'), 'state has params property (view)') + t.equal(state.params.resource, 'users', 'resources param is users (view)') + t.equal(state.params.id, '1', 'id param is 1 (view)') + t.equal(state.params.wildcard, 'docs/foo.txt', 'wildcard captures what remains (view)') return html`
` }) + app.use(function (state, emitter) { + t.ok(state.hasOwnProperty('params'), 'state has params property (store)') + t.equal(state.params.resource, 'users', 'resources param is users (store)') + t.equal(state.params.id, '1', 'id param is 1 (store)') + t.equal(state.params.wildcard, 'docs/foo.txt', 'wildcard captures what remains (store)') + }) app.toString('/users/1/docs/foo.txt') t.end() }) @@ -195,13 +201,17 @@ tape('state should include query', function (t) { }) tape('state should include href', function (t) { - t.plan(2) + t.plan(4) 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') + t.ok(state.hasOwnProperty('href'), 'state has href property (view)') + t.equal(state.href, '/users/1', 'href is users/1 (view)') return html`
` }) + app.use(function (state, emitter) { + t.ok(state.hasOwnProperty('href'), 'state has href property (store)') + t.equal(state.href, '/users/1', 'href is users/1 (store)') + }) app.toString('/users/1?page=2') // should ignore query t.end() })