Skip to content

Commit

Permalink
Remove root property in favor of container as 2nd argument. Close #410.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgebucaran committed Oct 10, 2017
1 parent 54de0fd commit 87efaf8
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 134 deletions.
2 changes: 1 addition & 1 deletion docs/hydration.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ Hyperapp works transparently with SSR and pre-rendered HTML, enabling SEO optimi
</html>
```

Then instead of throwing away the server-rendered markdown, we'll walk through your DOM tree and turn nodes into an interactive application. The default [root](/docs/root.md) is `document.body`, but you can specify another if you have multiple apps on the same page.
Then instead of throwing away the server-rendered markdown, we'll turn your DOM nodes into an interactive application.
11 changes: 5 additions & 6 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { h } from "./h"

export function app(props) {
export function app(props, container) {
var root = (container = container || document.body).children[0]
var node = elementToNode(root, [].map)
var callbacks = []
var root = props.root || document.body
var element = root.children[0]
var node = elementToNode(element, [].map)
var skipRender
var globalState
var globalActions
Expand All @@ -21,9 +20,9 @@ export function app(props) {

function render() {
flush(
(element = patch(
(root = patch(
container,
root,
element,
node,
(node = props.view(globalState, globalActions)),
(skipRender = !skipRender)
Expand Down
99 changes: 99 additions & 0 deletions test/container.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { h, app } from "../src"

window.requestAnimationFrame = setTimeout

beforeEach(() => {
document.body.innerHTML = ""
})

test("container", done => {
document.body.innerHTML = "<main></main>"
app(
{
view: state =>
h(
"div",
{
oncreate() {
expect(document.body.innerHTML).toBe("<main><div>foo</div></main>")
done()
}
},
"foo"
)
},
document.body.firstChild
)
})

test("nested container", done => {
document.body.innerHTML = "<main><section></section><div></div></main>"

app(
{
view: state =>
h(
"p",
{
oncreate() {
expect(document.body.innerHTML).toBe(
`<main><section></section><div><p>foo</p></div></main>`
)
done()
}
},
"foo"
)
},
document.body.firstChild.lastChild
)
})

test("container with mutated host", done => {
document.body.innerHTML = "<main><div></div></main>"

const host = document.body.firstChild
const container = host.firstChild

app(
{
view: (state, actions) =>
h(
"p",
{
oncreate() {
expect(document.body.innerHTML).toBe(
`<main><div><p>foo</p></div></main>`
)

host.insertBefore(
document.createElement("header"),
host.firstChild
)
host.appendChild(document.createElement("footer"))

actions.bar()
},
onupdate() {
expect(document.body.innerHTML).toBe(
`<main><header></header><div><p>bar</p></div><footer></footer></main>`
)
done()
}
},
state.value
),
state: {
value: "foo"
},
actions: {
bar(state) {
return {
value: "bar"
}
}
}
},
container
)
})
1 change: 0 additions & 1 deletion test/dom.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,6 @@ test("event bubling", done => {
function testTreeSegue(name, trees) {
test(name, done => {
app({
root: document.body,
view: (state, actions) =>
h(
"main",
Expand Down
36 changes: 19 additions & 17 deletions test/hydration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,38 @@ beforeEach(() => {
})

testHydration(
"hydrate without root",
"hydrate without container",
`<main><p>foo</p></main>`,
[h("p", {}, "foo")],
null
)

testHydration(
"hydrate with root",
"hydrate with container",
`<div id="app"><main><p>foo</p></main></div>`,
[h("p", {}, "foo")],
"app"
)

function testHydration(name, ssrBody, children, withRoot) {
function testHydration(name, ssrBody, children, container) {
test(name, done => {
document.body.innerHTML = ssrBody

app({
root: withRoot && document.getElementById(withRoot),
view: state =>
h(
"main",
{
onupdate() {
expect(document.body.innerHTML).toBe(ssrBody)
done()
}
},
children
)
})
app(
{
view: state =>
h(
"main",
{
onupdate() {
expect(document.body.innerHTML).toBe(ssrBody)
done()
}
},
children
)
},
container && document.getElementById(container)
)
})
}
109 changes: 0 additions & 109 deletions test/root.test.js

This file was deleted.

0 comments on commit 87efaf8

Please sign in to comment.