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("attributes", function() {
var $window, root, render
@ -14,38 +16,38 @@ o.spec("attributes", function() {
o.spec("basics", function() {
o("works (create/update/remove)", function() {
var a = {tag: "div", attrs: {}}
var b = {tag: "div", attrs: {id: "test"}}
var c = {tag: "div", attrs: {}}
var a = m("div")
var b = m("div", {id: "test"})
var c = m("div")
render(root, [a]);
render(root, a);
o(a.dom.hasAttribute("id")).equals(false)
render(root, [b]);
render(root, b);
o(b.dom.getAttribute("id")).equals("test")
render(root, [c]);
render(root, c);
o(c.dom.hasAttribute("id")).equals(false)
})
o("undefined attr is equivalent to a lack of attr", function() {
var a = {tag: "div", attrs: {id: undefined}}
var b = {tag: "div", attrs: {id: "test"}}
var c = {tag: "div", attrs: {id: undefined}}
var a = m("div", {id: undefined})
var b = m("div", {id: "test"})
var c = m("div", {id: undefined})
render(root, [a]);
render(root, a);
o(a.dom.hasAttribute("id")).equals(false)
render(root, [b]);
render(root, b);
o(b.dom.hasAttribute("id")).equals(true)
o(b.dom.getAttribute("id")).equals("test")
// #1804
render(root, [c]);
render(root, c);
o(c.dom.hasAttribute("id")).equals(false)
})
@ -66,16 +68,16 @@ o.spec("attributes", function() {
}
render(root, [
{tag: "input", attrs: {value: "hello"}},
{tag: "input", attrs: {value: "hello"}},
{tag: "input", attrs: {value: "hello"}},
{tag: "custom-element", attrs: {custom: "x"}},
{tag: "input", attrs: {is: "something-special", custom: "x"}},
{tag: "custom-element", attrs: {is: "something-special", custom: "x"}}
m("input", {value: "hello"}),
m("input", {value: "hello"}),
m("input", {value: "hello"}),
m("custom-element", {custom: "x"}),
m("input", {is: "something-special", custom: "x"}),
m("custom-element", {is: "something-special", custom: "x"})
])
o(spies[0].callCount).equals(0)
o(spies[1].callCount).equals(0)
o(spies[0].callCount).equals(0)
o(spies[2].callCount).equals(0)
o(spies[3].calls).deepEquals([{this: spies[3].elem, args: ["custom", "x"]}])
o(spies[4].calls).deepEquals([{this: spies[4].elem, args: ["custom", "x"]}])
@ -110,12 +112,12 @@ o.spec("attributes", function() {
}
render(root, [
{tag: "input", attrs: {value: "hello"}},
{tag: "input", attrs: {value: "hello"}},
{tag: "input", attrs: {value: "hello"}},
{tag: "custom-element", attrs: {custom: "x"}},
{tag: "input", attrs: {is: "something-special", custom: "x"}},
{tag: "custom-element", attrs: {is: "something-special", custom: "x"}}
m("input", {value: "hello"}),
m("input", {value: "hello"}),
m("input", {value: "hello"}),
m("custom-element", {custom: "x"}),
m("input", {is: "something-special", custom: "x"}),
m("custom-element", {is: "something-special", custom: "x"})
])
o(spies[0].callCount).equals(0)
@ -135,47 +137,47 @@ o.spec("attributes", function() {
})
o.spec("input readonly", function() {
o("when input readonly is true, attribute is present", function() {
var a = {tag: "input", attrs: {readonly: true}}
var a = m("input", {readonly: true})
render(root, [a])
render(root, a)
o(a.dom.attributes["readonly"].value).equals("")
})
o("when input readonly is false, attribute is not present", function() {
var a = {tag: "input", attrs: {readonly: false}}
var a = m("input", {readonly: false})
render(root, [a])
render(root, a)
o(a.dom.attributes["readonly"]).equals(undefined)
})
})
o.spec("input checked", function() {
o("when input checked is true, attribute is not present", function() {
var a = {tag: "input", attrs: {checked: true}}
var a = m("input", {checked: true})
render(root, [a])
render(root, a)
o(a.dom.checked).equals(true)
o(a.dom.attributes["checked"]).equals(undefined)
})
o("when input checked is false, attribute is not present", function() {
var a = {tag: "input", attrs: {checked: false}}
var a = m("input", {checked: false})
render(root, [a])
render(root, a)
o(a.dom.checked).equals(false)
o(a.dom.attributes["checked"]).equals(undefined)
})
o("after input checked is changed by 3rd party, it can still be changed by render", function() {
var a = {tag: "input", attrs: {checked: false}}
var b = {tag: "input", attrs: {checked: true}}
var a = m("input", {checked: false})
var b = m("input", {checked: true})
render(root, [a])
render(root, a)
a.dom.checked = true //setting the javascript property makes the value no longer track the state of the attribute
a.dom.checked = false
render(root, [b])
render(root, b)
o(a.dom.checked).equals(true)
o(a.dom.attributes["checked"]).equals(undefined)
@ -183,72 +185,72 @@ o.spec("attributes", function() {
})
o.spec("input.value", function() {
o("can be set as text", function() {
var a = {tag: "input", attrs: {value: "test"}}
var a = m("input", {value: "test"})
render(root, [a]);
render(root, a);
o(a.dom.value).equals("test")
})
o("a lack of attribute removes `value`", function() {
var a = {tag: "input", attrs: {}}
var b = {tag: "input", attrs: {value: "test"}}
var c = {tag: "input", attrs: {}}
var a = m("input")
var b = m("input", {value: "test"})
var c = m("input")
render(root, [a])
render(root, a)
o(a.dom.value).equals("")
render(root, [b])
render(root, b)
o(a.dom.value).equals("test")
// https://github.com/MithrilJS/mithril.js/issues/1804#issuecomment-304521235
render(root, [c])
render(root, c)
o(a.dom.value).equals("")
})
o("can be set as number", function() {
var a = {tag: "input", attrs: {value: 1}}
var a = m("input", {value: 1})
render(root, [a]);
render(root, a);
o(a.dom.value).equals("1")
})
o("null becomes the empty string", function() {
var a = {tag: "input", attrs: {value: null}}
var b = {tag: "input", attrs: {value: "test"}}
var c = {tag: "input", attrs: {value: null}}
var a = m("input", {value: null})
var b = m("input", {value: "test"})
var c = m("input", {value: null})
render(root, [a]);
render(root, a);
o(a.dom.value).equals("")
o(a.dom.getAttribute("value")).equals(null)
render(root, [b]);
render(root, b);
o(b.dom.value).equals("test")
o(b.dom.getAttribute("value")).equals(null)
render(root, [c]);
render(root, c);
o(c.dom.value).equals("")
o(c.dom.getAttribute("value")).equals(null)
})
o("'' and 0 are different values", function() {
var a = {tag: "input", attrs: {value: 0}, children:[{tag:"#", children:""}]}
var b = {tag: "input", attrs: {value: ""}, children:[{tag:"#", children:""}]}
var c = {tag: "input", attrs: {value: 0}, children:[{tag:"#", children:""}]}
var a = m("input", {value: 0})
var b = m("input", {value: ""})
var c = m("input", {value: 0})
render(root, [a]);
render(root, a);
o(a.dom.value).equals("0")
render(root, [b]);
render(root, b);
o(b.dom.value).equals("")
// #1595 redux
render(root, [c]);
render(root, c);
o(c.dom.value).equals("0")
})
@ -257,34 +259,34 @@ o.spec("attributes", function() {
var root = $window.document.body
var render = vdom($window)
var a = {tag: "input"}
var b = {tag: "input", attrs: {value: "1"}}
var c = {tag: "input", attrs: {value: "1"}}
var d = {tag: "input", attrs: {value: 1}}
var e = {tag: "input", attrs: {value: 2}}
var a =m("input")
var b = m("input", {value: "1"})
var c = m("input", {value: "1"})
var d = m("input", {value: 1})
var e = m("input", {value: 2})
render(root, [a])
render(root, a)
var spies = $window.__getSpies(a.dom)
a.dom.focus()
o(spies.valueSetter.callCount).equals(0)
render(root, [b])
render(root, b)
o(b.dom.value).equals("1")
o(spies.valueSetter.callCount).equals(1)
render(root, [c])
render(root, c)
o(c.dom.value).equals("1")
o(spies.valueSetter.callCount).equals(1)
render(root, [d])
render(root, d)
o(d.dom.value).equals("1")
o(spies.valueSetter.callCount).equals(1)
render(root, [e])
render(root, e)
o(d.dom.value).equals("2")
o(spies.valueSetter.callCount).equals(2)
@ -296,22 +298,22 @@ o.spec("attributes", function() {
var root = $window.document.body
var render = vdom($window)
var a = {tag: "input", attrs: {type: "radio"}}
var b = {tag: "input", attrs: {type: "text"}}
var c = {tag: "input", attrs: {}}
var a = m("input", {type: "radio"})
var b = m("input", {type: "text"})
var c = m("input")
render(root, [a])
render(root, a)
var spies = $window.__getSpies(a.dom)
o(spies.typeSetter.callCount).equals(0)
o(a.dom.getAttribute("type")).equals("radio")
render(root, [b])
render(root, b)
o(spies.typeSetter.callCount).equals(0)
o(b.dom.getAttribute("type")).equals("text")
render(root, [c])
render(root, c)
o(spies.typeSetter.callCount).equals(0)
o(c.dom.hasAttribute("type")).equals(false)
@ -319,15 +321,15 @@ o.spec("attributes", function() {
})
o.spec("textarea.value", function() {
o("can be removed by not passing a value", function() {
var a = {tag: "textarea", attrs: {value:"x"}}
var b = {tag: "textarea", attrs: {}}
var a = m("textarea", {value:"x"})
var b = m("textarea")
render(root, [a])
render(root, a)
o(a.dom.value).equals("x")
// https://github.com/MithrilJS/mithril.js/issues/1804#issuecomment-304521235
render(root, [b])
render(root, b)
o(b.dom.value).equals("")
})
@ -336,34 +338,34 @@ o.spec("attributes", function() {
var root = $window.document.body
var render = vdom($window)
var a = {tag: "textarea"}
var b = {tag: "textarea", attrs: {value: "1"}}
var c = {tag: "textarea", attrs: {value: "1"}}
var d = {tag: "textarea", attrs: {value: 1}}
var e = {tag: "textarea", attrs: {value: 2}}
var a = m("textarea")
var b = m("textarea", {value: "1"})
var c = m("textarea", {value: "1"})
var d = m("textarea", {value: 1})
var e = m("textarea", {value: 2})
render(root, [a])
render(root, a)
var spies = $window.__getSpies(a.dom)
a.dom.focus()
o(spies.valueSetter.callCount).equals(0)
render(root, [b])
render(root, b)
o(b.dom.value).equals("1")
o(spies.valueSetter.callCount).equals(1)
render(root, [c])
render(root, c)
o(c.dom.value).equals("1")
o(spies.valueSetter.callCount).equals(1)
render(root, [d])
render(root, d)
o(d.dom.value).equals("1")
o(spies.valueSetter.callCount).equals(1)
render(root, [e])
render(root, e)
o(d.dom.value).equals("2")
o(spies.valueSetter.callCount).equals(2)
@ -371,23 +373,23 @@ o.spec("attributes", function() {
})
o.spec("link href", function() {
o("when link href is true, attribute is present", function() {
var a = {tag: "a", attrs: {href: true}}
var a = m("a", {href: true})
render(root, [a])
render(root, a)
o(a.dom.attributes["href"]).notEquals(undefined)
})
o("when link href is false, attribute is not present", function() {
var a = {tag: "a", attrs: {href: false}}
var a = m("a", {href: false})
render(root, [a])
render(root, a)
o(a.dom.attributes["href"]).equals(undefined)
})
})
o.spec("canvas width and height", function() {
o("uses attribute API", function() {
var canvas = {tag: "canvas", attrs: {width: "100%"}}
var canvas = m("canvas", {width: "100%"})
render(root, canvas)
@ -397,27 +399,27 @@ o.spec("attributes", function() {
})
o.spec("svg", function() {
o("when className is specified then it should be added as a class", function() {
var a = {tag: "svg", attrs: {className: "test"}}
var a = m("svg", {className: "test"})
render(root, [a]);
render(root, a);
o(a.dom.attributes["class"].value).equals("test")
})
/* eslint-disable no-script-url */
o("handles xlink:href", function() {
var vnode = {tag: "svg", ns: "http://www.w3.org/2000/svg", children: [
{tag: "a", ns: "http://www.w3.org/2000/svg", attrs: {"xlink:href": "javascript:;"}}
]}
render(root, [vnode])
var vnode = m("svg", {ns: "http://www.w3.org/2000/svg"},
m("a", {ns: "http://www.w3.org/2000/svg", "xlink:href": "javascript:;"})
)
render(root, vnode)
o(vnode.dom.nodeName).equals("svg")
o(vnode.dom.firstChild.attributes["href"].value).equals("javascript:;")
o(vnode.dom.firstChild.attributes["href"].namespaceURI).equals("http://www.w3.org/1999/xlink")
vnode = {tag: "svg", ns: "http://www.w3.org/2000/svg", children: [
{tag: "a", ns: "http://www.w3.org/2000/svg", attrs: {}}
]}
render(root, [vnode])
vnode = m("svg", {ns: "http://www.w3.org/2000/svg"},
m("a", {ns: "http://www.w3.org/2000/svg"})
)
render(root, vnode)
o(vnode.dom.nodeName).equals("svg")
o("href" in vnode.dom.firstChild.attributes).equals(false)
@ -426,54 +428,54 @@ o.spec("attributes", function() {
})
o.spec("option.value", function() {
o("can be set as text", function() {
var a = {tag: "option", attrs: {value: "test"}}
var a = m("option", {value: "test"})
render(root, [a]);
render(root, a);
o(a.dom.value).equals("test")
})
o("can be set as number", function() {
var a = {tag: "option", attrs: {value: 1}}
var a = m("option", {value: 1})
render(root, [a]);
render(root, a);
o(a.dom.value).equals("1")
})
o("null removes the attribute", function() {
var a = {tag: "option", attrs: {value: null}}
var b = {tag: "option", attrs: {value: "test"}}
var c = {tag: "option", attrs: {value: null}}
var a = m("option", {value: null})
var b = m("option", {value: "test"})
var c = m("option", {value: null})
render(root, [a]);
render(root, a);
o(a.dom.value).equals("")
o(a.dom.hasAttribute("value")).equals(false)
render(root, [b]);
render(root, b);
o(b.dom.value).equals("test")
o(b.dom.getAttribute("value")).equals("test")
render(root, [c]);
render(root, c);
o(c.dom.value).equals("")
o(c.dom.hasAttribute("value")).equals(false)
})
o("'' and 0 are different values", function() {
var a = {tag: "option", attrs: {value: 0}, children:[{tag:"#", children:""}]}
var b = {tag: "option", attrs: {value: ""}, children:[{tag:"#", children:""}]}
var c = {tag: "option", attrs: {value: 0}, children:[{tag:"#", children:""}]}
var a = m("option", {value: 0}, "")
var b = m("option", {value: ""}, "")
var c = m("option", {value: 0}, "")
render(root, [a]);
render(root, a);
o(a.dom.value).equals("0")
render(root, [b]);
render(root, b);
o(a.dom.value).equals("")
// #1595 redux
render(root, [c]);
render(root, c);
o(c.dom.value).equals("0")
})
@ -482,33 +484,33 @@ o.spec("attributes", function() {
var root = $window.document.body
var render = vdom($window)
var a = {tag: "option"}
var b = {tag: "option", attrs: {value: "1"}}
var c = {tag: "option", attrs: {value: "1"}}
var d = {tag: "option", attrs: {value: 1}}
var e = {tag: "option", attrs: {value: 2}}
var a = m("option")
var b = m("option", {value: "1"})
var c = m("option", {value: "1"})
var d = m("option", {value: 1})
var e = m("option", {value: 2})
render(root, [a])
render(root, a)
var spies = $window.__getSpies(a.dom)
o(spies.valueSetter.callCount).equals(0)
render(root, [b])
render(root, b)
o(b.dom.value).equals("1")
o(spies.valueSetter.callCount).equals(1)
render(root, [c])
render(root, c)
o(c.dom.value).equals("1")
o(spies.valueSetter.callCount).equals(1)
render(root, [d])
render(root, d)
o(d.dom.value).equals("1")
o(spies.valueSetter.callCount).equals(1)
render(root, [e])
render(root, e)
o(d.dom.value).equals("2")
o(spies.valueSetter.callCount).equals(2)
@ -517,13 +519,13 @@ o.spec("attributes", function() {
o.spec("select.value", function() {
function makeSelect(value) {
var attrs = (arguments.length === 0) ? {} : {value: value}
return {tag: "select", attrs: attrs, children: [
{tag:"option", attrs: {value: "1"}},
{tag:"option", attrs: {value: "2"}},
{tag:"option", attrs: {value: "a"}},
{tag:"option", attrs: {value: "0"}},
{tag:"option", attrs: {value: ""}}
]}
return m("select", attrs,
m("option", {value: "1"}),
m("option", {value: "2"}),
m("option", {value: "a"}),
m("option", {value: "0"}),
m("option", {value: ""})
)
}
/* FIXME
This incomplete test is meant for testing #1916.
@ -532,9 +534,9 @@ o.spec("attributes", function() {
attribute. Ask isiahmeadows.
o("render select options", function() {
var select = {tag: "select", selectedIndex: 0, children: [
{tag:"option", attrs: {value: "1", selected: ""}}
]}
var select = m("select", {selectedIndex: 0},
m("option", {value: "1", selected: ""})
)
render(root, select)
})
*/
@ -543,17 +545,17 @@ o.spec("attributes", function() {
var b = makeSelect("2")
var c = makeSelect("a")
render(root, [a])
render(root, a)
o(a.dom.value).equals("1")
o(a.dom.selectedIndex).equals(0)
render(root, [b])
render(root, b)
o(b.dom.value).equals("2")
o(b.dom.selectedIndex).equals(1)
render(root, [c])
render(root, c)
o(c.dom.value).equals("a")
o(c.dom.selectedIndex).equals(2)
@ -561,7 +563,7 @@ o.spec("attributes", function() {
o("setting null unsets the value", function() {
var a = makeSelect(null)
render(root, [a])
render(root, a)
o(a.dom.value).equals("")
o(a.dom.selectedIndex).equals(-1)
@ -570,12 +572,12 @@ o.spec("attributes", function() {
var a = makeSelect(1)
var b = makeSelect(2)
render(root, [a])
render(root, a)
o(a.dom.value).equals("1")
o(a.dom.selectedIndex).equals(0)
render(root, [b])
render(root, b)
o(b.dom.value).equals("2")
o(b.dom.selectedIndex).equals(1)
@ -584,13 +586,13 @@ o.spec("attributes", function() {
var a = makeSelect("")
var b = makeSelect(0)
render(root, [a])
render(root, a)
a.dom.focus()
o(a.dom.value).equals("")
// #1595 redux
render(root, [b])
render(root, b)
o(b.dom.value).equals("0")
})
@ -599,18 +601,18 @@ o.spec("attributes", function() {
var b = makeSelect(null)
var c = makeSelect("")
render(root, [a])
render(root, a)
a.dom.focus()
o(a.dom.value).equals("")
o(a.dom.selectedIndex).equals(4)
render(root, [b])
render(root, b)
o(b.dom.value).equals("")
o(b.dom.selectedIndex).equals(-1)
render(root, [c])
render(root, c)
o(c.dom.value).equals("")
o(c.dom.selectedIndex).equals(4)
@ -625,45 +627,32 @@ o.spec("attributes", function() {
var c = makeSelect(1)
var d = makeSelect("2")
render(root, [a])
render(root, a)
var spies = $window.__getSpies(a.dom)
a.dom.focus()
o(spies.valueSetter.callCount).equals(0)
o(a.dom.value).equals("1")
render(root, [b])
render(root, b)
o(spies.valueSetter.callCount).equals(0)
o(b.dom.value).equals("1")
render(root, [c])
render(root, c)
o(spies.valueSetter.callCount).equals(0)
o(c.dom.value).equals("1")
render(root, [d])
render(root, d)
o(spies.valueSetter.callCount).equals(1)
o(d.dom.value).equals("2")
})
})
o.spec("contenteditable attr throws on untrusted children", function() {
o("including text nodes", function() {
var div = {tag: "div", attrs: {contenteditable: true}, text: ""}
var succeeded = false
try {
render(root, div)
succeeded = true
}
catch(e){/* ignore */}
o(succeeded).equals(false)
})
o.spec("contenteditable throws on untrusted children", function() {
o("including elements", function() {
var div = {tag: "div", attrs: {contenteditable: true}, children: [{tag: "script", attrs: {src: "http://evil.com"}}]}
var div = m("div", {contenteditable: true}, m("script", {src: "http://evil.com"}))
var succeeded = false
try {
@ -676,7 +665,7 @@ o.spec("attributes", function() {
o(succeeded).equals(false)
})
o("tolerating empty children", function() {
var div = {tag: "div", attrs: {contenteditable: true}, children: []}
var div = m("div", {contenteditable: true})
var succeeded = false
try {
@ -689,61 +678,7 @@ o.spec("attributes", function() {
o(succeeded).equals(true)
})
o("tolerating trusted content", function() {
var div = {tag: "div", attrs: {contenteditable: true}, children: [{tag: "<", children: "<a></a>"}]}
var succeeded = false
try {
render(root, div)
succeeded = true
}
catch(e){/* ignore */}
o(succeeded).equals(true)
})
})
o.spec("contentEditable prop throws on untrusted children", function() {
o("including text nodes", function() {
var div = {tag: "div", attrs: {contentEditable: true}, text: ""}
var succeeded = false
try {
render(root, div)
succeeded = true
}
catch(e){/* ignore */}
o(succeeded).equals(false)
})
o("including elements", function() {
var div = {tag: "div", attrs: {contentEditable: true}, children: [{tag: "script", attrs: {src: "http://evil.com"}}]}
var succeeded = false
try {
render(root, div)
succeeded = true
}
catch(e){/* ignore */}
o(succeeded).equals(false)
})
o("tolerating empty children", function() {
var div = {tag: "div", attrs: {contentEditable: true}, children: []}
var succeeded = false
try {
render(root, div)
succeeded = true
}
catch(e){/* ignore */}
o(succeeded).equals(true)
})
o("tolerating trusted content", function() {
var div = {tag: "div", attrs: {contentEditable: true}, children: [{tag: "<", children: "<a></a>"}]}
var div = m("div", {contenteditable: true}, m.trust("<a></a>"))
var succeeded = false
try {