From a7418b2b26da2856c14d67e48c1cf4d378d69070 Mon Sep 17 00:00:00 2001 From: Kate Higa <16447748+khiga8@users.noreply.github.com> Date: Mon, 17 Jul 2023 14:17:01 -0400 Subject: [PATCH] Add tests --- lib/utils/get-role.js | 8 ++++---- tests/utils/get-role.js | 17 +++++++++++++++-- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/lib/utils/get-role.js b/lib/utils/get-role.js index 98e61f7a..8fea9178 100644 --- a/lib/utils/get-role.js +++ b/lib/utils/get-role.js @@ -1,4 +1,4 @@ -const {getProp, getPropValue, getLiteralPropValue} = require('jsx-ast-utils') +const {getProp, getLiteralPropValue} = require('jsx-ast-utils') const {elementRoles} = require('aria-query') const {getElementType} = require('./get-element-type') const ObjectMap = require('./object-map') @@ -80,8 +80,8 @@ function getRole(context, node) { continue } - const value = getPropValue(propOnNode) - if (value || (value === '' && prop === 'alt')) { + const value = getLiteralPropValue(propOnNode) + if (propOnNode) { if ( prop === 'href' || prop === 'aria-labelledby' || @@ -90,7 +90,7 @@ function getRole(context, node) { (prop === 'alt' && value !== '') ) { key.attributes.push({name: prop, constraints: ['set']}) - } else { + } else if (value || (value === '' && prop === 'alt')) { key.attributes.push({name: prop, value}) } } diff --git a/tests/utils/get-role.js b/tests/utils/get-role.js index 195e771b..c8d2aa5b 100644 --- a/tests/utils/get-role.js +++ b/tests/utils/get-role.js @@ -46,14 +46,27 @@ describe('getRole', function () { expect(getRole({}, node)).to.equal('link') }) - it('returns link role for with polymorphic prop set to "a" and href', function () { + it('returns link role for with polymorphic prop set to "a" and conditional href', function () { const node = mockJSXOpeningElement('Foo', [ mockJSXAttribute('as', 'a'), - [mockJSXConditionalAttribute('href', 'getUrl', '#', 'https://github.com/')], + mockJSXConditionalAttribute('href', 'getUrl', '#', 'https://github.com/'), ]) expect(getRole({}, node)).to.equal('link') }) + it('returns link role for with polymorphic prop set to "a" and literal href', function () { + const node = mockJSXOpeningElement('Foo', [ + mockJSXAttribute('as', 'a'), + mockJSXAttribute('href', 'https://github.com/'), + ]) + expect(getRole({}, node)).to.equal('link') + }) + + it('returns generic role for with polymorphic prop set to "a" and no href', function () { + const node = mockJSXOpeningElement('Foo', [mockJSXAttribute('as', 'a')]) + expect(getRole({}, node)).to.equal('generic') + }) + it('returns region role for
with aria-label', function () { const node = mockJSXOpeningElement('section', [mockJSXAttribute('aria-label', 'something')]) expect(getRole({}, node)).to.equal('region')