Index: sky/tests/dom/replaceChild.sky |
diff --git a/sky/tests/dom/replaceChild.sky b/sky/tests/dom/replaceChild.sky |
new file mode 100644 |
index 0000000000000000000000000000000000000000..17cc7ad7bb21360e9bde8f800122841bb78e1410 |
--- /dev/null |
+++ b/sky/tests/dom/replaceChild.sky |
@@ -0,0 +1,90 @@ |
+<sky> |
+<import src="../resources/chai.sky" /> |
+<import src="../resources/mocha.sky" /> |
+<import src="../resources/dom-utils.sky" as="DomUtils" /> |
+<script> |
+describe("replaceChild", function() { |
+ var childElementCount = DomUtils.childElementCount; |
+ var childNodeCount = DomUtils.childNodeCount; |
+ |
+ it("should throw with invalid arguments", function() { |
+ var parent = document.createElement("div"); |
+ assert.throws(function() { |
+ parent.replaceChild(); |
+ }); |
+ assert.throws(function() { |
+ parent.replaceChild(null, null); |
+ }); |
+ assert.throws(function() { |
+ parent.replaceChild({tagName: "div"}); |
+ }); |
+ assert.throws(function() { |
+ parent.replaceChild(null, document.createElement("div")); |
+ }); |
+ assert.throws(function() { |
+ parent.replaceChild(document.createElement("div"), {tagName: "div"}); |
+ }); |
+ }); |
+ |
+ it("should replace elements", function() { |
+ var parent = document.createElement("div"); |
+ var oldChild = parent.appendChild(document.createElement("div")); |
+ var newChild = document.createElement("div"); |
+ parent.replaceChild(newChild, oldChild); |
+ assert.isNull(oldChild.parentNode); |
+ assert.equal(newChild.parentNode, parent); |
+ }); |
+ |
+ it("should replace text", function() { |
+ var parent = document.createElement("div"); |
+ var oldChild = parent.appendChild(new Text(" it's a text ")); |
+ var newChild = document.createElement("div"); |
+ parent.replaceChild(newChild, oldChild); |
+ assert.isNull(oldChild.parentNode); |
+ assert.equal(newChild.parentNode, parent); |
+ }); |
+ |
+ it("should replace children with a fragment", function() { |
+ var fragment = document.createDocumentFragment(); |
+ var child1 = fragment.appendChild(document.createElement("div")); |
+ var child2 = fragment.appendChild(new Text(" text ")); |
+ var child3 = fragment.appendChild(new Text(" ")); |
+ var child4 = fragment.appendChild(document.createElement("div")); |
+ var parent = document.createElement("div"); |
+ var oldChild = parent.appendChild(document.createElement("div")); |
+ var lastChild = parent.appendChild(document.createElement("div")); |
+ parent.replaceChild(fragment, oldChild); |
+ assert.equal(child1.parentNode, parent); |
+ assert.equal(child2.parentNode, parent); |
+ assert.equal(child3.parentNode, parent); |
+ assert.equal(child4.parentNode, parent); |
+ assert.isNull(oldChild.parentNode); |
+ assert.equal(childNodeCount(parent), 5); |
+ assert.equal(childElementCount(parent), 3); |
+ assert.equal(parent.lastChild, lastChild); |
+ }); |
+ |
+ it("should throw when inserting a tree scope", function() { |
+ var parent = document.createElement("div"); |
+ var doc = new Document(); |
+ var shadowRoot = document.createElement("span").createShadowRoot(); |
+ assert.throws(function() { |
+ parent.replaceChild(doc); |
+ }); |
+ assert.throws(function() { |
+ parent.replaceChild(shadowRoot); |
+ }); |
+ assert.throws(function() { |
+ doc.replaceChild(fragment); |
+ }); |
+ }); |
+ |
+ it("should throw when appending to a text", function() { |
+ var parent = new Text(); |
+ assert.throws(function() { |
+ parent.replaceChild(document.createElement("div"), null); |
+ }); |
+ }); |
+}); |
+</script> |
+</sky> |