@testing-library/jest-dom
Advanced tools
Comparing version 4.1.2 to 4.2.0
@@ -114,2 +114,8 @@ "use strict"; | ||
}); | ||
Object.defineProperty(exports, "toBeChecked", { | ||
enumerable: true, | ||
get: function () { | ||
return _toBeChecked.toBeChecked; | ||
} | ||
}); | ||
@@ -146,2 +152,4 @@ var _toBeInTheDom = require("./to-be-in-the-dom"); | ||
var _toHaveValue = require("./to-have-value"); | ||
var _toHaveValue = require("./to-have-value"); | ||
var _toBeChecked = require("./to-be-checked"); |
@@ -20,3 +20,3 @@ "use strict"; | ||
if (htmlElement.tagName.toLowerCase() === 'input' && ['checkbox', 'radio'].includes(htmlElement.type)) { | ||
throw new Error('input with type=checkbox or type=radio cannot be used with .toHaveValue(). Use .toHaveFormValues() instead'); | ||
throw new Error('input with type=checkbox or type=radio cannot be used with .toHaveValue(). Use .toBeChecked() for type=checkbox or .toHaveFormValues() instead'); | ||
} | ||
@@ -23,0 +23,0 @@ |
@@ -27,3 +27,4 @@ declare namespace jest { | ||
toHaveValue(value?: string | string[] | number): R | ||
toBeChecked(): R | ||
} | ||
} |
{ | ||
"name": "@testing-library/jest-dom", | ||
"version": "4.1.2", | ||
"version": "4.2.0", | ||
"description": "Custom jest matchers to test the state of the DOM", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
111
README.md
@@ -69,2 +69,3 @@ <div align="center"> | ||
- [`toHaveValue`](#tohavevalue) | ||
- [`toBeChecked`](#tobechecked) | ||
- [Deprecated matchers](#deprecated-matchers) | ||
@@ -101,3 +102,4 @@ - [`toBeInTheDOM`](#tobeinthedom) | ||
> Note: If you're using TypeScript, make sure your setup file is a `.ts` and not a `.js` to include the necessary types. | ||
> Note: If you're using TypeScript, make sure your setup file is a `.ts` and not | ||
> a `.js` to include the necessary types. | ||
@@ -113,3 +115,5 @@ Alternatively, you can selectively import only the matchers you intend to use, | ||
> Note: when using TypeScript, this way of importing matchers won't provide the necessary type definitions. More on this [here](https://github.com/testing-library/jest-dom/pull/11#issuecomment-387817459). | ||
> Note: when using TypeScript, this way of importing matchers won't provide the | ||
> necessary type definitions. More on this | ||
> [here](https://github.com/testing-library/jest-dom/pull/11#issuecomment-387817459). | ||
@@ -925,3 +929,4 @@ ## Custom matchers | ||
of `<input type="checkbox">` and `<input type="radio">`, which can be | ||
meaningfully matched only using [`toHaveFormValue`](#tohaveformvalues). | ||
meaningfully matched only using [`toBeChecked`](#tobechecked) or | ||
[`toHaveFormValue`](#tohaveformvalues). | ||
@@ -974,2 +979,94 @@ For all other form elements, the value is matched using the same algorithm as in | ||
<hr /> | ||
### `toBeChecked` | ||
```typescript | ||
toBeChecked() | ||
``` | ||
This allows you to check whether the given element is checked. It accepts an | ||
`input` of type `checkbox` or `radio` and elements with a `role` of `checkbox` | ||
or `radio` with a valid `aria-checked` attribute of `"true"` or `"false"`. | ||
#### Examples | ||
```html | ||
<input type="checkbox" checked data-testid="input-checkbox-checked" /> | ||
<input type="checkbox" data-testid="input-checkbox-unchecked" /> | ||
<div role="checkbox" aria-checked="true" data-testid="aria-checkbox-checked" /> | ||
<div | ||
role="checkbox" | ||
aria-checked="false" | ||
data-testid="aria-checkbox-unchecked" | ||
/> | ||
<input type="radio" checked value="foo" data-testid="input-radio-checked" /> | ||
<input type="radio" value="foo" data-testid="input-radio-unchecked" /> | ||
<div role="radio" aria-checked="true" data-testid="aria-radio-checked" /> | ||
<div role="radio" aria-checked="false" data-testid="aria-radio-unchecked" /> | ||
``` | ||
##### Using document.querySelector | ||
```javascript | ||
const inputCheckboxChecked = document.querySelector( | ||
'[data-testid="input-checkbox-checked"]', | ||
) | ||
const inputCheckboxUnchecked = document.querySelector( | ||
'[data-testid="input-checkbox-unchecked"]', | ||
) | ||
const ariaCheckboxChecked = document.querySelector( | ||
'[data-testid="aria-checkbox-checked"]', | ||
) | ||
const ariaCheckboxUnchecked = document.querySelector( | ||
'[data-testid="aria-checkbox-unchecked"]', | ||
) | ||
expect(inputCheckboxChecked).toBeChecked() | ||
expect(inputCheckboxUnchecked).not.toBeChecked() | ||
expect(ariaCheckboxChecked).toBeChecked() | ||
expect(ariaCheckboxUnchecked).not.toBeChecked() | ||
const inputRadioChecked = document.querySelector( | ||
'[data-testid="input-radio-checked"]', | ||
) | ||
const inputRadioUnchecked = document.querySelector( | ||
'[data-testid="input-radio-unchecked"]', | ||
) | ||
const ariaRadioChecked = document.querySelector( | ||
'[data-testid="aria-radio-checked"]', | ||
) | ||
const ariaRadioUnchecked = document.querySelector( | ||
'[data-testid="aria-radio-unchecked"]', | ||
) | ||
expect(inputRadioChecked).toBeChecked() | ||
expect(inputRadioUnchecked).not.toBeChecked() | ||
expect(ariaRadioChecked).toBeChecked() | ||
expect(ariaRadioUnchecked).not.toBeChecked() | ||
``` | ||
##### Using DOM Testing Library | ||
```javascript | ||
const {getByTestId} = render(/* Rendered HTML */) | ||
const inputCheckboxChecked = getByTestId('input-checkbox-checked') | ||
const inputCheckboxUnchecked = getByTestId('input-checkbox-unchecked') | ||
const ariaCheckboxChecked = getByTestId('aria-checkbox-checked') | ||
const ariaCheckboxUnchecked = getByTestId('aria-checkbox-unchecked') | ||
expect(inputCheckboxChecked).toBeChecked() | ||
expect(inputCheckboxUnchecked).not.toBeChecked() | ||
expect(ariaCheckboxChecked).toBeChecked() | ||
expect(ariaCheckboxUnchecked).not.toBeChecked() | ||
const inputRadioChecked = getByTestId('input-radio-checked') | ||
const inputRadioUnchecked = getByTestId('input-radio-unchecked') | ||
const ariaRadioChecked = getByTestId('aria-radio-checked') | ||
const ariaRadioUnchecked = getByTestId('aria-radio-unchecked') | ||
expect(inputRadioChecked).toBeChecked() | ||
expect(inputRadioUnchecked).not.toBeChecked() | ||
expect(ariaRadioChecked).toBeChecked() | ||
expect(ariaRadioUnchecked).not.toBeChecked() | ||
``` | ||
## Deprecated matchers | ||
@@ -1011,4 +1108,5 @@ | ||
This whole library was extracted out of Kent C. Dodds' [DOM Testing Library][dom-testing-library], | ||
which was in turn extracted out of [React Testing Library][react-testing-library]. | ||
This whole library was extracted out of Kent C. Dodds' [DOM Testing | ||
Library][dom-testing-library], which was in turn extracted out of [React Testing | ||
Library][react-testing-library]. | ||
@@ -1030,3 +1128,4 @@ The intention is to make this available to be used independently of these other | ||
This library follows the same guiding principles as its mother library [DOM | ||
Testing Library][dom-testing-library]. Go [check them out][guiding-principle] for more details. | ||
Testing Library][dom-testing-library]. Go [check them out][guiding-principle] | ||
for more details. | ||
@@ -1033,0 +1132,0 @@ Additionally, with respect to custom DOM matchers, this library aims to maintain |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
87386
26
831
1231