Address review comments, linter and build concerns

This commit is contained in:
Pierre-Yves 2022-06-09 15:12:08 +02:00 committed by Pierre-Yves Gérardy
parent 3fd82e6359
commit 665578060e
5 changed files with 224 additions and 208 deletions

View file

@ -1,176 +1,178 @@
var o = require("ospec")
var components = require("../../test-utils/components")
var domMock = require("../../test-utils/domMock")
var vdom = require("../../render/render")
var m = require("../../render/hyperscript")
var fragment = require("../../render/fragment")
var domFor = require('../../render/dom-for').domFor
"use strict"
const o = require("ospec")
const components = require("../../test-utils/components")
const domMock = require("../../test-utils/domMock")
const vdom = require("../../render/render")
const m = require("../../render/hyperscript")
const fragment = require("../../render/fragment")
const domFor = require("../../render/dom-for").domFor
o.spec("domFor(vnode)", function() {
var $window, root, render
let $window, root, render
o.beforeEach(function() {
$window = domMock()
root = $window.document.createElement("div")
render = vdom($window)
})
o('works for simple vnodes', function() {
render(root, m('div', {oncreate(vnode){
let n = 0
for (const dom of domFor(vnode)) {
o(dom).equals(root.firstChild)
o(++n).equals(1)
}
}}))
})
o('works for fragments', function () {
render(root, fragment({
oncreate(vnode){
let n = 0
for (const dom of domFor(vnode)) {
o(dom).equals(root.childNodes[n])
n++
}
o(n).equals(2)
}
}, [
m('a'),
m('b')
]))
})
o('works in fragments with children that have delayed removal', function() {
function oncreate(vnode){
o(root.childNodes.length).equals(3)
o(root.childNodes[0].nodeName).equals('A')
o(root.childNodes[1].nodeName).equals('B')
o(root.childNodes[2].nodeName).equals('C')
o("works for simple vnodes", function() {
render(root, m("div", {oncreate(vnode){
let n = 0
for (const dom of domFor(vnode)) {
o(dom).equals(root.firstChild)
o(++n).equals(1)
}
}}))
})
o("works for fragments", function () {
render(root, fragment({
oncreate(vnode){
let n = 0
for (const dom of domFor(vnode)) {
o(dom).equals(root.childNodes[n])
n++
}
o(n).equals(2)
}
}, [
m("a"),
m("b")
]))
})
o("works in fragments with children that have delayed removal", function() {
function oncreate(vnode){
o(root.childNodes.length).equals(3)
o(root.childNodes[0].nodeName).equals("A")
o(root.childNodes[1].nodeName).equals("B")
o(root.childNodes[2].nodeName).equals("C")
const iter = domFor(vnode)
o(iter.next()).deepEquals({done:false, value: root.childNodes[0]})
o(iter.next()).deepEquals({done:false, value: root.childNodes[1]})
o(iter.next()).deepEquals({done:false, value: root.childNodes[2]})
o(iter.next().done).deepEquals(true)
o(root.childNodes.length).equals(3)
}
function onupdate(vnode) {
// the b node is still present in the DOM
o(root.childNodes.length).equals(3)
o(root.childNodes[0].nodeName).equals('A')
o(root.childNodes[1].nodeName).equals('B')
o(root.childNodes[2].nodeName).equals('C')
const iter = domFor(vnode)
o(iter.next()).deepEquals({done:false, value: root.childNodes[0]})
o(iter.next()).deepEquals({done:false, value: root.childNodes[1]})
o(iter.next()).deepEquals({done:false, value: root.childNodes[2]})
o(iter.next().done).deepEquals(true)
o(root.childNodes.length).equals(3)
}
function onupdate(vnode) {
// the b node is still present in the DOM
o(root.childNodes.length).equals(3)
o(root.childNodes[0].nodeName).equals("A")
o(root.childNodes[1].nodeName).equals("B")
o(root.childNodes[2].nodeName).equals("C")
const iter = domFor(vnode)
o(iter.next()).deepEquals({done:false, value: root.childNodes[0]})
o(iter.next()).deepEquals({done:false, value: root.childNodes[2]})
o(iter.next().done).deepEquals(true)
o(root.childNodes.length).equals(3)
}
render(root, fragment(
{oncreate, onupdate},
[
m('a'),
m('b', {onbeforeremove(){return {then(){}, finally(){}}}}),
m('c')
]
))
render(root, fragment(
{oncreate, onupdate},
[
m('a'),
null,
m('c'),
]
))
const iter = domFor(vnode)
o(iter.next()).deepEquals({done:false, value: root.childNodes[0]})
o(iter.next()).deepEquals({done:false, value: root.childNodes[2]})
o(iter.next().done).deepEquals(true)
o(root.childNodes.length).equals(3)
}
})
components.forEach(function(cmp){
o.spec(cmp.kind, function(){
var createComponent = cmp.create
o('works for components that return one element', function() {
const C = createComponent({
view(){return m('div')},
oncreate(vnode){
let n = 0
for (const dom of domFor(vnode)) {
o(dom).equals(root.firstChild)
o(++n).equals(1)
}
}
})
render(root, m(C))
})
o('works for components that return fragments', function () {
const oncreate = o.spy(function oncreate(vnode){
o(root.childNodes.length).equals(3)
o(root.childNodes[0].nodeName).equals('A')
o(root.childNodes[1].nodeName).equals('B')
o(root.childNodes[2].nodeName).equals('C')
const iter = domFor(vnode)
o(iter.next()).deepEquals({done:false, value: root.childNodes[0]})
o(iter.next()).deepEquals({done:false, value: root.childNodes[1]})
o(iter.next()).deepEquals({done:false, value: root.childNodes[2]})
o(iter.next().done).deepEquals(true)
o(root.childNodes.length).equals(3)
})
const C = createComponent({
view({children}){return children},
oncreate
})
render(root, m(C, [
m('a'),
m('b'),
m('c')
]))
o(oncreate.callCount).equals(1)
})
o('works for components that return fragments with delayed removal', function () {
const onbeforeremove = o.spy(function onbeforeremove(){return {then(){}, finally(){}}})
const oncreate = o.spy(function oncreate(vnode){
o(root.childNodes.length).equals(3)
o(root.childNodes[0].nodeName).equals('A')
o(root.childNodes[1].nodeName).equals('B')
o(root.childNodes[2].nodeName).equals('C')
const iter = domFor(vnode)
o(iter.next()).deepEquals({done:false, value: root.childNodes[0]})
o(iter.next()).deepEquals({done:false, value: root.childNodes[1]})
o(iter.next()).deepEquals({done:false, value: root.childNodes[2]})
o(iter.next().done).deepEquals(true)
o(root.childNodes.length).equals(3)
})
const onupdate = o.spy(function onupdate(vnode) {
o(root.childNodes.length).equals(3)
o(root.childNodes[0].nodeName).equals('A')
o(root.childNodes[1].nodeName).equals('B')
o(root.childNodes[2].nodeName).equals('C')
const iter = domFor(vnode)
render(root, fragment(
{oncreate, onupdate},
[
m("a"),
m("b", {onbeforeremove(){return {then(){}, finally(){}}}}),
m("c")
]
))
render(root, fragment(
{oncreate, onupdate},
[
m("a"),
null,
m("c"),
]
))
o(iter.next()).deepEquals({done:false, value: root.childNodes[0]})
o(iter.next()).deepEquals({done:false, value: root.childNodes[2]})
o(iter.next().done).deepEquals(true)
o(root.childNodes.length).equals(3)
})
const C = createComponent({
view({children}){return children},
oncreate,
onupdate
})
render(root, m(C, [
m('a'),
m('b', {onbeforeremove}),
m('c')
]))
render(root, m(C, [
m('a'),
null,
m('c')
]))
o(oncreate.callCount).equals(1)
o(onupdate.callCount).equals(1)
o(onbeforeremove.callCount).equals(1)
})
})
})
})
components.forEach(function(cmp){
const {kind, create: createComponent} = cmp
o.spec(kind, function(){
o("works for components that return one element", function() {
const C = createComponent({
view(){return m("div")},
oncreate(vnode){
let n = 0
for (const dom of domFor(vnode)) {
o(dom).equals(root.firstChild)
o(++n).equals(1)
}
}
})
render(root, m(C))
})
o("works for components that return fragments", function () {
const oncreate = o.spy(function oncreate(vnode){
o(root.childNodes.length).equals(3)
o(root.childNodes[0].nodeName).equals("A")
o(root.childNodes[1].nodeName).equals("B")
o(root.childNodes[2].nodeName).equals("C")
const iter = domFor(vnode)
o(iter.next()).deepEquals({done:false, value: root.childNodes[0]})
o(iter.next()).deepEquals({done:false, value: root.childNodes[1]})
o(iter.next()).deepEquals({done:false, value: root.childNodes[2]})
o(iter.next().done).deepEquals(true)
o(root.childNodes.length).equals(3)
})
const C = createComponent({
view({children}){return children},
oncreate
})
render(root, m(C, [
m("a"),
m("b"),
m("c")
]))
o(oncreate.callCount).equals(1)
})
o("works for components that return fragments with delayed removal", function () {
const onbeforeremove = o.spy(function onbeforeremove(){return {then(){}, finally(){}}})
const oncreate = o.spy(function oncreate(vnode){
o(root.childNodes.length).equals(3)
o(root.childNodes[0].nodeName).equals("A")
o(root.childNodes[1].nodeName).equals("B")
o(root.childNodes[2].nodeName).equals("C")
const iter = domFor(vnode)
o(iter.next()).deepEquals({done:false, value: root.childNodes[0]})
o(iter.next()).deepEquals({done:false, value: root.childNodes[1]})
o(iter.next()).deepEquals({done:false, value: root.childNodes[2]})
o(iter.next().done).deepEquals(true)
o(root.childNodes.length).equals(3)
})
const onupdate = o.spy(function onupdate(vnode) {
o(root.childNodes.length).equals(3)
o(root.childNodes[0].nodeName).equals("A")
o(root.childNodes[1].nodeName).equals("B")
o(root.childNodes[2].nodeName).equals("C")
const iter = domFor(vnode)
o(iter.next()).deepEquals({done:false, value: root.childNodes[0]})
o(iter.next()).deepEquals({done:false, value: root.childNodes[2]})
o(iter.next().done).deepEquals(true)
o(root.childNodes.length).equals(3)
})
const C = createComponent({
view({children}){return children},
oncreate,
onupdate
})
render(root, m(C, [
m("a"),
m("b", {onbeforeremove}),
m("c")
]))
render(root, m(C, [
m("a"),
null,
m("c")
]))
o(oncreate.callCount).equals(1)
o(onupdate.callCount).equals(1)
o(onbeforeremove.callCount).equals(1)
})
})
})
})

