|
16 | 16 | [![downloads][downloads-badge]][npmtrends]
|
17 | 17 | [![MIT License][license-badge]][license]
|
18 | 18 |
|
19 |
| -[](#contributors) |
| 19 | +[](#contributors) |
20 | 20 | [![PRs Welcome][prs-badge]][prs]
|
21 | 21 | [![Code of Conduct][coc-badge]][coc]
|
22 | 22 |
|
@@ -49,6 +49,7 @@ to maintain.
|
49 | 49 | - [`toBeEnabled`](#tobeenabled)
|
50 | 50 | - [`toBeEmpty`](#tobeempty)
|
51 | 51 | - [`toBeInTheDocument`](#tobeinthedocument)
|
| 52 | + - [`toBeRequired`](#toberequired) |
52 | 53 | - [`toBeVisible`](#tobevisible)
|
53 | 54 | - [`toContainElement`](#tocontainelement)
|
54 | 55 | - [`toContainHTML`](#tocontainhtml)
|
@@ -234,6 +235,74 @@ expect(
|
234 | 235 |
|
235 | 236 | <hr />
|
236 | 237 |
|
| 238 | +### `toBeRequired` |
| 239 | +
|
| 240 | +```typescript |
| 241 | +toBeRequired() |
| 242 | +``` |
| 243 | +
|
| 244 | +This allows you to check if an form element is currently required. |
| 245 | +
|
| 246 | +An element is required if it is having a `required` or `aria-required="true"` attribute. |
| 247 | +
|
| 248 | +#### Examples |
| 249 | +
|
| 250 | +```html |
| 251 | +<input data-testid="required-input" required /> |
| 252 | +<input data-testid="aria-required-input" aria-required="true" /> |
| 253 | +<input data-testid="conflicted-input" required aria-required="false" /> |
| 254 | +<input data-testid="aria-not-required-input" aria-required="false" /> |
| 255 | +<input data-testid="optional-input" /> |
| 256 | +<input data-testid="unsupported-type" type="image" required /> |
| 257 | +<select data-testid="select" required></select> |
| 258 | +<textarea data-testid="textarea" required></textarea> |
| 259 | +<div data-testid="supported-role" role="tree" required></div> |
| 260 | +<div data-testid="supported-role-aria" role="tree" aria-required="true"></div> |
| 261 | +``` |
| 262 | +
|
| 263 | +##### Using document.querySelector |
| 264 | +
|
| 265 | +```javascript |
| 266 | +expect(document.querySelector('[data-testid="required-input"]')).toBeRequired() |
| 267 | +expect( |
| 268 | + document.querySelector('[data-testid="aria-required-input"]'), |
| 269 | +).toBeRequired() |
| 270 | +expect( |
| 271 | + document.querySelector('[data-testid="conflicted-input"]'), |
| 272 | +).toBeRequired() |
| 273 | +expect( |
| 274 | + document.querySelector('[data-testid="aria-not-required-input"]'), |
| 275 | +).not.toBeRequired() |
| 276 | +expect( |
| 277 | + document.querySelector('[data-testid="unsupported-type"]'), |
| 278 | +).not.toBeRequired() |
| 279 | +expect(document.querySelector('[data-testid="select"]')).toBeRequired() |
| 280 | +expect(document.querySelector('[data-testid="textarea"]')).toBeRequired() |
| 281 | +expect( |
| 282 | + document.querySelector('[data-testid="supported-role"]'), |
| 283 | +).not.toBeRequired() |
| 284 | +expect( |
| 285 | + document.querySelector('[data-testid="supported-role-aria"]'), |
| 286 | +).toBeRequired() |
| 287 | +``` |
| 288 | +
|
| 289 | +##### Using dom-testing-library |
| 290 | +
|
| 291 | +```javascript |
| 292 | +expect(getByTestId(container, 'required-input')).toBeRequired() |
| 293 | +expect(getByTestId(container, 'aria-required-input')).toBeRequired() |
| 294 | +expect(getByTestId(container, 'conflicted-input')).toBeRequired() |
| 295 | +expect(getByTestId(container, 'aria-not-required-input')).not.toBeRequired() |
| 296 | +expect(getByTestId(container, 'optional-input')).not.toBeRequired() |
| 297 | +expect(getByTestId(container, 'unsupported-type')).not.toBeRequired() |
| 298 | +expect(getByTestId(container, 'select')).toBeRequired() |
| 299 | +expect(getByTestId(container, 'textarea')).toBeRequired() |
| 300 | +expect(getByTestId(container, 'supported-role')).not.toBeRequired() |
| 301 | +expect(getByTestId(container, 'supported-role-aria')).toBeRequired() |
| 302 | +``` |
| 303 | +
|
| 304 | +<hr /> |
| 305 | +
|
237 | 306 | ### `toBeVisible`
|
238 | 307 |
|
239 | 308 | ```typescript
|
@@ -763,7 +832,8 @@ Thanks goes to these people ([emoji key][emojis]):
|
763 | 832 | | :---: | :---: | :---: | :---: | :---: | :---: | :---: |
|
764 | 833 | | [<img src="https://avatars1.githubusercontent.com/u/1241511?s=460&v=4" width="100px;" alt="Anto Aravinth"/><br /><sub><b>Anto Aravinth</b></sub>](https://github.com/antoaravinth)<br />[💻](https://github.com/testing-library/jest-dom/commits?author=antoaravinth "Code") [⚠️](https://github.com/testing-library/jest-dom/commits?author=antoaravinth "Tests") [📖](https://github.com/testing-library/jest-dom/commits?author=antoaravinth "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/3462296?v=4" width="100px;" alt="Jonah Moses"/><br /><sub><b>Jonah Moses</b></sub>](https://github.com/JonahMoses)<br />[📖](https://github.com/testing-library/jest-dom/commits?author=JonahMoses "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/4002543?v=4" width="100px;" alt="Łukasz Gandecki"/><br /><sub><b>Łukasz Gandecki</b></sub>](http://team.thebrain.pro)<br />[💻](https://github.com/testing-library/jest-dom/commits?author=lgandecki "Code") [⚠️](https://github.com/testing-library/jest-dom/commits?author=lgandecki "Tests") [📖](https://github.com/testing-library/jest-dom/commits?author=lgandecki "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/498274?v=4" width="100px;" alt="Ivan Babak"/><br /><sub><b>Ivan Babak</b></sub>](https://sompylasar.github.io)<br />[🐛](https://github.com/testing-library/jest-dom/issues?q=author%3Asompylasar "Bug reports") [🤔](#ideas-sompylasar "Ideas, Planning, & Feedback") | [<img src="https://avatars3.githubusercontent.com/u/4439618?v=4" width="100px;" alt="Jesse Day"/><br /><sub><b>Jesse Day</b></sub>](https://github.com/jday3)<br />[💻](https://github.com/testing-library/jest-dom/commits?author=jday3 "Code") | [<img src="https://avatars0.githubusercontent.com/u/15199?v=4" width="100px;" alt="Ernesto García"/><br /><sub><b>Ernesto García</b></sub>](http://gnapse.github.io)<br />[💻](https://github.com/testing-library/jest-dom/commits?author=gnapse "Code") [📖](https://github.com/testing-library/jest-dom/commits?author=gnapse "Documentation") [⚠️](https://github.com/testing-library/jest-dom/commits?author=gnapse "Tests") | [<img src="https://avatars0.githubusercontent.com/u/79312?v=4" width="100px;" alt="Mark Volkmann"/><br /><sub><b>Mark Volkmann</b></sub>](http://ociweb.com/mark/)<br />[🐛](https://github.com/testing-library/jest-dom/issues?q=author%3Amvolkmann "Bug reports") [💻](https://github.com/testing-library/jest-dom/commits?author=mvolkmann "Code") |
|
765 | 834 | | [<img src="https://avatars1.githubusercontent.com/u/1659099?v=4" width="100px;" alt="smacpherson64"/><br /><sub><b>smacpherson64</b></sub>](https://github.com/smacpherson64)<br />[💻](https://github.com/testing-library/jest-dom/commits?author=smacpherson64 "Code") [📖](https://github.com/testing-library/jest-dom/commits?author=smacpherson64 "Documentation") [⚠️](https://github.com/testing-library/jest-dom/commits?author=smacpherson64 "Tests") | [<img src="https://avatars2.githubusercontent.com/u/132233?v=4" width="100px;" alt="John Gozde"/><br /><sub><b>John Gozde</b></sub>](https://github.com/jgoz)<br />[🐛](https://github.com/testing-library/jest-dom/issues?q=author%3Ajgoz "Bug reports") [💻](https://github.com/testing-library/jest-dom/commits?author=jgoz "Code") | [<img src="https://avatars2.githubusercontent.com/u/7830590?v=4" width="100px;" alt="Iwona"/><br /><sub><b>Iwona</b></sub>](https://github.com/callada)<br />[💻](https://github.com/testing-library/jest-dom/commits?author=callada "Code") [📖](https://github.com/testing-library/jest-dom/commits?author=callada "Documentation") [⚠️](https://github.com/testing-library/jest-dom/commits?author=callada "Tests") | [<img src="https://avatars0.githubusercontent.com/u/840609?v=4" width="100px;" alt="Lewis"/><br /><sub><b>Lewis</b></sub>](https://github.com/6ewis)<br />[💻](https://github.com/testing-library/jest-dom/commits?author=6ewis "Code") | [<img src="https://avatars3.githubusercontent.com/u/2339362?v=4" width="100px;" alt="Leandro Lourenci"/><br /><sub><b>Leandro Lourenci</b></sub>](https://blog.lourenci.com/)<br />[🐛](https://github.com/testing-library/jest-dom/issues?q=author%3Alourenci "Bug reports") [📖](https://github.com/testing-library/jest-dom/commits?author=lourenci "Documentation") [💻](https://github.com/testing-library/jest-dom/commits?author=lourenci "Code") [⚠️](https://github.com/testing-library/jest-dom/commits?author=lourenci "Tests") | [<img src="https://avatars1.githubusercontent.com/u/626420?v=4" width="100px;" alt="Shukhrat Mukimov"/><br /><sub><b>Shukhrat Mukimov</b></sub>](https://github.com/mufasa71)<br />[🐛](https://github.com/testing-library/jest-dom/issues?q=author%3Amufasa71 "Bug reports") | [<img src="https://avatars3.githubusercontent.com/u/1481264?v=4" width="100px;" alt="Roman Usherenko"/><br /><sub><b>Roman Usherenko</b></sub>](https://github.com/dreyks)<br />[💻](https://github.com/testing-library/jest-dom/commits?author=dreyks "Code") [⚠️](https://github.com/testing-library/jest-dom/commits?author=dreyks "Tests") |
|
766 |
| -| [<img src="https://avatars1.githubusercontent.com/u/648?v=4" width="100px;" alt="Joe Hsu"/><br /><sub><b>Joe Hsu</b></sub>](http://josephhsu.com)<br />[📖](https://github.com/testing-library/jest-dom/commits?author=jhsu "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/3068563?v=4" width="100px;" alt="Haz"/><br /><sub><b>Haz</b></sub>](https://twitter.com/diegohaz)<br />[🐛](https://github.com/testing-library/jest-dom/issues?q=author%3Adiegohaz "Bug reports") [💻](https://github.com/testing-library/jest-dom/commits?author=diegohaz "Code") | [<img src="https://avatars3.githubusercontent.com/u/463904?v=4" width="100px;" alt="Revath S Kumar"/><br /><sub><b>Revath S Kumar</b></sub>](https://blog.revathskumar.com)<br />[💻](https://github.com/testing-library/jest-dom/commits?author=revathskumar "Code") | |
| 835 | +| [<img src="https://avatars1.githubusercontent.com/u/648?v=4" width="100px;" alt="Joe Hsu"/><br /><sub><b>Joe Hsu</b></sub>](http://josephhsu.com)<br />[📖](https://github.com/testing-library/jest-dom/commits?author=jhsu "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/3068563?v=4" width="100px;" alt="Haz"/><br /><sub><b>Haz</b></sub>](https://twitter.com/diegohaz)<br />[🐛](https://github.com/testing-library/jest-dom/issues?q=author%3Adiegohaz "Bug reports") [💻](https://github.com/testing-library/jest-dom/commits?author=diegohaz "Code") | [<img src="https://avatars3.githubusercontent.com/u/463904?v=4" width="100px;" alt="Revath S Kumar"/><br /><sub><b>Revath S Kumar</b></sub>](https://blog.revathskumar.com)<br />[💻](https://github.com/testing-library/jest-dom/commits?author=revathskumar "Code") | [<img src="https://avatars0.githubusercontent.com/u/4989733?v=4" width="100px;" alt="hiwelo."/><br /><sub><b>hiwelo.</b></sub>](https://raccoon.studio)<br />[💻](https://github.com/testing-library/jest-dom/commits?author=hiwelo "Code") [🤔](#ideas-hiwelo "Ideas, Planning, & Feedback") [⚠️](https://github.com/testing-library/jest-dom/commits?author=hiwelo "Tests") | |
| 836 | +
|
767 | 837 | <!-- ALL-CONTRIBUTORS-LIST:END -->
|
768 | 838 |
|
769 | 839 | This project follows the [all-contributors][all-contributors] specification.
|
|
0 commit comments