Immutable state on SSR (toString) (#649)

* Init stores with state

* Reset state.components on toString
This commit is contained in:
Carl Törnqvist
2019-06-12 15:13:33 +02:00
committed by GitHub
parent a871818d32
commit 842b7aaa5c
2 changed files with 58 additions and 21 deletions
+34
View File
@@ -224,3 +224,37 @@ tape('state should include cache', function (t) {
t.equal(arg, 'arg', 'constructor args were forwarded')
}
})
tape('state should not mutate on toString', function (t) {
t.plan(6)
var app = choo()
app.use(store)
var routes = ['foo', 'bar']
var states = routes.map(function (route) {
var state = {}
app.route(`/${route}`, view)
app.toString(`/${route}`, state)
return state
})
for (var i = 0, len = routes.length; i < len; i++) {
t.equal(states[i].test, routes[i], 'store was used')
t.equal(states[i].title, routes[i], 'title was added to state')
}
function store (state, emitter) {
state.test = null
emitter.on('test', function (str) {
t.equal(state.test, null, 'state has been reset')
state.test = str
})
}
function view (state, emit) {
emit('test', state.route)
emit(state.events.DOMTITLECHANGE, state.route)
return html`<body>Hello ${state.route}</body>`
}
})