This repository has been archived by the owner on Oct 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Smarter act and text matchers (#626)
- Loading branch information
Showing
4 changed files
with
118 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { | ||
matcherHint, | ||
printReceived, | ||
printExpected, | ||
RECEIVED_COLOR as receivedColor, | ||
INVERTED_COLOR as invertedColor, | ||
} from 'jest-matcher-utils'; | ||
import {Node} from './types'; | ||
import {assertIsNode} from './utilities'; | ||
|
||
export function toContainReactText<Props>( | ||
this: jest.MatcherUtils, | ||
node: Node<Props>, | ||
text: string, | ||
) { | ||
assertIsNode(node, { | ||
expectation: 'toContainReactText', | ||
isNot: this.isNot, | ||
}); | ||
|
||
const nodeText = node.text(); | ||
const matchIndex = nodeText.indexOf(text); | ||
const pass = matchIndex >= 0; | ||
|
||
const message = pass | ||
? () => | ||
`${matcherHint('.not.toContainReactText', node.toString())}\n\n` + | ||
`Expected the React element:\n ${receivedColor(node.toString())}\n` + | ||
`Not to contain text:\n ${printExpected(text)}\n` + | ||
`But it did:\n ${printReceivedWithHighlight( | ||
nodeText, | ||
matchIndex, | ||
text.length, | ||
)}\n` | ||
: () => | ||
`${matcherHint('.not.toContainReactText', node.toString())}\n\n` + | ||
`Expected the React element:\n ${receivedColor(node.toString())}\n` + | ||
`With text content:\n ${printReceived(nodeText)}\n` + | ||
`To contain string:\n ${printExpected(text)}\n`; | ||
|
||
return {pass, message}; | ||
} | ||
|
||
export function toContainReactHtml<Props>( | ||
this: jest.MatcherUtils, | ||
node: Node<Props>, | ||
text: string, | ||
) { | ||
assertIsNode(node, { | ||
expectation: 'toContainReactHtml', | ||
isNot: this.isNot, | ||
}); | ||
|
||
const nodeHtml = node.html(); | ||
const matchIndex = nodeHtml.indexOf(text); | ||
const pass = matchIndex >= 0; | ||
|
||
const message = pass | ||
? () => | ||
`${matcherHint('.not.toContainReactHtml', node.toString())}\n\n` + | ||
`Expected the React element:\n ${receivedColor(node.toString())}\n` + | ||
`Not to contain HTML:\n ${printExpected(text)}\n` + | ||
`But it did:\n ${printReceivedWithHighlight( | ||
nodeHtml, | ||
matchIndex, | ||
text.length, | ||
)}\n` | ||
: () => | ||
`${matcherHint('.not.toContainReactHtml', node.toString())}\n\n` + | ||
`Expected the React element:\n ${receivedColor(node.toString())}\n` + | ||
`With HTML content:\n ${printReceived(nodeHtml)}\n` + | ||
`To contain HTML:\n ${printExpected(text)}\n`; | ||
|
||
return {pass, message}; | ||
} | ||
|
||
function printReceivedWithHighlight( | ||
text: string, | ||
start: number, | ||
length: number, | ||
) { | ||
return receivedColor( | ||
`"${text.slice(0, start)}${invertedColor( | ||
text.slice(start, start + length), | ||
)}${text.slice(start + length)}"`, | ||
); | ||
} |
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