Skip to content

Commit

Permalink
Add new core matchers for specs
Browse files Browse the repository at this point in the history
  • Loading branch information
demiazz committed Jun 7, 2017
1 parent 145efa8 commit 2e13acd
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
72 changes: 72 additions & 0 deletions build/jasmine/matchers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
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 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 };
}
};
}

function toHaveText() {
return {
compare(actual, expected) {
if (actual.nodeType !== 1 && actual.nodeType !== 3) {
return { pass: false, message: "Expected text or element node" };
}

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}"`;

return { pass, message };
}
};
}

jasmine.Expectation.addCoreMatchers({
toBeTextNode,
toBeElementNode,
toBeTag,
toHaveText
});
5 changes: 5 additions & 0 deletions build/karma/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ module.exports = {
frameworks: ["jasmine"],

files: [
{
pattern: "build/jasmine/matchers.js",
watched: process.env.CI !== "true"
},
{
pattern: "build/jasmine/setup.js",
watched: process.env.CI !== "true"
Expand All @@ -32,6 +36,7 @@ module.exports = {
],

preprocessors: {
"build/jasmine/matchers.js": ["rollup"],
"build/jasmine/setup.js": ["rollup"],
"build/jasmine/fixtures.js": ["rollup"],
"build/jasmine/helpers.js": ["rollup"],
Expand Down

0 comments on commit 2e13acd

Please sign in to comment.