define([ 'intern!object', 'intern/chai!assert', 'sinon', '../../NodeList', '../../dom-attr', '../../dom-construct', '../../query', '../../NodeList-manipulate', '../../NodeList-traverse' ], function (registerSuite, assert, sinon, NodeList, attr, construct, query) { registerSuite({ name: 'dojo/NodeList-manipulate', // Test this private method because a lot of public methods defer to it. // Better to test all in one place so that the consumers can rely on it to "do the right thing" // instead of checking implementation details themselves "_placeMultiple": { "returns original node list": function () { //arrange var nodeList = new NodeList(); //act var result = nodeList._placeMultiple(document.createElement("div"), "last"); //assert assert.strictEqual(result, nodeList); }, "string + string calls dojo/dom-construct::place with correct arguments": function () { //arrange var nodes = [ document.createElement("input"), document.createElement("input") ], nodeList = new NodeList(nodes), queryResults = [ document.createElement("span"), document.createElement("span") ], container = document.createElement("div"), mock = sinon.spy(construct, "place"), position = "last"; for (var i in nodes) { nodes[i].className = "class" + i; } for (var i in queryResults) { container.appendChild(queryResults[i]); } document.body.appendChild(container); //act nodeList._placeMultiple("span", position); //assert //last node in nodelist placed with dojo/dom-construct::place into first query result node assert.deepEqual(mock.args[0], [nodes[1], queryResults[0], position]); //addiitional nodes in nodelist place into first query result before the last node assert.equal(nodes[1].previousSibling, nodes[0]); //last node in nodelist is cloned and that clone is placed with dojo/dom-construct::place into additional query result nodes assert.notEqual(mock.args[1][0], nodes[1]); assert.equal(mock.args[1][0].outerHTML, nodes[1].outerHTML); //addiitional nodes in nodelist place into first query result before the last node assert.notEqual(queryResults[1].children[1], nodes[1]); assert.equal(queryResults[1].children[1].outerHTML, nodes[1].outerHTML); mock.restore(); }, "Node + string calls dojo/dom-construct::place with correct arguments": function () { //arrange var nodes = [ document.createElement("input"), document.createElement("input") ], nodeList = new NodeList(nodes), queryNode = document.createElement("span"), container = document.createElement("div"), mock = sinon.spy(construct, "place"), position = "last"; for (var i in nodes) { nodes[i].className = "class" + i; } container.appendChild(queryNode); document.body.appendChild(container); //act nodeList._placeMultiple(queryNode, position); //assert //last node in nodelist placed with dojo/dom-construct::place into queryNode assert.deepEqual(mock.args[0], [nodes[1], queryNode, position]); //addiitional nodes in nodelist place into queryNode before the last node assert.equal(nodes[1].previousSibling, nodes[0]); mock.restore(); }, "NodeList + string calls dojo/dom-construct::place with correct arguments": function () { //arrange var nodes = [ document.createElement("input"), document.createElement("input") ], nodeList = new NodeList(nodes), queryResults = [ document.createElement("span"), document.createElement("span") ], queryNodeList = new NodeList(queryResults), container = document.createElement("div"), mock = sinon.spy(construct, "place"), position = "last"; for (var i in nodes) { nodes[i].className = "class" + i; } for (var i in queryResults) { container.appendChild(queryResults[i]); } document.body.appendChild(container); //act nodeList._placeMultiple(queryNodeList, position); //assert //last node in nodelist placed with dojo/dom-construct::place into first query result node assert.deepEqual(mock.args[0], [nodes[1], queryResults[0], position]); //addiitional nodes in nodelist place into first query result before the last node assert.equal(nodes[1].previousSibling, nodes[0]); //last node in nodelist is cloned and that clone is placed with dojo/dom-construct::place into additional query result nodes assert.notEqual(mock.args[1][0], nodes[1]); assert.equal(mock.args[1][0].outerHTML, nodes[1].outerHTML); //addiitional nodes in nodelist place into first query result before the last node assert.notEqual(queryResults[1].children[1], nodes[1]); assert.equal(queryResults[1].children[1].outerHTML, nodes[1].outerHTML); mock.restore(); } }, "innerHTML()": { "when argument present => delegate to addContent": function () { //arrange var expected = "foo", argument = "bar", nodes = ["bar", "baz"], nodeList = new NodeList(nodes), mock = sinon.stub(nodeList, "addContent").returns(expected); //act var result = nodeList.innerHTML(argument); //assert assert.equal(result, expected); assert.deepEqual(mock.args[0], [argument, "only"]); mock.restore(); }, "when no argument, return first nodes innerHTML": function () { //arrange var node = document.createElement("div"), otherNode = document.createElement("div"), expected = "the expected result", nodeList = new NodeList([node, otherNode]); node.innerHTML = expected; otherNode = "this is not the expected content"; //act var result = nodeList.innerHTML(); //assert assert.equal(result, expected); } }, "text()": { "string => attr.set": function () { //arrange var nodes = [ document.createElement("div"), document.createElement("div") ], nodeList = new NodeList(nodes), expected = "the expected content", mock = sinon.spy(attr, "set"); //act var result = nodeList.text(expected); //assert for (var i in nodes) { assert.deepEqual(mock.args[i], [nodes[i], "textContent", expected]); } assert.equal(result, nodeList); mock.restore(); }, "no args => attr.get": function () { //arrange var nodes = [ document.createElement("div"), document.createElement("div") ], nodeList = new NodeList(nodes), expected = [ "the first expected content", "the second expected content" ], mock = sinon.stub(attr, "get"); for (var i in nodes) { mock.withArgs(nodes[i]).returns(expected[i]); } //act var result = nodeList.text(); //assert assert.equal(result, expected[0] + expected[1]); for (var i in nodes) { assert.deepEqual(mock.args[i], [nodes[i], "textContent"]); } mock.restore(); } }, "val()": { "empty nodelist::val() = undefined": function () { //arrange var nodeList = new NodeList(); //act var result = nodeList.val(); //assert assert.isUndefined(result); }, "nodeList([, ...]);:val() = value of first element": function () { //arrange var expected = "the value", firstNode = document.createElement("select"), nextNode = document.createElement("input"), notSelectedOption = document.createElement("option"), selectedOption = document.createElement("option"), nodeList = new NodeList([firstNode, nextNode]); notSelectedOption.value = "not the expected value"; selectedOption.value = expected; firstNode.add(notSelectedOption); firstNode.add(selectedOption); selectedOption.selected = true; //act var result = nodeList.val(); //assert assert.equal(result, expected); }, "nodeList([, ...])::val() = array of selected options": function () { //arrange var expected = ["blue", "yellow"], notExpected = "red", node = document.createElement("select"), notSelectedOption = document.createElement("option"), firstSelectedOption = document.createElement("option"), secondSelectedOption = document.createElement("option"), nodeList = new NodeList([node]); node.multiple = true; notSelectedOption.value = notExpected; firstSelectedOption.value = expected[0]; secondSelectedOption.value = expected[1]; node.add(firstSelectedOption); node.add(notSelectedOption); node.add(secondSelectedOption); firstSelectedOption.selected = true; secondSelectedOption.selected = true; //act var result = nodeList.val(); //assert assert.deepEqual(result, expected); }, "nodeList([, ])::val(string) => sets the value of the input elements": function () { //arrange var expected = "the value", nodes = [ document.createElement("input"), document.createElement("input") ], nodeList = new NodeList(nodes); for (var i in nodes) { nodes[i].value = "the old value"; } //act var result = nodeList.val(expected); //assert for (var i in nodes) { assert.equal(expected, nodes[i].value); } assert.equal(result, nodeList); }, "nodeList([, ])::val(array) => sets the value of the input elements": function () { //arrange var expected = [ "the first value", "the second value" ], nodes = [ document.createElement("input"), document.createElement("input") ], nodeList = new NodeList(nodes); for (var i in nodes) { nodes[i].value = "the old value"; } //act var result = nodeList.val(expected); //assert for (var i in nodes) { assert.equal(expected[i], nodes[i].value); } assert.equal(result, nodeList); }, "nodeList([])::val(string) => sets correct options to selected": function () { //arrange var expected = "the value", node = document.createElement("select"), notSelectedOption = document.createElement("option"), selectedOption = document.createElement("option"), nodeList = new NodeList([node]); notSelectedOption.value = "not the expected value"; selectedOption.value = expected; node.add(notSelectedOption); node.add(selectedOption); //act var result = nodeList.val(expected); //assert assert.isTrue(selectedOption.selected); assert.isFalse(notSelectedOption.selected); assert.equal(result, nodeList); }, "nodeList([, ])::val(string) => selects correct elements": function () { //arrange var expected = "the value", nodeToCheck = document.createElement("input"), nodeToNotCheck = document.createElement("input"), nodes = [nodeToNotCheck, nodeToCheck], nodeList = new NodeList(nodes); for (var i in nodes) { nodes[i].type = "checkbox"; } nodeToCheck.value = expected; nodeToNotCheck.value = "not expected value"; nodeToCheck.checked = false; nodeToNotCheck.checked = true; //act var result = nodeList.val(expected); //assert assert.isTrue(nodeToCheck.checked); assert.isFalse(nodeToNotCheck.checked); assert.equal(result, nodeList); }, "nodeList([])::val(string) => selects correct elements": function () { //arrange var expected = "the value", nodeToCheck = document.createElement("input"), nodeToNotCheck = document.createElement("input"), nodes = [nodeToNotCheck, nodeToCheck], nodeList = new NodeList(nodes); for (var i in nodes) { nodes[i].type = "radio"; nodes[i].name = "theName" + i; } nodeToCheck.value = expected; nodeToNotCheck.value = "not expected value"; nodeToCheck.checked = false; nodeToNotCheck.checked = true; //act var result = nodeList.val(expected); //assert assert.isTrue(nodeToCheck.checked); assert.isFalse(nodeToNotCheck.checked); assert.equal(result, nodeList); } }, "append()": { "any => this.addContent(any, 'last')": function () { //arrange var arg = { foo: "bar" }, expected = "the result", nodeList = new NodeList(), mock = sinon.stub(nodeList, "addContent").returns(expected); //act var result = nodeList.append(arg); //assert assert.deepEqual(mock.args[0], [arg, "last"]); assert.equal(result, expected); mock.restore(); } }, "appendTo()": { "string => this._placeMultiple(string, 'last')": function () { //arrange var arg = { foo: "bar" }, expected = "the result", nodeList = new NodeList(), mock = sinon.stub(nodeList, "_placeMultiple").returns(expected); //act var result = nodeList.appendTo(arg); //assert assert.deepEqual(mock.args[0], [arg, "last"]); assert.equal(result, expected); mock.restore(); } }, "prepend()": { "string => this.prepend(string, 'first')": function () { //arrange var arg = { foo: "bar" }, expected = "the result", nodeList = new NodeList(), mock = sinon.stub(nodeList, "addContent").returns(expected); //act var result = nodeList.prepend(arg); //assert assert.deepEqual(mock.args[0], [arg, "first"]); assert.equal(result, expected); mock.restore(); } }, "prependTo()": { "string => this._placeMultiple(string, 'first')": function () { //arrange var arg = { foo: "bar" }, expected = "the result", nodeList = new NodeList(), mock = sinon.stub(nodeList, "_placeMultiple").returns(expected); //act var result = nodeList.prependTo(arg); //assert assert.deepEqual(mock.args[0], [arg, "first"]); assert.equal(result, expected); mock.restore(); } }, "after()": { "string => this.after(string, 'after')": function () { //arrange var arg = { foo: "bar" }, expected = "the result", nodeList = new NodeList(), mock = sinon.stub(nodeList, "addContent").returns(expected); //act var result = nodeList.after(arg); //assert assert.deepEqual(mock.args[0], [arg, "after"]); assert.equal(result, expected); mock.restore(); } }, "insertAfter()": { "string => this._placeMultiple(string, 'after')": function () { //arrange var arg = { foo: "bar" }, expected = "the result", nodeList = new NodeList(), mock = sinon.stub(nodeList, "_placeMultiple").returns(expected); //act var result = nodeList.insertAfter(arg); //assert assert.deepEqual(mock.args[0], [arg, "after"]); assert.equal(result, expected); mock.restore(); } }, "before()": { "string => this.after(string, 'before')": function () { //arrange var arg = { foo: "bar" }, expected = "the result", nodeList = new NodeList(), mock = sinon.stub(nodeList, "addContent").returns(expected); //act var result = nodeList.before(arg); //assert assert.deepEqual(mock.args[0], [arg, "before"]); assert.equal(result, expected); mock.restore(); } }, "insertBefore()": { "string => this._placeMultiple(string, 'before')": function () { //arrange var arg = { foo: "bar" }, expected = "the result", nodeList = new NodeList(), mock = sinon.stub(nodeList, "_placeMultiple").returns(expected); //act var result = nodeList.insertBefore(arg); //assert assert.deepEqual(mock.args[0], [arg, "before"]); assert.equal(result, expected); mock.restore(); } }, "remove()": { "remove is alias of NodeList.prototype.orphan": function () { //arrange var nodeList = new NodeList(); //act //assert assert.strictEqual(nodeList.remove, NodeList.prototype.orphan); } }, "wrap()": { "returns nodeList": function () { //arrange var nodeList = new NodeList(); //act var result = nodeList.wrap("
"); //assert assert.strictEqual(result, nodeList); }, "inserts nodes at the lowest level of the wrapper": function () { //arrange var node = document.createElement("input"), nodeList = new NodeList([node]), container = document.createElement("div") container.appendChild(node); //act nodeList.wrap("