-
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.
Rewrite specs for
prepend
and append
- Loading branch information
Showing
5 changed files
with
83 additions
and
257 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 |
---|---|---|
@@ -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 }); |
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
Oops, something went wrong.