Tests use hyperscript instead of manually constructing nodes

This commit is contained in:
Barney Carroll 2021-04-14 17:05:14 +01:00 committed by Stephan Hoyer
parent 5502570b16
commit 31d2ed4be8
27 changed files with 1515 additions and 1841 deletions

View file

@ -3,6 +3,8 @@
var o = require("ospec")
var domMock = require("../../test-utils/domMock")
var vdom = require("../../render/render")
var m = require("../../render/hyperscript")
m.trust = require("../../render/trust")
o.spec("updateHTML", function() {
var $window, root, render
@ -13,22 +15,22 @@ o.spec("updateHTML", function() {
})
o("updates html", function() {
var vnode = {tag: "<", children: "a"}
var updated = {tag: "<", children: "b"}
var vnode = m.trust("a")
var updated = m.trust("b")
render(root, [vnode])
render(root, [updated])
render(root, vnode)
render(root, updated)
o(updated.dom).equals(root.firstChild)
o(updated.domSize).equals(1)
o(updated.dom.nodeValue).equals("b")
})
o("adds html", function() {
var vnode = {tag: "<", children: ""}
var updated = {tag: "<", children: "<a></a><b></b>"}
var vnode = m.trust("")
var updated = m.trust("<a></a><b></b>")
render(root, [vnode])
render(root, [updated])
render(root, vnode)
render(root, updated)
o(updated.domSize).equals(2)
o(updated.dom).equals(root.firstChild)
@ -37,11 +39,11 @@ o.spec("updateHTML", function() {
o(root.childNodes[1].nodeName).equals("B")
})
o("removes html", function() {
var vnode = {tag: "<", children: "<a></a><b></b>"}
var updated = {tag: "<", children: ""}
var vnode = m.trust("<a></a><b></b>")
var updated = m.trust("")
render(root, [vnode])
render(root, [updated])
render(root, vnode)
render(root, updated)
o(updated.dom).equals(null)
o(updated.domSize).equals(0)
@ -58,14 +60,14 @@ o.spec("updateHTML", function() {
return result
}
o("updates the dom correctly with a contenteditable parent", function() {
var div = {tag: "div", attrs: {contenteditable: true}, children: [{tag: "<", children: "<a></a>"}]}
var div = m("div", {contenteditable: true}, m.trust("<a></a>"))
render(root, div)
o(childKeysOf(div.dom, "nodeName")).deepEquals(["A"])
})
o("updates dom with multiple text children", function() {
var vnode = [{tag: "#", children: "a"}, {tag: "<", children: "<a></a>"}, {tag: "<", children: "<b></b>"}]
var replacement = [{tag: "#", children: "a"}, {tag: "<", children: "<c></c>"}, {tag: "<", children: "<d></d>"}]
var vnode = ["a", m.trust("<a></a>"), m.trust("<b></b>")]
var replacement = ["a", m.trust("<c></c>"), m.trust("<d></d>")]
render(root, vnode)
render(root, replacement)
@ -74,12 +76,12 @@ o.spec("updateHTML", function() {
})
o("updates dom with multiple text children in other parents", function() {
var vnode = [
{tag: "div", attrs: {}, children: [{tag: "#", children: "a"}, {tag: "<", children: "<a></a>"}]},
{tag: "div", attrs: {}, children: [{tag: "#", children: "b"}, {tag: "<", children: "<b></b>"}]},
m("div", "a", m.trust("<a></a>")),
m("div", "b", m.trust("<b></b>")),
]
var replacement = [
{tag: "div", attrs: {}, children: [{tag: "#", children: "c"}, {tag: "<", children: "<c></c>"}]},
{tag: "div", attrs: {}, children: [{tag: "#", children: "d"}, {tag: "<", children: "<d></d>"}]},
m("div", "c", m.trust("<c></c>")),
m("div", "d", m.trust("<d></d>")),
]
render(root, vnode)
@ -93,20 +95,20 @@ o.spec("updateHTML", function() {
})
o("correctly diffs if followed by another trusted vnode", function() {
render(root, [
{tag: "<", children: "<span>A</span>"},
{tag: "<", children: "<span>A</span>"},
m.trust("<span>A</span>"),
m.trust("<span>A</span>"),
])
o(childKeysOf(root, "nodeName")).deepEquals(["SPAN", "SPAN"])
o(childKeysOf(root, "firstChild.nodeValue")).deepEquals(["A", "A"])
render(root, [
{tag: "<", children: "<span>B</span>"},
{tag: "<", children: "<span>A</span>"},
m.trust("<span>B</span>"),
m.trust("<span>A</span>"),
])
o(childKeysOf(root, "nodeName")).deepEquals(["SPAN", "SPAN"])
o(childKeysOf(root, "firstChild.nodeValue")).deepEquals(["B", "A"])
render(root, [
{tag: "<", children: "<span>B</span>"},
{tag: "<", children: "<span>B</span>"},
m.trust("<span>B</span>"),
m.trust("<span>B</span>"),
])
o(childKeysOf(root, "nodeName")).deepEquals(["SPAN", "SPAN"])
o(childKeysOf(root, "firstChild.nodeValue")).deepEquals(["B", "B"])