Skip to content

Commit

Permalink
Merge pull request #1 from kschow/support-unenclosed-inner-text-for-d…
Browse files Browse the repository at this point in the history
…etails-elements-in-toBeVisible-main-rebased

Support unenclosed inner text for details elements in to be visible main rebased
  • Loading branch information
feedmypixel authored Mar 30, 2022
2 parents 2958023 + a77798e commit fc80f89
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 14 deletions.
56 changes: 56 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,62 @@
"contributions": [
"doc"
]
},
{
"login": "MatanBobi",
"name": "Matan Borenkraout",
"avatar_url": "https://avatars.githubusercontent.com/u/12711091?v=4",
"profile": "https://matan.io",
"contributions": [
"platform"
]
},
{
"login": "yannbf",
"name": "Yann Braga",
"avatar_url": "https://avatars.githubusercontent.com/u/1671563?v=4",
"profile": "https:/yannbf",
"contributions": [
"code"
]
},
{
"login": "IanVS",
"name": "Ian VanSchooten",
"avatar_url": "https://avatars.githubusercontent.com/u/4616705?v=4",
"profile": "https:/IanVS",
"contributions": [
"code"
]
},
{
"login": "cbroeren",
"name": "Chantal Broeren",
"avatar_url": "https://avatars.githubusercontent.com/u/7499806?v=4",
"profile": "http://chantalbroeren.nl",
"contributions": [
"doc"
]
},
{
"login": "astorije",
"name": "Jérémie Astori",
"avatar_url": "https://avatars.githubusercontent.com/u/113730?v=4",
"profile": "https://jeremie.astori.fr",
"contributions": [
"code",
"ideas"
]
},
{
"login": "ashleyryan",
"name": "Ashley Ryan",
"avatar_url": "https://avatars.githubusercontent.com/u/9469374?v=4",
"profile": "https:/ashleyryan",
"contributions": [
"code",
"ideas"
]
}
],
"repoHost": "https:",
Expand Down
2 changes: 1 addition & 1 deletion .npmrc
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
registry=http://registry.npmjs.org/
registry=https://registry.npmjs.org/
package-lock=false
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ toBeDisabled()
This allows you to check whether an element is disabled from the user's
perspective. According to the specification, the following elements can be
[disabled](https://html.spec.whatwg.org/multipage/semantics-other.html#disabled-elements):
`button`, `input`, `select`, `textarea`, `optgroup`, `option`, `fieldset`.
`button`, `input`, `select`, `textarea`, `optgroup`, `option`, `fieldset`, and
custom elements.

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

<hr />
Expand Down Expand Up @@ -1359,6 +1360,12 @@ Thanks goes to these people ([emoji key][emojis]):
</tr>
<tr>
<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:/testing-library/jest-dom/commits?author=tu4mo" title="Documentation">📖</a></td>
<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>
<td align="center"><a href="https:/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:/testing-library/jest-dom/commits?author=yannbf" title="Code">💻</a></td>
<td align="center"><a href="https:/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:/testing-library/jest-dom/commits?author=IanVS" title="Code">💻</a></td>
<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:/testing-library/jest-dom/commits?author=cbroeren" title="Documentation">📖</a></td>
<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:/testing-library/jest-dom/commits?author=astorije" title="Code">💻</a> <a href="#ideas-astorije" title="Ideas, Planning, & Feedback">🤔</a></td>
<td align="center"><a href="https:/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:/testing-library/jest-dom/commits?author=ashleyryan" title="Code">💻</a> <a href="#ideas-ashleyryan" title="Ideas, Planning, & Feedback">🤔</a></td>
</tr>
</table>

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"dependencies": {
"@babel/runtime": "^7.9.2",
"@types/testing-library__jest-dom": "^5.9.1",
"aria-query": "^4.2.2",
"aria-query": "^5.0.0",
"chalk": "^3.0.0",
"css": "^3.0.0",
"css.escape": "^1.5.1",
Expand Down
42 changes: 42 additions & 0 deletions src/__tests__/to-be-disabled.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import document from './helpers/document'
import {render} from './helpers/test-utils'

const window = document.defaultView

window.customElements.define(
'custom-element',
class extends window.HTMLElement {},
)

test('.toBeDisabled', () => {
const {queryByTestId} = render(`
<div>
Expand Down Expand Up @@ -109,6 +117,23 @@ test('.toBeDisabled fieldset>legend', () => {
expect(queryByTestId('outer-fieldset-element')).toBeDisabled()
})

test('.toBeDisabled custom element', () => {
const {queryByTestId} = render(`
<custom-element data-testid="disabled-custom-element" disabled=""></custom-element>
<custom-element data-testid="enabled-custom-element"></custom-element>
`)

expect(queryByTestId('disabled-custom-element')).toBeDisabled()
expect(() => {
expect(queryByTestId('disabled-custom-element')).not.toBeDisabled()
}).toThrowError('element is disabled')

expect(queryByTestId('enabled-custom-element')).not.toBeDisabled()
expect(() => {
expect(queryByTestId('enabled-custom-element')).toBeDisabled()
}).toThrowError('element is not disabled')
})

test('.toBeEnabled', () => {
const {queryByTestId} = render(`
<div>
Expand Down Expand Up @@ -241,3 +266,20 @@ test('.toBeEnabled fieldset>legend', () => {
expect(queryByTestId('outer-fieldset-element')).toBeEnabled()
}).toThrowError()
})

test('.toBeEnabled custom element', () => {
const {queryByTestId} = render(`
<custom-element data-testid="disabled-custom-element" disabled=""></custom-element>
<custom-element data-testid="enabled-custom-element"></custom-element>
`)

expect(queryByTestId('disabled-custom-element')).not.toBeEnabled()
expect(() => {
expect(queryByTestId('disabled-custom-element')).toBeEnabled()
}).toThrowError('element is not enabled')

expect(queryByTestId('enabled-custom-element')).toBeEnabled()
expect(() => {
expect(queryByTestId('enabled-custom-element')).not.toBeEnabled()
}).toThrowError('element is enabled')
})
2 changes: 1 addition & 1 deletion src/to-be-checked.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function supportedRolesSentence() {
}

function supportedRoles() {
return Array.from(roles.keys()).filter(roleSupportsChecked)
return roles.keys().filter(roleSupportsChecked)
}

function roleSupportsChecked(role) {
Expand Down
11 changes: 10 additions & 1 deletion src/to-be-disabled.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,17 @@ function isElementDisabledByParent(element, parent) {
)
}

function isCustomElement(tag) {
return tag.includes('-')
}

/*
* Only certain form elements and custom elements can actually be disabled:
* https://html.spec.whatwg.org/multipage/semantics-other.html#disabled-elements
*/
function canElementBeDisabled(element) {
return FORM_TAGS.includes(getTag(element))
const tag = getTag(element)
return FORM_TAGS.includes(tag) || isCustomElement(tag)
}

function isElementDisabled(element) {
Expand Down
5 changes: 5 additions & 0 deletions src/to-have-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ export function toHaveClass(htmlElement, ...params) {
const to = this.isNot ? 'not to' : 'to'
return getMessage(
this,
this.utils.matcherHint(
`${this.isNot ? '.not' : ''}.toHaveClass`,
'element',
this.utils.printExpected(expected.join(' ')),
),
`Expected the element ${to} have EXACTLY defined classes`,
expected.join(' '),
'Received',
Expand Down
4 changes: 2 additions & 2 deletions src/to-have-description.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {checkHtmlElement, getMessage, normalize, deprecate} from './utils'
// See algoritm: https://www.w3.org/TR/accname-1.1/#mapping_additional_nd_description
export function toHaveDescription(htmlElement, checkWith) {
deprecate(
'toBeInTheDOM',
'Please use toBeInTheDocument for searching the entire document and toContainElement for searching a specific container.',
'toHaveDescription',
'Please use toHaveAccessibleDescription.',
)

checkHtmlElement(htmlElement, toHaveDescription, this)
Expand Down
17 changes: 13 additions & 4 deletions src/to-have-focus.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,19 @@ export function toHaveFocus(element) {
'',
),
'',
'Expected element with focus:',
` ${this.utils.printExpected(element)}`,
'Received element with focus:',
` ${this.utils.printReceived(element.ownerDocument.activeElement)}`,
...(this.isNot
? [
'Received element is focused:',
` ${this.utils.printReceived(element)}`,
]
: [
'Expected element with focus:',
` ${this.utils.printExpected(element)}`,
'Received element with focus:',
` ${this.utils.printReceived(
element.ownerDocument.activeElement,
)}`,
]),
].join('\n')
},
}
Expand Down
4 changes: 2 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import redent from 'redent'
import {parse} from 'css'
import cssParse from 'css/lib/parse'
import isEqual from 'lodash/isEqual'

class GenericTypeError extends Error {
Expand Down Expand Up @@ -100,7 +100,7 @@ class InvalidCSSError extends Error {
}

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

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

0 comments on commit fc80f89

Please sign in to comment.