-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Improving api #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improving api #13
Changes from 12 commits
5a2c05f
dd727d0
485e8fb
96a1a9e
cc2a1c3
3931a69
3ae81f6
987c4f3
57fd424
d1157a2
0cdb02b
74d8205
8348852
492645b
e222206
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ | |
[![downloads][downloads-badge]][npmtrends] | ||
[![MIT License][license-badge]][license] | ||
|
||
[](#contributors) | ||
[](#contributors) | ||
[![PRs Welcome][prs-badge]][prs] | ||
[![Code of Conduct][coc-badge]][coc] | ||
|
||
|
@@ -219,6 +219,31 @@ const usernameInputElement = getByTestId('username-input') | |
> Learn more about `data-testid`s from the blog post | ||
> ["Making your UI tests resilient to change"][data-testid-blog-post] | ||
|
||
#### `toBeInTheDOM` | ||
|
||
There are two simple API which extend the `expect` API of jest for making assertions easier. | ||
The first one `toBeInTheDOM` which allows you to assert whether an element present in the DOM or not | ||
|
||
```javascript | ||
import 'extend-expect' //adds few API to jest's extend | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will be |
||
|
||
render(<span data-testid="count-value">2</span>) | ||
expect(queryByTestId('count-value')).toBeInTheDOM() | ||
expect(queryByTestId('count-value1')).not.toBeInTheDOM() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a comment below that says: "Note: for this assertion, make sure you use a |
||
``` | ||
|
||
#### `toHaveTextContent` | ||
|
||
This API allows you to check whether the given element has a text content or not. | ||
|
||
```javascript | ||
import 'extend-expect' //adds few API to jest's extend | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here as well. Also, please add a space after the |
||
|
||
render(<span data-testid="count-value">2</span>) | ||
expect(queryByTestId('count-value')).toHaveTextContent('2') | ||
expect(queryByTestId('count-value')).not.toHaveTextContent('21') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For these two examples, could we use |
||
``` | ||
|
||
## `TextMatch` | ||
|
||
Several APIs accept a `TextMatch` which can be a `string`, `regex` or a | ||
|
@@ -560,12 +585,10 @@ light-weight, simple, and understandable. | |
Thanks goes to these people ([emoji key][emojis]): | ||
|
||
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --> | ||
|
||
<!-- prettier-ignore --> | ||
| [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub><b>Kent C. Dodds</b></sub>](https://kentcdodds.com)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=kentcdodds "Code") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/kentcdodds/react-testing-library/commits?author=kentcdodds "Tests") | [<img src="https://avatars1.githubusercontent.com/u/2430381?v=4" width="100px;"/><br /><sub><b>Ryan Castner</b></sub>](http://audiolion.github.io)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=audiolion "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/8008023?v=4" width="100px;"/><br /><sub><b>Daniel Sandiego</b></sub>](https://www.dnlsandiego.com)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=dnlsandiego "Code") | [<img src="https://avatars2.githubusercontent.com/u/12592677?v=4" width="100px;"/><br /><sub><b>Paweł Mikołajczyk</b></sub>](https://github.com/Miklet)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=Miklet "Code") | [<img src="https://avatars3.githubusercontent.com/u/464978?v=4" width="100px;"/><br /><sub><b>Alejandro Ñáñez Ortiz</b></sub>](http://co.linkedin.com/in/alejandronanez/)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=alejandronanez "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/1402095?v=4" width="100px;"/><br /><sub><b>Matt Parrish</b></sub>](https://github.com/pbomb)<br />[🐛](https://github.com/kentcdodds/react-testing-library/issues?q=author%3Apbomb "Bug reports") [💻](https://github.com/kentcdodds/react-testing-library/commits?author=pbomb "Code") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=pbomb "Documentation") [⚠️](https://github.com/kentcdodds/react-testing-library/commits?author=pbomb "Tests") | [<img src="https://avatars1.githubusercontent.com/u/1288694?v=4" width="100px;"/><br /><sub><b>Justin Hall</b></sub>](https://github.com/wKovacs64)<br />[📦](#platform-wKovacs64 "Packaging/porting to new platform") | | ||
| :---: | :---: | :---: | :---: | :---: | :---: | :---: | | ||
| [<img src="https://avatars2.githubusercontent.com/u/3462296?v=4" width="100px;"/><br /><sub><b>Jonah Moses</b></sub>](https://github.com/JonahMoses)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=JonahMoses "Documentation") | | ||
|
||
| [<img src="https://avatars1.githubusercontent.com/u/1241511?s=460&v=4" width="100px;"/><br /><sub><b>Anto Aravinth</b></sub>](https://github.com/antoaravinth)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=antoaravinth "Code") [⚠️](https://github.com/kentcdodds/react-testing-library/commits?author=antoaravinth "Tests") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=antoaravinth "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/3462296?v=4" width="100px;"/><br /><sub><b>Jonah Moses</b></sub>](https://github.com/JonahMoses)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=JonahMoses "Documentation") | | ||
<!-- ALL-CONTRIBUTORS-LIST:END --> | ||
|
||
This project follows the [all-contributors][all-contributors] specification. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require('./dist/extend-expect') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perfect |
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 |
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 |
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) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we put these in a different section of the README (maybe right where it is).
So just change this to: