Skip to content

Commit 02c340d

Browse files
lourencignapse
authored andcommitted
feat: Add toBeEnabled matcher (#78)
1 parent dd310ee commit 02c340d

File tree

5 files changed

+169
-6
lines changed

5 files changed

+169
-6
lines changed

.all-contributorsrc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,9 @@
196196
"profile": "https://blog.lourenci.com/",
197197
"contributions": [
198198
"bug",
199-
"doc"
199+
"doc",
200+
"code",
201+
"test"
200202
]
201203
},
202204
{

README.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ to maintain.
4646
- [Usage](#usage)
4747
- [Custom matchers](#custom-matchers)
4848
- [`toBeDisabled`](#tobedisabled)
49+
- [`toBeEnabled`](#tobeenabled)
4950
- [`toBeEmpty`](#tobeempty)
5051
- [`toBeInTheDocument`](#tobeinthedocument)
5152
- [`toBeVisible`](#tobevisible)
@@ -144,6 +145,18 @@ expect(getByText(container, 'link')).not.toBeDisabled()
144145

145146
<hr />
146147

148+
### `toBeEnabled`
149+
150+
```typescript
151+
toBeEnabled()
152+
```
153+
154+
This allows you to check whether an element is not disabled from the user's perspective.
155+
156+
It works like `not.toBeDisabled()`. Use this matcher to avoid double negation in your tests.
157+
158+
<hr />
159+
147160
### `toBeEmpty`
148161

149162
```typescript
@@ -733,10 +746,10 @@ Thanks goes to these people ([emoji key][emojis]):
733746
734747
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
735748
<!-- prettier-ignore -->
736-
| [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;" alt="Kent C. Dodds"/><br /><sub><b>Kent C. Dodds</b></sub>](https://kentcdodds.com)<br />[💻](https://github.com/gnapse/jest-dom/commits?author=kentcdodds "Code") [📖](https://github.com/gnapse/jest-dom/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/gnapse/jest-dom/commits?author=kentcdodds "Tests") | [<img src="https://avatars1.githubusercontent.com/u/2430381?v=4" width="100px;" alt="Ryan Castner"/><br /><sub><b>Ryan Castner</b></sub>](http://audiolion.github.io)<br />[📖](https://github.com/gnapse/jest-dom/commits?author=audiolion "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/8008023?v=4" width="100px;" alt="Daniel Sandiego"/><br /><sub><b>Daniel Sandiego</b></sub>](https://www.dnlsandiego.com)<br />[💻](https://github.com/gnapse/jest-dom/commits?author=dnlsandiego "Code") | [<img src="https://avatars2.githubusercontent.com/u/12592677?v=4" width="100px;" alt="Paweł Mikołajczyk"/><br /><sub><b>Paweł Mikołajczyk</b></sub>](https://github.com/Miklet)<br />[💻](https://github.com/gnapse/jest-dom/commits?author=Miklet "Code") | [<img src="https://avatars3.githubusercontent.com/u/464978?v=4" width="100px;" alt="Alejandro Ñáñez Ortiz"/><br /><sub><b>Alejandro Ñáñez Ortiz</b></sub>](http://co.linkedin.com/in/alejandronanez/)<br />[📖](https://github.com/gnapse/jest-dom/commits?author=alejandronanez "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/1402095?v=4" width="100px;" alt="Matt Parrish"/><br /><sub><b>Matt Parrish</b></sub>](https://github.com/pbomb)<br />[🐛](https://github.com/gnapse/jest-dom/issues?q=author%3Apbomb "Bug reports") [💻](https://github.com/gnapse/jest-dom/commits?author=pbomb "Code") [📖](https://github.com/gnapse/jest-dom/commits?author=pbomb "Documentation") [⚠️](https://github.com/gnapse/jest-dom/commits?author=pbomb "Tests") | [<img src="https://avatars1.githubusercontent.com/u/1288694?v=4" width="100px;" alt="Justin Hall"/><br /><sub><b>Justin Hall</b></sub>](https://github.com/wKovacs64)<br />[📦](#platform-wKovacs64 "Packaging/porting to new platform") |
749+
| [<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/gnapse/jest-dom/commits?author=kentcdodds "Code") [📖](https://github.com/gnapse/jest-dom/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/gnapse/jest-dom/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/gnapse/jest-dom/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/gnapse/jest-dom/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/gnapse/jest-dom/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/gnapse/jest-dom/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/gnapse/jest-dom/issues?q=author%3Apbomb "Bug reports") [💻](https://github.com/gnapse/jest-dom/commits?author=pbomb "Code") [📖](https://github.com/gnapse/jest-dom/commits?author=pbomb "Documentation") [⚠️](https://github.com/gnapse/jest-dom/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") |
737750
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
738-
| [<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/gnapse/jest-dom/commits?author=antoaravinth "Code") [⚠️](https://github.com/gnapse/jest-dom/commits?author=antoaravinth "Tests") [📖](https://github.com/gnapse/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/gnapse/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/gnapse/jest-dom/commits?author=lgandecki "Code") [⚠️](https://github.com/gnapse/jest-dom/commits?author=lgandecki "Tests") [📖](https://github.com/gnapse/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/gnapse/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/gnapse/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/gnapse/jest-dom/commits?author=gnapse "Code") [📖](https://github.com/gnapse/jest-dom/commits?author=gnapse "Documentation") [⚠️](https://github.com/gnapse/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/gnapse/jest-dom/issues?q=author%3Amvolkmann "Bug reports") [💻](https://github.com/gnapse/jest-dom/commits?author=mvolkmann "Code") |
739-
| [<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/gnapse/jest-dom/commits?author=smacpherson64 "Code") [📖](https://github.com/gnapse/jest-dom/commits?author=smacpherson64 "Documentation") [⚠️](https://github.com/gnapse/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/gnapse/jest-dom/issues?q=author%3Ajgoz "Bug reports") [💻](https://github.com/gnapse/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/gnapse/jest-dom/commits?author=callada "Code") [📖](https://github.com/gnapse/jest-dom/commits?author=callada "Documentation") [⚠️](https://github.com/gnapse/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/gnapse/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/gnapse/jest-dom/issues?q=author%3Alourenci "Bug reports") [📖](https://github.com/gnapse/jest-dom/commits?author=lourenci "Documentation") | [<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/gnapse/jest-dom/issues?q=author%3Amufasa71 "Bug reports") |
751+
| [<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/gnapse/jest-dom/commits?author=antoaravinth "Code") [⚠️](https://github.com/gnapse/jest-dom/commits?author=antoaravinth "Tests") [📖](https://github.com/gnapse/jest-dom/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/gnapse/jest-dom/commits?author=JonahMoses "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/4002543?v=4" width="100px;"/><br /><sub><b>Łukasz Gandecki</b></sub>](http://team.thebrain.pro)<br />[💻](https://github.com/gnapse/jest-dom/commits?author=lgandecki "Code") [⚠️](https://github.com/gnapse/jest-dom/commits?author=lgandecki "Tests") [📖](https://github.com/gnapse/jest-dom/commits?author=lgandecki "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/498274?v=4" width="100px;"/><br /><sub><b>Ivan Babak</b></sub>](https://sompylasar.github.io)<br />[🐛](https://github.com/gnapse/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;"/><br /><sub><b>Jesse Day</b></sub>](https://github.com/jday3)<br />[💻](https://github.com/gnapse/jest-dom/commits?author=jday3 "Code") | [<img src="https://avatars0.githubusercontent.com/u/15199?v=4" width="100px;"/><br /><sub><b>Ernesto García</b></sub>](http://gnapse.github.io)<br />[💻](https://github.com/gnapse/jest-dom/commits?author=gnapse "Code") [📖](https://github.com/gnapse/jest-dom/commits?author=gnapse "Documentation") [⚠️](https://github.com/gnapse/jest-dom/commits?author=gnapse "Tests") | [<img src="https://avatars0.githubusercontent.com/u/79312?v=4" width="100px;"/><br /><sub><b>Mark Volkmann</b></sub>](http://ociweb.com/mark/)<br />[🐛](https://github.com/gnapse/jest-dom/issues?q=author%3Amvolkmann "Bug reports") [💻](https://github.com/gnapse/jest-dom/commits?author=mvolkmann "Code") |
752+
| [<img src="https://avatars1.githubusercontent.com/u/1659099?v=4" width="100px;"/><br /><sub><b>smacpherson64</b></sub>](https://github.com/smacpherson64)<br />[💻](https://github.com/gnapse/jest-dom/commits?author=smacpherson64 "Code") [📖](https://github.com/gnapse/jest-dom/commits?author=smacpherson64 "Documentation") [⚠️](https://github.com/gnapse/jest-dom/commits?author=smacpherson64 "Tests") | [<img src="https://avatars2.githubusercontent.com/u/132233?v=4" width="100px;"/><br /><sub><b>John Gozde</b></sub>](https://github.com/jgoz)<br />[🐛](https://github.com/gnapse/jest-dom/issues?q=author%3Ajgoz "Bug reports") [💻](https://github.com/gnapse/jest-dom/commits?author=jgoz "Code") | [<img src="https://avatars2.githubusercontent.com/u/7830590?v=4" width="100px;"/><br /><sub><b>Iwona</b></sub>](https://github.com/callada)<br />[💻](https://github.com/gnapse/jest-dom/commits?author=callada "Code") [📖](https://github.com/gnapse/jest-dom/commits?author=callada "Documentation") [⚠️](https://github.com/gnapse/jest-dom/commits?author=callada "Tests") | [<img src="https://avatars0.githubusercontent.com/u/840609?v=4" width="100px;"/><br /><sub><b>Lewis</b></sub>](https://github.com/6ewis)<br />[💻](https://github.com/gnapse/jest-dom/commits?author=6ewis "Code") | [<img src="https://avatars3.githubusercontent.com/u/2339362?v=4" width="100px;"/><br /><sub><b>Leandro Lourenci</b></sub>](https://blog.lourenci.com/)<br />[🐛](https://github.com/gnapse/jest-dom/issues?q=author%3Alourenci "Bug reports") [📖](https://github.com/gnapse/jest-dom/commits?author=lourenci "Documentation") [💻](https://github.com/gnapse/jest-dom/commits?author=lourenci "Code") [⚠️](https://github.com/gnapse/jest-dom/commits?author=lourenci "Tests") | [<img src="https://avatars1.githubusercontent.com/u/626420?v=4" width="100px;"/><br /><sub><b>Shukhrat Mukimov</b></sub>](https://github.com/mufasa71)<br />[🐛](https://github.com/gnapse/jest-dom/issues?q=author%3Amufasa71 "Bug reports") |
740753
741754
<!-- ALL-CONTRIBUTORS-LIST:END -->
742755

src/__tests__/to-be-disabled.js

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,131 @@ test('.toBeDisabled fieldset>legend', () => {
103103

104104
expect(queryByTestId('outer-fieldset-element')).toBeDisabled()
105105
})
106+
107+
test('.toBeEnabled', () => {
108+
const {queryByTestId} = render(`
109+
<div>
110+
<button disabled={true} data-testid="button-element">x</button>
111+
<textarea disabled={true} data-testid="textarea-element"></textarea>
112+
<input type="checkbox" disabled={true} data-testid="input-element" />
113+
114+
<fieldset disabled={true} data-testid="fieldset-element">
115+
<button data-testid="fieldset-child-element">x</button>
116+
</fieldset>
117+
118+
<div disabled={true} data-testid="div-element">
119+
<button data-testid="div-child-element">x</button>
120+
</div>
121+
122+
<fieldset disabled={true}>
123+
<div>
124+
<button data-testid="nested-form-element">x</button>
125+
126+
<select data-testid="deep-select-element">
127+
<optgroup data-testid="deep-optgroup-element">
128+
<option data-testid="deep-option-element">x</option>
129+
</optgroup>
130+
</select>
131+
</div>
132+
</fieldset>
133+
134+
<a href="http://github.com" disabled={true} data-testid="a-element">x</a>
135+
</div>
136+
`)
137+
138+
expect(() => {
139+
expect(queryByTestId('button-element')).toBeEnabled()
140+
}).toThrowError()
141+
expect(queryByTestId('button-element')).not.toBeEnabled()
142+
expect(() => {
143+
expect(queryByTestId('textarea-element')).toBeEnabled()
144+
}).toThrowError()
145+
expect(() => {
146+
expect(queryByTestId('input-element')).toBeEnabled()
147+
}).toThrowError()
148+
149+
expect(() => {
150+
expect(queryByTestId('fieldset-element')).toBeEnabled()
151+
}).toThrowError()
152+
expect(() => {
153+
expect(queryByTestId('fieldset-child-element')).toBeEnabled()
154+
}).toThrowError()
155+
156+
expect(queryByTestId('div-element')).toBeEnabled()
157+
expect(queryByTestId('div-child-element')).toBeEnabled()
158+
159+
expect(() => {
160+
expect(queryByTestId('nested-form-element')).toBeEnabled()
161+
}).toThrowError()
162+
expect(() => {
163+
expect(queryByTestId('deep-select-element')).toBeEnabled()
164+
}).toThrowError()
165+
expect(() => {
166+
expect(queryByTestId('deep-optgroup-element')).toBeEnabled()
167+
}).toThrowError()
168+
expect(() => {
169+
expect(queryByTestId('deep-option-element')).toBeEnabled()
170+
}).toThrowError()
171+
172+
expect(queryByTestId('a-element')).toBeEnabled()
173+
expect(() =>
174+
expect(queryByTestId('a-element')).not.toBeEnabled(),
175+
).toThrowError()
176+
})
177+
178+
test('.toBeEnabled fieldset>legend', () => {
179+
const {queryByTestId} = render(`
180+
<div>
181+
<fieldset disabled={true}>
182+
<button data-testid="inherited-element">x</button>
183+
</fieldset>
184+
185+
<fieldset disabled={true}>
186+
<legend>
187+
<button data-testid="inside-legend-element">x</button>
188+
</legend>
189+
</fieldset>
190+
191+
<fieldset disabled={true}>
192+
<legend>
193+
<div>
194+
<button data-testid="nested-inside-legend-element">x</button>
195+
</div>
196+
</legend>
197+
</fieldset>
198+
199+
<fieldset disabled={true}>
200+
<div></div>
201+
<legend>
202+
<button data-testid="first-legend-element">x</button>
203+
</legend>
204+
<legend>
205+
<button data-testid="second-legend-element">x</button>
206+
</legend>
207+
</fieldset>
208+
209+
<fieldset disabled={true}>
210+
<fieldset>
211+
<legend>
212+
<button data-testid="outer-fieldset-element">x</button>
213+
</legend>
214+
</fieldset>
215+
</fieldset>
216+
</div>
217+
`)
218+
219+
expect(() => {
220+
expect(queryByTestId('inherited-element')).toBeEnabled()
221+
}).toThrowError()
222+
expect(queryByTestId('inside-legend-element')).toBeEnabled()
223+
expect(queryByTestId('nested-inside-legend-element')).toBeEnabled()
224+
225+
expect(queryByTestId('first-legend-element')).toBeEnabled()
226+
expect(() => {
227+
expect(queryByTestId('second-legend-element')).toBeEnabled()
228+
}).toThrowError()
229+
230+
expect(() => {
231+
expect(queryByTestId('outer-fieldset-element')).toBeEnabled()
232+
}).toThrowError()
233+
})

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {toHaveStyle} from './to-have-style'
1010
import {toHaveFocus} from './to-have-focus'
1111
import {toHaveFormValues} from './to-have-form-values'
1212
import {toBeVisible} from './to-be-visible'
13-
import {toBeDisabled} from './to-be-disabled'
13+
import {toBeDisabled, toBeEnabled} from './to-be-disabled'
1414

1515
export {
1616
toBeInTheDOM,
@@ -26,4 +26,5 @@ export {
2626
toHaveFormValues,
2727
toBeVisible,
2828
toBeDisabled,
29+
toBeEnabled,
2930
}

src/to-be-disabled.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function getTag(element) {
2020
* According to specification:
2121
* If <fieldset> is disabled, the form controls that are its descendants,
2222
* except descendants of its first optional <legend> element, are disabled
23-
*
23+
*
2424
* https://html.spec.whatwg.org/multipage/form-elements.html#concept-fieldset-disabled
2525
*
2626
* This method tests whether element is first legend child of fieldset parent
@@ -71,3 +71,22 @@ export function toBeDisabled(element) {
7171
},
7272
}
7373
}
74+
75+
export function toBeEnabled(element) {
76+
checkHtmlElement(element, toBeEnabled, this)
77+
78+
const isEnabled = !(isElementDisabled(element) || isAncestorDisabled(element))
79+
80+
return {
81+
pass: isEnabled,
82+
message: () => {
83+
const is = isEnabled ? 'is' : 'is not'
84+
return [
85+
matcherHint(`${this.isNot ? '.not' : ''}.toBeEnabled`, 'element', ''),
86+
'',
87+
`Received element ${is} enabled:`,
88+
` ${printReceived(element.cloneNode(false))}`,
89+
].join('\n')
90+
},
91+
}
92+
}

0 commit comments

Comments
 (0)