Skip to content

Commit

Permalink
Rewrite specs for prepend and append
Browse files Browse the repository at this point in the history
  • Loading branch information
demiazz committed Jun 10, 2017
1 parent 618205b commit 475f61a
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 257 deletions.
74 changes: 14 additions & 60 deletions build/jasmine/matchers.js
Original file line number Diff line number Diff line change
@@ -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+</gm, "<")
.replace(/>\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 });
133 changes: 36 additions & 97 deletions spec/manipulation/append.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,21 @@ describe("append", () => {
it("inserts given HTML string to end of element", () => {
useFixture(`
<div class="root">
Text Node
<span>Existing Node</span>
</div>
`);

const subject = document.querySelector(".root");

append(subject, "Text Node<span>Span Node</span>");

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 Node<span>Span Node</span>");

expect(nodes[2]).toBeElementNode();
expect(nodes[2]).toBeTag("span");
expect(nodes[2]).toHaveText("Span Node");
expect(subject).toHaveSameHtml(`
Text Node
<span>Existing Node</span>
New Text Node
<span>Span Node</span>
`);
});

it("inserts given node to begin of element", () => {
Expand All @@ -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
<span>Existing Node</span>
<span>Span Node</span>
New Text Node
`);
});

it("inserts given nodes from node list to begin of element", () => {
Expand All @@ -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
<span>Existing Node</span>
New Text Node
<span>Span Node</span>
`);
});

it("inserts given document fragment to begin of element", () => {
Expand All @@ -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
<span>Existing Node</span>
New Text Node
<span>Span Node</span>
`);
});

it("inserts given multiple arguments to begin of element", () => {
Expand Down Expand Up @@ -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
<span>Existing Node</span>
HTML Text Node
<span>Span from HTML</span>
Standalone Text Node
<span>Standalone Element Node</span>
Text Node from NodeList
<span>Element Node from NodeList</span>
`);
});
});
Loading

0 comments on commit 475f61a

Please sign in to comment.