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

Match initial route before stores are called #681

Closed
wants to merge 1 commit into from
Closed
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
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down
26 changes: 18 additions & 8 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`<div></div>`
})
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()
})
Expand All @@ -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`<div></div>`
})
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()
})
Expand Down