Skip to content

Commit fc80f89

Browse files
authored
Merge pull request #1 from kschow/support-unenclosed-inner-text-for-details-elements-in-toBeVisible-main-rebased
Support unenclosed inner text for details elements in to be visible main rebased
2 parents 2958023 + a77798e commit fc80f89

11 files changed

+142
-14
lines changed

.all-contributorsrc

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,62 @@
641641
"contributions": [
642642
"doc"
643643
]
644+
},
645+
{
646+
"login": "MatanBobi",
647+
"name": "Matan Borenkraout",
648+
"avatar_url": "https://avatars.githubusercontent.com/u/12711091?v=4",
649+
"profile": "https://matan.io",
650+
"contributions": [
651+
"platform"
652+
]
653+
},
654+
{
655+
"login": "yannbf",
656+
"name": "Yann Braga",
657+
"avatar_url": "https://avatars.githubusercontent.com/u/1671563?v=4",
658+
"profile": "https://github.com/yannbf",
659+
"contributions": [
660+
"code"
661+
]
662+
},
663+
{
664+
"login": "IanVS",
665+
"name": "Ian VanSchooten",
666+
"avatar_url": "https://avatars.githubusercontent.com/u/4616705?v=4",
667+
"profile": "https://github.com/IanVS",
668+
"contributions": [
669+
"code"
670+
]
671+
},
672+
{
673+
"login": "cbroeren",
674+
"name": "Chantal Broeren",
675+
"avatar_url": "https://avatars.githubusercontent.com/u/7499806?v=4",
676+
"profile": "http://chantalbroeren.nl",
677+
"contributions": [
678+
"doc"
679+
]
680+
},
681+
{
682+
"login": "astorije",
683+
"name": "Jérémie Astori",
684+
"avatar_url": "https://avatars.githubusercontent.com/u/113730?v=4",
685+
"profile": "https://jeremie.astori.fr",
686+
"contributions": [
687+
"code",
688+
"ideas"
689+
]
690+
},
691+
{
692+
"login": "ashleyryan",
693+
"name": "Ashley Ryan",
694+
"avatar_url": "https://avatars.githubusercontent.com/u/9469374?v=4",
695+
"profile": "https://github.com/ashleyryan",
696+
"contributions": [
697+
"code",
698+
"ideas"
699+
]
644700
}
645701
],
646702
"repoHost": "https://github.com",

.npmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
registry=http://registry.npmjs.org/
1+
registry=https://registry.npmjs.org/
22
package-lock=false

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ toBeDisabled()
159159
This allows you to check whether an element is disabled from the user's
160160
perspective. According to the specification, the following elements can be
161161
[disabled](https://html.spec.whatwg.org/multipage/semantics-other.html#disabled-elements):
162-
`button`, `input`, `select`, `textarea`, `optgroup`, `option`, `fieldset`.
162+
`button`, `input`, `select`, `textarea`, `optgroup`, `option`, `fieldset`, and
163+
custom elements.
163164

164165
This custom matcher considers an element as disabled if the element is among the
165166
types of elements that can be disabled (listed above), and the `disabled`
@@ -899,7 +900,7 @@ const selectInput = getByTestId('select-number')
899900
expect(textInput).toHaveValue('text')
900901
expect(numberInput).toHaveValue(5)
901902
expect(emptyInput).not.toHaveValue()
902-
expect(selectInput).not.toHaveValue(['second', 'third'])
903+
expect(selectInput).toHaveValue(['second', 'third'])
903904
```
904905

905906
<hr />
@@ -1359,6 +1360,12 @@ Thanks goes to these people ([emoji key][emojis]):
13591360
</tr>
13601361
<tr>
13611362
<td align="center"><a href="http://tu4mo.com"><img src="https://avatars.githubusercontent.com/u/16735302?v=4?s=100" width="100px;" alt=""/><br /><sub><b>tu4mo</b></sub></a><br /><a href="https://github.com/testing-library/jest-dom/commits?author=tu4mo" title="Documentation">📖</a></td>
1363+
<td align="center"><a href="https://matan.io"><img src="https://avatars.githubusercontent.com/u/12711091?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Matan Borenkraout</b></sub></a><br /><a href="#platform-MatanBobi" title="Packaging/porting to new platform">📦</a></td>
1364+
<td align="center"><a href="https://github.com/yannbf"><img src="https://avatars.githubusercontent.com/u/1671563?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Yann Braga</b></sub></a><br /><a href="https://github.com/testing-library/jest-dom/commits?author=yannbf" title="Code">💻</a></td>
1365+
<td align="center"><a href="https://github.com/IanVS"><img src="https://avatars.githubusercontent.com/u/4616705?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Ian VanSchooten</b></sub></a><br /><a href="https://github.com/testing-library/jest-dom/commits?author=IanVS" title="Code">💻</a></td>
1366+
<td align="center"><a href="http://chantalbroeren.nl"><img src="https://avatars.githubusercontent.com/u/7499806?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Chantal Broeren</b></sub></a><br /><a href="https://github.com/testing-library/jest-dom/commits?author=cbroeren" title="Documentation">📖</a></td>
1367+
<td align="center"><a href="https://jeremie.astori.fr"><img src="https://avatars.githubusercontent.com/u/113730?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jérémie Astori</b></sub></a><br /><a href="https://github.com/testing-library/jest-dom/commits?author=astorije" title="Code">💻</a> <a href="#ideas-astorije" title="Ideas, Planning, & Feedback">🤔</a></td>
1368+
<td align="center"><a href="https://github.com/ashleyryan"><img src="https://avatars.githubusercontent.com/u/9469374?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Ashley Ryan</b></sub></a><br /><a href="https://github.com/testing-library/jest-dom/commits?author=ashleyryan" title="Code">💻</a> <a href="#ideas-ashleyryan" title="Ideas, Planning, & Feedback">🤔</a></td>
13621369
</tr>
13631370
</table>
13641371

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"dependencies": {
3434
"@babel/runtime": "^7.9.2",
3535
"@types/testing-library__jest-dom": "^5.9.1",
36-
"aria-query": "^4.2.2",
36+
"aria-query": "^5.0.0",
3737
"chalk": "^3.0.0",
3838
"css": "^3.0.0",
3939
"css.escape": "^1.5.1",

src/__tests__/to-be-disabled.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1+
import document from './helpers/document'
12
import {render} from './helpers/test-utils'
23

4+
const window = document.defaultView
5+
6+
window.customElements.define(
7+
'custom-element',
8+
class extends window.HTMLElement {},
9+
)
10+
311
test('.toBeDisabled', () => {
412
const {queryByTestId} = render(`
513
<div>
@@ -109,6 +117,23 @@ test('.toBeDisabled fieldset>legend', () => {
109117
expect(queryByTestId('outer-fieldset-element')).toBeDisabled()
110118
})
111119

120+
test('.toBeDisabled custom element', () => {
121+
const {queryByTestId} = render(`
122+
<custom-element data-testid="disabled-custom-element" disabled=""></custom-element>
123+
<custom-element data-testid="enabled-custom-element"></custom-element>
124+
`)
125+
126+
expect(queryByTestId('disabled-custom-element')).toBeDisabled()
127+
expect(() => {
128+
expect(queryByTestId('disabled-custom-element')).not.toBeDisabled()
129+
}).toThrowError('element is disabled')
130+
131+
expect(queryByTestId('enabled-custom-element')).not.toBeDisabled()
132+
expect(() => {
133+
expect(queryByTestId('enabled-custom-element')).toBeDisabled()
134+
}).toThrowError('element is not disabled')
135+
})
136+
112137
test('.toBeEnabled', () => {
113138
const {queryByTestId} = render(`
114139
<div>
@@ -241,3 +266,20 @@ test('.toBeEnabled fieldset>legend', () => {
241266
expect(queryByTestId('outer-fieldset-element')).toBeEnabled()
242267
}).toThrowError()
243268
})
269+
270+
test('.toBeEnabled custom element', () => {
271+
const {queryByTestId} = render(`
272+
<custom-element data-testid="disabled-custom-element" disabled=""></custom-element>
273+
<custom-element data-testid="enabled-custom-element"></custom-element>
274+
`)
275+
276+
expect(queryByTestId('disabled-custom-element')).not.toBeEnabled()
277+
expect(() => {
278+
expect(queryByTestId('disabled-custom-element')).toBeEnabled()
279+
}).toThrowError('element is not enabled')
280+
281+
expect(queryByTestId('enabled-custom-element')).toBeEnabled()
282+
expect(() => {
283+
expect(queryByTestId('enabled-custom-element')).not.toBeEnabled()
284+
}).toThrowError('element is enabled')
285+
})

src/to-be-checked.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function supportedRolesSentence() {
5757
}
5858

5959
function supportedRoles() {
60-
return Array.from(roles.keys()).filter(roleSupportsChecked)
60+
return roles.keys().filter(roleSupportsChecked)
6161
}
6262

6363
function roleSupportsChecked(role) {

src/to-be-disabled.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,17 @@ function isElementDisabledByParent(element, parent) {
3636
)
3737
}
3838

39+
function isCustomElement(tag) {
40+
return tag.includes('-')
41+
}
42+
43+
/*
44+
* Only certain form elements and custom elements can actually be disabled:
45+
* https://html.spec.whatwg.org/multipage/semantics-other.html#disabled-elements
46+
*/
3947
function canElementBeDisabled(element) {
40-
return FORM_TAGS.includes(getTag(element))
48+
const tag = getTag(element)
49+
return FORM_TAGS.includes(tag) || isCustomElement(tag)
4150
}
4251

4352
function isElementDisabled(element) {

src/to-have-class.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ export function toHaveClass(htmlElement, ...params) {
4242
const to = this.isNot ? 'not to' : 'to'
4343
return getMessage(
4444
this,
45+
this.utils.matcherHint(
46+
`${this.isNot ? '.not' : ''}.toHaveClass`,
47+
'element',
48+
this.utils.printExpected(expected.join(' ')),
49+
),
4550
`Expected the element ${to} have EXACTLY defined classes`,
4651
expected.join(' '),
4752
'Received',

src/to-have-description.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import {checkHtmlElement, getMessage, normalize, deprecate} from './utils'
33
// See algoritm: https://www.w3.org/TR/accname-1.1/#mapping_additional_nd_description
44
export function toHaveDescription(htmlElement, checkWith) {
55
deprecate(
6-
'toBeInTheDOM',
7-
'Please use toBeInTheDocument for searching the entire document and toContainElement for searching a specific container.',
6+
'toHaveDescription',
7+
'Please use toHaveAccessibleDescription.',
88
)
99

1010
checkHtmlElement(htmlElement, toHaveDescription, this)

src/to-have-focus.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,19 @@ export function toHaveFocus(element) {
1313
'',
1414
),
1515
'',
16-
'Expected element with focus:',
17-
` ${this.utils.printExpected(element)}`,
18-
'Received element with focus:',
19-
` ${this.utils.printReceived(element.ownerDocument.activeElement)}`,
16+
...(this.isNot
17+
? [
18+
'Received element is focused:',
19+
` ${this.utils.printReceived(element)}`,
20+
]
21+
: [
22+
'Expected element with focus:',
23+
` ${this.utils.printExpected(element)}`,
24+
'Received element with focus:',
25+
` ${this.utils.printReceived(
26+
element.ownerDocument.activeElement,
27+
)}`,
28+
]),
2029
].join('\n')
2130
},
2231
}

src/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import redent from 'redent'
2-
import {parse} from 'css'
2+
import cssParse from 'css/lib/parse'
33
import isEqual from 'lodash/isEqual'
44

55
class GenericTypeError extends Error {
@@ -100,7 +100,7 @@ class InvalidCSSError extends Error {
100100
}
101101

102102
function parseCSS(css, ...args) {
103-
const ast = parse(`selector { ${css} }`, {silent: true}).stylesheet
103+
const ast = cssParse(`selector { ${css} }`, {silent: true}).stylesheet
104104

105105
if (ast.parsingErrors && ast.parsingErrors.length > 0) {
106106
const {reason, line} = ast.parsingErrors[0]

0 commit comments

Comments
 (0)