Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(matchers): add custom jest matchers (#13)
* Improving API's for testing. * Improving Apis * Adding all contributions * Fixing wrong url * Fixing review comments & making colorful assertions :) * Removing unwanted changes * Fixing review comments * removing unwanted comments * Adding test cases for the coverage * removing commented code and making few changes to the contribution file * Updating the readme * Making line break changes * Update README.md
- Loading branch information
1 parent
50aa1bc
commit 18104fe
Showing
10 changed files
with
168 additions
and
15 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
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
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 @@ | ||
require('./dist/extend-expect') |
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
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
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
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,7 @@ | ||
import expect from 'expect' //eslint-disable-line import/no-extraneous-dependencies | ||
import extensions from './jest-extensions' | ||
|
||
const {toBeInTheDOM, toHaveTextContent} = extensions | ||
expect.extend({toBeInTheDOM, toHaveTextContent}) | ||
|
||
export default expect |
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,78 @@ | ||
import {matcherHint, printReceived, printExpected} from 'jest-matcher-utils' //eslint-disable-line import/no-extraneous-dependencies | ||
import {matches} from './utils' | ||
|
||
function getDisplayName(subject) { | ||
if (subject && subject.constructor) { | ||
return subject.constructor.name | ||
} else { | ||
return typeof subject | ||
} | ||
} | ||
|
||
const assertMessage = (assertionName, message, received, expected) => | ||
`${matcherHint(`${assertionName}`, 'received', '')} \n${message}: ` + | ||
`${printExpected(expected)} \nReceived: ${printReceived(received)}` | ||
|
||
const extensions = { | ||
toBeInTheDOM(received) { | ||
getDisplayName(received) | ||
if (received) { | ||
return { | ||
message: | ||
`${matcherHint( | ||
'.not.toBeInTheDOM', | ||
'received', | ||
'', | ||
)} Expected the element not to be present` + | ||
`\nReceived : ${printReceived(received)}`, | ||
pass: true, | ||
} | ||
} else { | ||
return { | ||
message: | ||
`${matcherHint( | ||
'.toBeInTheDOM', | ||
'received', | ||
'', | ||
)} Expected the element to be present` + | ||
`\nReceived : ${printReceived(received)}`, | ||
pass: false, | ||
} | ||
} | ||
}, | ||
|
||
toHaveTextContent(htmlElement, checkWith) { | ||
if (!(htmlElement instanceof HTMLElement)) | ||
throw new Error( | ||
`The given subject is a ${getDisplayName( | ||
htmlElement, | ||
)}, not an HTMLElement`, | ||
) | ||
|
||
const textContent = htmlElement.textContent | ||
const pass = matches(textContent, htmlElement, checkWith) | ||
if (pass) { | ||
return { | ||
message: assertMessage( | ||
'.not.toHaveTextContent', | ||
'Expected value not equals to', | ||
htmlElement, | ||
checkWith, | ||
), | ||
pass: true, | ||
} | ||
} else { | ||
return { | ||
message: assertMessage( | ||
'.toHaveTextContent', | ||
'Expected value equals to', | ||
htmlElement, | ||
checkWith, | ||
), | ||
pass: false, | ||
} | ||
} | ||
}, | ||
} | ||
|
||
export default extensions |
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
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,10 @@ | ||
//eslint-disable-next-line import/prefer-default-export | ||
export function matches(textToMatch, node, matcher) { | ||
if (typeof matcher === 'string') { | ||
return textToMatch.toLowerCase().includes(matcher.toLowerCase()) | ||
} else if (typeof matcher === 'function') { | ||
return matcher(textToMatch, node) | ||
} else { | ||
return matcher.test(textToMatch) | ||
} | ||
} |