Skip to content

Commit

Permalink
feat(toHaveValue): Asserting aria-valuenow
Browse files Browse the repository at this point in the history
  • Loading branch information
idanen committed Aug 5, 2024
1 parent 47a667c commit 90d8070
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/__tests__/to-have-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,14 @@ Received:
<red> foo</>
`)
})

test('handles value of aria-valuenow', () => {
const valueToCheck = 70
const {queryByTestId} = render(`
<div role="meter" aria-valuemin="0" aria-valuemax="100" aria-valuenow="${valueToCheck}" data-testid="meter"></div>
`)

expect(queryByTestId('meter')).toHaveValue(valueToCheck)
expect(queryByTestId('meter')).not.toHaveValue(valueToCheck + 1)
})
})
17 changes: 15 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,18 +196,31 @@ function getInputValue(inputElement) {
}
}

const rolesSupportingValues = ['meter', 'progressbar', 'slider', 'spinbutton']
function getAccessibleValue(element) {
if (!rolesSupportingValues.includes(element.getAttribute('role'))) {
return
}
// We want same behavior as accessing the `value` property
// eslint-disable-next-line consistent-return
return Number(element.getAttribute('aria-valuenow'))
}

function getSingleElementValue(element) {
/* istanbul ignore if */
if (!element) {
return undefined
}

switch (element.tagName.toLowerCase()) {
case 'input':
return getInputValue(element)
case 'select':
return getSelectValue(element)
default:
return element.value
default: {
const accessibleValue = getAccessibleValue(element)
return element.value ?? accessibleValue
}
}
}

Expand Down

0 comments on commit 90d8070

Please sign in to comment.