From 90d80701b2a228dd812ebc88a5c2b06c0d29f41e Mon Sep 17 00:00:00 2001 From: Idan Entin Date: Tue, 20 Sep 2022 20:46:07 +0300 Subject: [PATCH 1/5] feat(toHaveValue): Asserting aria-valuenow Resolves #478 --- src/__tests__/to-have-value.js | 10 ++++++++++ src/utils.js | 17 +++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/__tests__/to-have-value.js b/src/__tests__/to-have-value.js index 80cc7828..bc8e6493 100644 --- a/src/__tests__/to-have-value.js +++ b/src/__tests__/to-have-value.js @@ -203,4 +203,14 @@ Received: foo `) }) + + test('handles value of aria-valuenow', () => { + const valueToCheck = 70 + const {queryByTestId} = render(` +
+ `) + + expect(queryByTestId('meter')).toHaveValue(valueToCheck) + expect(queryByTestId('meter')).not.toHaveValue(valueToCheck + 1) + }) }) diff --git a/src/utils.js b/src/utils.js index ee8e484b..c46ef25c 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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 + } } } From 34d6978357f81f6245c6eecc56ecb8f784ac48ca Mon Sep 17 00:00:00 2001 From: Idan Entin Date: Tue, 20 Sep 2022 20:55:51 +0300 Subject: [PATCH 2/5] docs(to-have-value): Update the docs --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 11f2dd4c..5c5acc20 100644 --- a/README.md +++ b/README.md @@ -996,6 +996,9 @@ It accepts ``, `