Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/__tests__/to-be-disabled.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ test('.toBeDisabled', () => {
</optgroup>
</select>
</div>
<a href="http://github.com" data-testid="deep-a-element">x</a>
</fieldset>

<a href="http://github.com" disabled={true} data-testid="a-element">x</a>
Expand All @@ -50,7 +51,11 @@ test('.toBeDisabled', () => {
expect(queryByTestId('deep-option-element')).toBeDisabled()

expect(queryByTestId('a-element')).not.toBeDisabled()
expect(queryByTestId('deep-a-element')).not.toBeDisabled()
expect(() => expect(queryByTestId('a-element')).toBeDisabled()).toThrowError()
expect(() =>
expect(queryByTestId('deep-a-element')).toBeDisabled(),
).toThrowError()
})

test('.toBeDisabled fieldset>legend', () => {
Expand Down Expand Up @@ -129,6 +134,7 @@ test('.toBeEnabled', () => {
</optgroup>
</select>
</div>
<a href="http://github.com" data-testid="deep-a-element">x</a>
</fieldset>

<a href="http://github.com" disabled={true} data-testid="a-element">x</a>
Expand Down Expand Up @@ -173,6 +179,10 @@ test('.toBeEnabled', () => {
expect(() =>
expect(queryByTestId('a-element')).not.toBeEnabled(),
).toThrowError()
expect(queryByTestId('deep-a-element')).toBeEnabled()
expect(() =>
expect(queryByTestId('deep-a-element')).not.toBeEnabled(),
).toThrowError()
})

test('.toBeEnabled fieldset>legend', () => {
Expand Down
17 changes: 14 additions & 3 deletions src/to-be-disabled.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ function isElementDisabledByParent(element, parent) {
)
}

function canElementBeDisabled(element) {
return FORM_TAGS.includes(getTag(element))
}

function isElementDisabled(element) {
return FORM_TAGS.includes(getTag(element)) && element.hasAttribute('disabled')
return canElementBeDisabled(element) && element.hasAttribute('disabled')
}

function isAncestorDisabled(element) {
Expand All @@ -49,10 +53,17 @@ function isAncestorDisabled(element) {
)
}

function isElementOrAncestorDisabled(element) {
return (
canElementBeDisabled(element) &&
(isElementDisabled(element) || isAncestorDisabled(element))
)
}

export function toBeDisabled(element) {
checkHtmlElement(element, toBeDisabled, this)

const isDisabled = isElementDisabled(element) || isAncestorDisabled(element)
const isDisabled = isElementOrAncestorDisabled(element)

return {
pass: isDisabled,
Expand All @@ -71,7 +82,7 @@ export function toBeDisabled(element) {
export function toBeEnabled(element) {
checkHtmlElement(element, toBeEnabled, this)

const isEnabled = !(isElementDisabled(element) || isAncestorDisabled(element))
const isEnabled = !isElementOrAncestorDisabled(element)

return {
pass: isEnabled,
Expand Down