diff --git a/build/jasmine/matchers.js b/build/jasmine/matchers.js index f5c4bf3..0cfd2ed 100644 --- a/build/jasmine/matchers.js +++ b/build/jasmine/matchers.js @@ -1,72 +1,26 @@ -function toBeTextNode() { - return { - compare(actual) { - const pass = actual.nodeType === 3; - const message = pass - ? `Expected node to be a text node` - : `Expected node not to be a text node`; - - return { pass, message }; - } - }; -} - -function toBeElementNode() { - return { - compare(actual) { - const pass = actual.nodeType === 1; - const message = pass - ? `Expected node to be a element node` - : `Expected node not to be a element node`; - - return { pass, message }; - } - }; -} +function toHaveSameHtml() { + function getCompressedHtml(html) { + return html + .replace(/\s+\s+/gm, ">") + .replace(/\s+/gm, " ") + .trim(); + } -function toBeTag() { return { compare(actual, expected) { - if (actual.nodeType !== 1) { - return { pass: false, message: "Expected element node" }; - } - - const expectedTag = expected.toLowerCase(); - const actualTag = actual.tagName.toLowerCase(); - const pass = actualTag === expectedTag; - const message = pass - ? `Expected to be a "${expectedTag}" but it a "${actualTag}"` - : `Expected not to be a "${expectedTag}"`; - - return { pass, message }; - } - }; -} + const actualHtml = getCompressedHtml(actual.innerHTML); + const expectedHtml = getCompressedHtml(expected); -function toHaveText() { - return { - compare(actual, expected) { - if (actual.nodeType !== 1 && actual.nodeType !== 3) { - return { pass: false, message: "Expected text or element node" }; - } + const pass = actualHtml === expectedHtml; - const expectedText = expected.trim(); - const actualText = actual.nodeType === 1 - ? actual.textContent.trim() - : actual.wholeText.trim(); - const pass = actualText === expected; const message = pass - ? `Expected node to have "${expectedText}" but have "${actualText}"` - : `Expected node not to have "${expectedText}"`; + ? `Expected node not to have "${expectedHtml}"` + : `Expected node to have "${expectedHtml}" but have "${actualHtml}"`; return { pass, message }; } }; } -jasmine.Expectation.addCoreMatchers({ - toBeTextNode, - toBeElementNode, - toBeTag, - toHaveText -}); +jasmine.Expectation.addCoreMatchers({ toHaveSameHtml }); diff --git a/spec/manipulation/append.spec.js b/spec/manipulation/append.spec.js index 9e5504b..87305f9 100644 --- a/spec/manipulation/append.spec.js +++ b/spec/manipulation/append.spec.js @@ -6,28 +6,21 @@ describe("append", () => { it("inserts given HTML string to end of element", () => { useFixture(`
+ Text Node Existing Node
`); const subject = document.querySelector(".root"); - append(subject, "Text NodeSpan Node"); - - const nodes = getSignificantNodes(subject); - - expect(nodes.length).toBe(3); - - expect(nodes[0]).toBeElementNode(); - expect(nodes[0]).toBeTag("span"); - expect(nodes[0]).toHaveText("Existing Node"); - - expect(nodes[1]).toBeTextNode(); - expect(nodes[1]).toHaveText("Text Node"); + append(subject, "New Text NodeSpan Node"); - expect(nodes[2]).toBeElementNode(); - expect(nodes[2]).toBeTag("span"); - expect(nodes[2]).toHaveText("Span Node"); + expect(subject).toHaveSameHtml(` + Text Node + Existing Node + New Text Node + Span Node + `); }); it("inserts given node to begin of element", () => { @@ -47,23 +40,12 @@ describe("append", () => { append(subject, elementNode); append(subject, textNode); - const nodes = getSignificantNodes(subject); - - expect(nodes.length).toBe(4); - - expect(nodes[0]).toBeTextNode(); - expect(nodes[0]).toHaveText("Text Node"); - - expect(nodes[1]).toBeElementNode(); - expect(nodes[1]).toBeTag("span"); - expect(nodes[1]).toHaveText("Existing Node"); - - expect(nodes[2]).toBeElementNode(); - expect(nodes[2]).toBeTag("span"); - expect(nodes[2]).toHaveText("Span Node"); - - expect(nodes[3]).toBeTextNode(); - expect(nodes[3]).toHaveText("New Text Node"); + expect(subject).toHaveSameHtml(` + Text Node + Existing Node + Span Node + New Text Node + `); }); it("inserts given nodes from node list to begin of element", () => { @@ -87,23 +69,12 @@ describe("append", () => { append(subject, fragment.childNodes); - const nodes = getSignificantNodes(subject); - - expect(nodes.length).toBe(4); - - expect(nodes[0]).toBeTextNode(); - expect(nodes[0]).toHaveText("Text Node"); - - expect(nodes[1]).toBeElementNode(); - expect(nodes[1]).toBeTag("span"); - expect(nodes[1]).toHaveText("Existing Node"); - - expect(nodes[2]).toBeTextNode(); - expect(nodes[2]).toHaveText("New Text Node"); - - expect(nodes[3]).toBeElementNode(); - expect(nodes[3]).toBeTag("span"); - expect(nodes[3]).toHaveText("Span Node"); + expect(subject).toHaveSameHtml(` + Text Node + Existing Node + New Text Node + Span Node + `); }); it("inserts given document fragment to begin of element", () => { @@ -127,23 +98,12 @@ describe("append", () => { append(subject, fragment); - const nodes = getSignificantNodes(subject); - - expect(nodes.length).toBe(4); - - expect(nodes[0]).toBeTextNode(); - expect(nodes[0]).toHaveText("Text Node"); - - expect(nodes[1]).toBeElementNode(); - expect(nodes[1]).toBeTag("span"); - expect(nodes[1]).toHaveText("Existing Node"); - - expect(nodes[2]).toBeTextNode(); - expect(nodes[2]).toHaveText("New Text Node"); - - expect(nodes[3]).toBeElementNode(); - expect(nodes[3]).toBeTag("span"); - expect(nodes[3]).toHaveText("Span Node"); + expect(subject).toHaveSameHtml(` + Text Node + Existing Node + New Text Node + Span Node + `); }); it("inserts given multiple arguments to begin of element", () => { @@ -177,36 +137,15 @@ describe("append", () => { fragment.childNodes ); - const nodes = getSignificantNodes(subject); - - expect(nodes.length).toBe(8); - - expect(nodes[0]).toBeTextNode(); - expect(nodes[0]).toHaveText("Text Node"); - - expect(nodes[1]).toBeElementNode(); - expect(nodes[1]).toBeTag("span"); - expect(nodes[1]).toHaveText("Existing Node"); - - expect(nodes[2]).toBeTextNode(); - expect(nodes[2]).toHaveText("HTML Text Node"); - - expect(nodes[3]).toBeElementNode(); - expect(nodes[3]).toBeTag("span"); - expect(nodes[3]).toHaveText("Span from HTML"); - - expect(nodes[4]).toBeTextNode(); - expect(nodes[4]).toHaveText("Standalone Text Node"); - - expect(nodes[5]).toBeElementNode(); - expect(nodes[5]).toBeTag("span"); - expect(nodes[5]).toHaveText("Standalone Element Node"); - - expect(nodes[6]).toBeTextNode(); - expect(nodes[6]).toHaveText("Text Node from NodeList"); - - expect(nodes[7]).toBeElementNode(); - expect(nodes[7]).toBeTag("span"); - expect(nodes[7]).toHaveText("Element Node from NodeList"); + expect(subject).toHaveSameHtml(` + Text Node + Existing Node + HTML Text Node + Span from HTML + Standalone Text Node + Standalone Element Node + Text Node from NodeList + Element Node from NodeList + `); }); }); diff --git a/spec/manipulation/prepend.spec.js b/spec/manipulation/prepend.spec.js index b944804..b1eb65e 100644 --- a/spec/manipulation/prepend.spec.js +++ b/spec/manipulation/prepend.spec.js @@ -14,20 +14,11 @@ describe("prepend", () => { prepend(subject, "Text NodeSpan Node"); - const nodes = getSignificantNodes(subject); - - expect(nodes.length).toBe(3); - - expect(nodes[0]).toBeTextNode(); - expect(nodes[0]).toHaveText("Text Node"); - - expect(nodes[1]).toBeElementNode(); - expect(nodes[1]).toBeTag("span"); - expect(nodes[1]).toHaveText("Span Node"); - - expect(nodes[2]).toBeElementNode(); - expect(nodes[2]).toBeTag("span"); - expect(nodes[2]).toHaveText("Existing Node"); + expect(subject).toHaveSameHtml(` + Text Node + Span Node + Existing Node + `); }); it("inserts given node to begin of element", () => { @@ -47,23 +38,12 @@ describe("prepend", () => { prepend(subject, elementNode); prepend(subject, textNode); - const nodes = getSignificantNodes(subject); - - expect(nodes.length).toBe(4); - - expect(nodes[0]).toBeTextNode(); - expect(nodes[0]).toHaveText("New Text Node"); - - expect(nodes[1]).toBeElementNode(); - expect(nodes[1]).toBeTag("span"); - expect(nodes[1]).toHaveText("Span Node"); - - expect(nodes[2]).toBeTextNode(); - expect(nodes[2]).toHaveText("Text Node"); - - expect(nodes[3]).toBeElementNode(); - expect(nodes[3]).toBeTag("span"); - expect(nodes[3]).toHaveText("Existing Node"); + expect(subject).toHaveSameHtml(` + New Text Node + Span Node + Text Node + Existing Node + `); }); it("inserts given nodes from node list to begin of element", () => { @@ -87,23 +67,12 @@ describe("prepend", () => { prepend(subject, fragment.childNodes); - const nodes = getSignificantNodes(subject); - - expect(nodes.length).toBe(4); - - expect(nodes[0]).toBeTextNode(); - expect(nodes[0]).toHaveText("New Text Node"); - - expect(nodes[1]).toBeElementNode(); - expect(nodes[1]).toBeTag("span"); - expect(nodes[1]).toHaveText("Span Node"); - - expect(nodes[2]).toBeTextNode(); - expect(nodes[2]).toHaveText("Text Node"); - - expect(nodes[3]).toBeElementNode(); - expect(nodes[3]).toBeTag("span"); - expect(nodes[3]).toHaveText("Existing Node"); + expect(subject).toHaveSameHtml(` + New Text Node + Span Node + Text Node + Existing Node + `); }); it("inserts given document fragment to begin of element", () => { @@ -127,23 +96,12 @@ describe("prepend", () => { prepend(subject, fragment); - const nodes = getSignificantNodes(subject); - - expect(nodes.length).toBe(4); - - expect(nodes[0]).toBeTextNode(); - expect(nodes[0]).toHaveText("New Text Node"); - - expect(nodes[1]).toBeElementNode(); - expect(nodes[1]).toBeTag("span"); - expect(nodes[1]).toHaveText("Span Node"); - - expect(nodes[2]).toBeTextNode(); - expect(nodes[2]).toHaveText("Text Node"); - - expect(nodes[3]).toBeElementNode(); - expect(nodes[3]).toBeTag("span"); - expect(nodes[3]).toHaveText("Existing Node"); + expect(subject).toHaveSameHtml(` + New Text Node + Span Node + Text Node + Existing Node + `); }); it("inserts given multiple arguments to begin of element", () => { @@ -177,36 +135,15 @@ describe("prepend", () => { fragment.childNodes ); - const nodes = getSignificantNodes(subject); - - expect(nodes.length).toBe(8); - - expect(nodes[0]).toBeTextNode(); - expect(nodes[0]).toHaveText("HTML Text Node"); - - expect(nodes[1]).toBeElementNode(); - expect(nodes[1]).toBeTag("span"); - expect(nodes[1]).toHaveText("Span from HTML"); - - expect(nodes[2]).toBeTextNode(); - expect(nodes[2]).toHaveText("Standalone Text Node"); - - expect(nodes[3]).toBeElementNode(); - expect(nodes[3]).toBeTag("span"); - expect(nodes[3]).toHaveText("Standalone Element Node"); - - expect(nodes[4]).toBeTextNode(); - expect(nodes[4]).toHaveText("Text Node from NodeList"); - - expect(nodes[5]).toBeElementNode(); - expect(nodes[5]).toBeTag("span"); - expect(nodes[5]).toHaveText("Element Node from NodeList"); - - expect(nodes[6]).toBeTextNode(); - expect(nodes[6]).toHaveText("Text Node"); - - expect(nodes[7]).toBeElementNode(); - expect(nodes[7]).toBeTag("span"); - expect(nodes[7]).toHaveText("Existing Node"); + expect(subject).toHaveSameHtml(` + HTML Text Node + Span from HTML + Standalone Text Node + Standalone Element Node + Text Node from NodeList + Element Node from NodeList + Text Node + Existing Node + `); }); }); diff --git a/src/manipulation/append.js b/src/manipulation/append.js index fac55c8..11346da 100644 --- a/src/manipulation/append.js +++ b/src/manipulation/append.js @@ -31,6 +31,4 @@ export default function append( appendNodeList(element, insertable); } }); - - element.normalize(); } diff --git a/src/manipulation/prepend.js b/src/manipulation/prepend.js index 8edb5c1..b87eef0 100644 --- a/src/manipulation/prepend.js +++ b/src/manipulation/prepend.js @@ -31,6 +31,4 @@ export default function prepend( prependNodeList(element, insertable); } }); - - element.normalize(); }