diff --git a/build/jasmine/matchers.js b/build/jasmine/matchers.js new file mode 100644 index 0000000..f5c4bf3 --- /dev/null +++ b/build/jasmine/matchers.js @@ -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 +}); diff --git a/build/karma/base.js b/build/karma/base.js index c1f2526..093f418 100644 --- a/build/karma/base.js +++ b/build/karma/base.js @@ -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" @@ -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"],