Skip to content

Commit a871818

Browse files
authored
Match route before init stores (choojs#698)
1 parent b373c7e commit a871818

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,11 @@ Choo.prototype.start = function () {
142142
}
143143

144144
this._setCache(this.state)
145+
this._matchRoute()
145146
this._stores.forEach(function (initStore) {
146147
initStore(self.state)
147148
})
148149

149-
this._matchRoute()
150150
this._tree = this._prerender(this.state)
151151
assert.ok(this._tree, 'choo.start: no valid DOM node returned for location ' + this.state.href)
152152

@@ -220,11 +220,11 @@ Choo.prototype.toString = function (location, state) {
220220

221221
var self = this
222222
this._setCache(this.state)
223+
this._matchRoute(location)
223224
this._stores.forEach(function (initStore) {
224225
initStore(self.state)
225226
})
226227

227-
this._matchRoute(location)
228228
var html = this._prerender(this.state)
229229
assert.ok(html, 'choo.toString: no valid value returned for the route ' + location)
230230
assert(!Array.isArray(html), 'choo.toString: return value was an array for the route ' + location)

test/browser.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,27 @@ tape('state should include location on render', function (t) {
191191
app.mount(container)
192192
})
193193

194+
tape('state should include location on store init', function (t) {
195+
t.plan(6)
196+
var app = choo()
197+
var container = init('/foo/bar/file.txt?bin=baz')
198+
app.use(store)
199+
app.route('/:first/:second/*', function (state, emit) {
200+
return html`<div></div>`
201+
})
202+
app.mount(container)
203+
204+
function store (state, emit) {
205+
var params = { first: 'foo', second: 'bar', wildcard: 'file.txt' }
206+
t.equal(state.href, '/foo/bar/file.txt', 'state has href')
207+
t.equal(state.route, ':first/:second/*', 'state has route')
208+
t.ok(state.hasOwnProperty('params'), 'state has params')
209+
t.deepEqual(state.params, params, 'params match')
210+
t.ok(state.hasOwnProperty('query'), 'state has query')
211+
t.deepEqual(state.query, { bin: 'baz' }, 'query match')
212+
}
213+
})
214+
194215
tape('state should include title', function (t) {
195216
t.plan(3)
196217
document.title = 'foo'

test/node.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,26 @@ tape('state should include location on render', function (t) {
185185
t.end()
186186
})
187187

188+
tape('state should include location on store init', function (t) {
189+
t.plan(6)
190+
var app = choo()
191+
app.use(store)
192+
app.route('/:first/:second/*', function (state, emit) {
193+
return html`<div></div>`
194+
})
195+
app.toString('/foo/bar/file.txt?bin=baz')
196+
197+
function store (state, emit) {
198+
var params = { first: 'foo', second: 'bar', wildcard: 'file.txt' }
199+
t.equal(state.href, '/foo/bar/file.txt', 'state has href')
200+
t.equal(state.route, ':first/:second/*', 'state has route')
201+
t.ok(state.hasOwnProperty('params'), 'state has params')
202+
t.deepEqual(state.params, params, 'params match')
203+
t.ok(state.hasOwnProperty('query'), 'state has query')
204+
t.deepEqual(state.query, { bin: 'baz' }, 'query match')
205+
}
206+
})
207+
188208
tape('state should include cache', function (t) {
189209
t.plan(6)
190210
var app = choo()

0 commit comments

Comments
 (0)