Skip to content

Commit a7418b2

Browse files
committed
Add tests
1 parent 1aa7815 commit a7418b2

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

lib/utils/get-role.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const {getProp, getPropValue, getLiteralPropValue} = require('jsx-ast-utils')
1+
const {getProp, getLiteralPropValue} = require('jsx-ast-utils')
22
const {elementRoles} = require('aria-query')
33
const {getElementType} = require('./get-element-type')
44
const ObjectMap = require('./object-map')
@@ -80,8 +80,8 @@ function getRole(context, node) {
8080
continue
8181
}
8282

83-
const value = getPropValue(propOnNode)
84-
if (value || (value === '' && prop === 'alt')) {
83+
const value = getLiteralPropValue(propOnNode)
84+
if (propOnNode) {
8585
if (
8686
prop === 'href' ||
8787
prop === 'aria-labelledby' ||
@@ -90,7 +90,7 @@ function getRole(context, node) {
9090
(prop === 'alt' && value !== '')
9191
) {
9292
key.attributes.push({name: prop, constraints: ['set']})
93-
} else {
93+
} else if (value || (value === '' && prop === 'alt')) {
9494
key.attributes.push({name: prop, value})
9595
}
9696
}

tests/utils/get-role.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,27 @@ describe('getRole', function () {
4646
expect(getRole({}, node)).to.equal('link')
4747
})
4848

49-
it('returns link role for <Foo> with polymorphic prop set to "a" and href', function () {
49+
it('returns link role for <Foo> with polymorphic prop set to "a" and conditional href', function () {
5050
const node = mockJSXOpeningElement('Foo', [
5151
mockJSXAttribute('as', 'a'),
52-
[mockJSXConditionalAttribute('href', 'getUrl', '#', 'https://github.com/')],
52+
mockJSXConditionalAttribute('href', 'getUrl', '#', 'https://github.com/'),
5353
])
5454
expect(getRole({}, node)).to.equal('link')
5555
})
5656

57+
it('returns link role for <Foo> with polymorphic prop set to "a" and literal href', function () {
58+
const node = mockJSXOpeningElement('Foo', [
59+
mockJSXAttribute('as', 'a'),
60+
mockJSXAttribute('href', 'https://github.com/'),
61+
])
62+
expect(getRole({}, node)).to.equal('link')
63+
})
64+
65+
it('returns generic role for <Foo> with polymorphic prop set to "a" and no href', function () {
66+
const node = mockJSXOpeningElement('Foo', [mockJSXAttribute('as', 'a')])
67+
expect(getRole({}, node)).to.equal('generic')
68+
})
69+
5770
it('returns region role for <section> with aria-label', function () {
5871
const node = mockJSXOpeningElement('section', [mockJSXAttribute('aria-label', 'something')])
5972
expect(getRole({}, node)).to.equal('region')

0 commit comments

Comments
 (0)