View file

@ -222,8 +222,8 @@ o.spec("onremove", function() {
text: textNode.nodeValue,
})
}
actual = JSON.stringify(actual, null, ' ')
expected = JSON.stringify(expected, null, ' ')
actual = JSON.stringify(actual, null, " ")
expected = JSON.stringify(expected, null, " ")
return {
pass: actual === expected,
message:
@ -232,21 +232,24 @@ o.spec("onremove", function() {
${actual}`
}
}}
var finallyCB
var finallyCB1
var finallyCB2
var C = createComponent({
view({children}){return children},
onbeforeremove(){
return {then(){}, finally: function (fcb) { finallyCB1 = fcb }}
}
})
function update(id, showParent, showChild) {
render(root,
m("div",
showParent && fragment(
"", // Required
showChild && fragment(
{
onbeforeremove: function () {
return {then(){}, finally: function (fcb) { finallyCB = fcb }}
},
showChild && m(C, {
onbeforeremove: function () {
return {then(){}, finally: function (fcb) { finallyCB2 = fcb }}
},
m("div", id)
)
}, m("div", id))
)
)
)
@ -257,7 +260,8 @@ ${actual}`
["#text", ""],
["DIV", "1"],
]))
o(finallyCB).equals(undefined)
o(finallyCB1).equals(undefined)
o(finallyCB2).equals(undefined)
update("2", true, false)
@ -265,9 +269,10 @@ ${actual}`
["#text", ""],
["DIV", "1"],
]))
o(typeof finallyCB).equals("function")
o(typeof finallyCB1).equals("function")
var original = finallyCB
var original1 = finallyCB1
var original2 = finallyCB2
update("3", true, true)
@ -276,14 +281,16 @@ ${actual}`
["DIV", "1"],
["DIV", "3"],
]))
o(finallyCB).equals(original)
o(finallyCB1).equals(original1)
o(finallyCB2).equals(original2)
update("4", false, true)
o(root).satisfies(template([
["DIV", "1"],
]))
o(finallyCB).equals(original)
o(finallyCB1).equals(original1)
o(finallyCB2).equals(original2)
update("5", true, true)
@ -292,22 +299,26 @@ ${actual}`
["#text", ""],
["DIV", "5"],
]))
o(finallyCB).equals(original)
o(finallyCB1).equals(original1)
o(finallyCB2).equals(original2)
finallyCB()
finallyCB1()
finallyCB2()
o(root).satisfies(template([
["#text", ""],
["DIV", "5"],
]))
o(finallyCB).equals(original)
o(finallyCB1).equals(original1)
o(finallyCB2).equals(original2)
update("6", true, true)
o(root).satisfies(template([
["#text", ""],
["DIV", "6"],
]))
o(finallyCB).equals(original)
o(finallyCB1).equals(original1)
o(finallyCB2).equals(original2)
})
})
})