-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters