Add tests and fixes for rehydration

* Barracks must get an initial state or state.location is undefined
* Replaces document.addEventListener w/ pkg handling existing ready doc
This commit is contained in:
Ben Drucker
2016-07-04 16:19:12 -07:00
parent e2f0c5429c
commit e0783320f7
3 changed files with 32 additions and 2 deletions
+3 -2
View File
@@ -1,6 +1,7 @@
const history = require('sheet-router/history')
const sheetRouter = require('sheet-router')
const document = require('global/document')
const onReady = require('document-ready')
const href = require('sheet-router/href')
const hash = require('sheet-router/hash')
const hashMatch = require('hash-match')
@@ -61,14 +62,14 @@ function choo (opts) {
_store.model(appInit(startOpts))
const createSend = _store.start(startOpts)
_router = start._router = createRouter(_defaultRoute, _routes, createSend)
const state = _store.state()
const state = _store.state({state: {}})
if (!selector) {
const tree = _router(state.location.pathname, state)
_rootNode = tree
return tree
} else {
document.addEventListener('DOMContentLoaded', function (event) {
onReady(function onReady () {
const oldTree = document.querySelector(selector)
assert.ok(oldTree, 'could not query selector: ' + selector)
const newTree = _router(state.location.pathname, state)
+1
View File
@@ -25,6 +25,7 @@
"license": "MIT",
"dependencies": {
"barracks": "^7.0.3",
"document-ready": "~1.0.1",
"global": "^4.3.0",
"hash-match": "^1.0.2",
"sheet-router": "^3.1.0",
+28
View File
@@ -0,0 +1,28 @@
const test = require('tape')
const onReady = require('document-ready')
const choo = require('../../')
const view = require('../../html')
test('rehydration', function (t) {
t.plan(2)
const app = choo()
app.router((route) => [
route('/', function (state, prev, send) {
return view`<div id="app-root" onclick=${() => send('test')}>Hello world!</span>`
})
])
var node = document.createElement('div')
node.innerHTML = app.toString('/')
node = node.childNodes[0]
document.body.appendChild(node)
app.start('#app-root')
onReady(function () {
t.equal(node.innerHTML, 'Hello world!', 'same content')
t.equal(typeof node.onclick, 'function', 'attaches dom listeners')
})
})