OLD | NEW |
(Empty) | |
| 1 <sky> |
| 2 <import src="../resources/chai.sky" /> |
| 3 <import src="../resources/mocha.sky" /> |
| 4 <import src="../resources/dom-utils.sky" as="DomUtils" /> |
| 5 <script> |
| 6 describe("replaceChild", function() { |
| 7 var childElementCount = DomUtils.childElementCount; |
| 8 var childNodeCount = DomUtils.childNodeCount; |
| 9 |
| 10 it("should throw with invalid arguments", function() { |
| 11 var parent = document.createElement("div"); |
| 12 assert.throws(function() { |
| 13 parent.replaceChild(); |
| 14 }); |
| 15 assert.throws(function() { |
| 16 parent.replaceChild(null, null); |
| 17 }); |
| 18 assert.throws(function() { |
| 19 parent.replaceChild({tagName: "div"}); |
| 20 }); |
| 21 assert.throws(function() { |
| 22 parent.replaceChild(null, document.createElement("div")); |
| 23 }); |
| 24 assert.throws(function() { |
| 25 parent.replaceChild(document.createElement("div"), {tagName: "div"}); |
| 26 }); |
| 27 }); |
| 28 |
| 29 it("should replace elements", function() { |
| 30 var parent = document.createElement("div"); |
| 31 var oldChild = parent.appendChild(document.createElement("div")); |
| 32 var newChild = document.createElement("div"); |
| 33 parent.replaceChild(newChild, oldChild); |
| 34 assert.isNull(oldChild.parentNode); |
| 35 assert.equal(newChild.parentNode, parent); |
| 36 }); |
| 37 |
| 38 it("should replace text", function() { |
| 39 var parent = document.createElement("div"); |
| 40 var oldChild = parent.appendChild(new Text(" it's a text ")); |
| 41 var newChild = document.createElement("div"); |
| 42 parent.replaceChild(newChild, oldChild); |
| 43 assert.isNull(oldChild.parentNode); |
| 44 assert.equal(newChild.parentNode, parent); |
| 45 }); |
| 46 |
| 47 it("should replace children with a fragment", function() { |
| 48 var fragment = document.createDocumentFragment(); |
| 49 var child1 = fragment.appendChild(document.createElement("div")); |
| 50 var child2 = fragment.appendChild(new Text(" text ")); |
| 51 var child3 = fragment.appendChild(new Text(" ")); |
| 52 var child4 = fragment.appendChild(document.createElement("div")); |
| 53 var parent = document.createElement("div"); |
| 54 var oldChild = parent.appendChild(document.createElement("div")); |
| 55 var lastChild = parent.appendChild(document.createElement("div")); |
| 56 parent.replaceChild(fragment, oldChild); |
| 57 assert.equal(child1.parentNode, parent); |
| 58 assert.equal(child2.parentNode, parent); |
| 59 assert.equal(child3.parentNode, parent); |
| 60 assert.equal(child4.parentNode, parent); |
| 61 assert.isNull(oldChild.parentNode); |
| 62 assert.equal(childNodeCount(parent), 5); |
| 63 assert.equal(childElementCount(parent), 3); |
| 64 assert.equal(parent.lastChild, lastChild); |
| 65 }); |
| 66 |
| 67 it("should throw when inserting a tree scope", function() { |
| 68 var parent = document.createElement("div"); |
| 69 var doc = new Document(); |
| 70 var shadowRoot = document.createElement("span").createShadowRoot(); |
| 71 assert.throws(function() { |
| 72 parent.replaceChild(doc); |
| 73 }); |
| 74 assert.throws(function() { |
| 75 parent.replaceChild(shadowRoot); |
| 76 }); |
| 77 assert.throws(function() { |
| 78 doc.replaceChild(fragment); |
| 79 }); |
| 80 }); |
| 81 |
| 82 it("should throw when appending to a text", function() { |
| 83 var parent = new Text(); |
| 84 assert.throws(function() { |
| 85 parent.replaceChild(document.createElement("div"), null); |
| 86 }); |
| 87 }); |
| 88 }); |
| 89 </script> |
| 90 </sky> |
OLD | NEW |