Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f54eae3

Browse files
committed
Add failing test cases and non-literal mocks
1 parent 5253542 commit f54eae3

File tree

4 files changed

+65
-3
lines changed

4 files changed

+65
-3
lines changed

tests/a11y-role-supports-aria-props.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ ruleTester.run('a11y-role-supports-aria-props', rule, {
3636
{code: '<div role="presentation" {...props} />'},
3737
{code: '<Foo.Bar baz={true} />'},
3838
{code: '<Link href="#" aria-checked />'},
39+
{code: '<Box aria-labelledby="some-id" role={role} />'},
40+
{code: '<Box aria-labelledby="some-id"as={isNavigationOpen ? "div" : "nav"} />'},
3941

4042
// IMPLICIT ROLE TESTS
4143
// A TESTS - implicit role is `link`

tests/utils/get-element-type.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const {getElementType} = require('../../lib/utils/get-element-type')
2-
const {mockJSXAttribute, mockJSXOpeningElement} = require('./mocks')
2+
const {mockJSXAttribute, mockJSXConditionalAttribute, mockJSXOpeningElement} = require('./mocks')
33

44
const mocha = require('mocha')
55
const describe = mocha.describe
@@ -55,4 +55,12 @@ describe('getElementType', function () {
5555
const node = mockJSXOpeningElement('Link', [mockJSXAttribute('as', 'Button')])
5656
expect(getElementType(setting, node)).to.equal('button')
5757
})
58+
59+
it('returns raw type when polymorphic prop is set to non-literal expression', function () {
60+
// <Box as={isNavigationOpen ? 'generic' : 'navigation'} />
61+
const node = mockJSXOpeningElement('Box', [
62+
mockJSXConditionalAttribute('as', 'isNavigationOpen', 'generic', 'navigation'),
63+
])
64+
expect(getElementType({}, node)).to.equal('Box')
65+
})
5866
})

tests/utils/get-role.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,31 @@
11
const {getRole} = require('../../lib/utils/get-role')
2-
const {mockJSXAttribute, mockJSXOpeningElement} = require('./mocks')
2+
const {mockJSXAttribute, mockJSXConditionalAttribute, mockJSXOpeningElement} = require('./mocks')
33
const mocha = require('mocha')
44
const describe = mocha.describe
55
const it = mocha.it
66
const expect = require('chai').expect
77

88
describe('getRole', function () {
9+
it('returns undefined when polymorphic prop is set with a non-literal expression', function () {
10+
// <Box as={isNavigationOpen ? 'div' : 'nav'} />
11+
const node = mockJSXOpeningElement('Box', [mockJSXConditionalAttribute('as', 'isNavigationOpen', 'div', 'nav')])
12+
expect(getRole({}, node)).to.equal(undefined)
13+
})
14+
15+
it('returns undefined when role is set to non-literal expression', function () {
16+
// <Box role={isNavigationOpen ? 'generic' : 'navigation'} />
17+
const node = mockJSXOpeningElement('Box', [
18+
mockJSXConditionalAttribute('role', 'isNavigationOpen', 'generic', 'navigation'),
19+
])
20+
expect(getRole({}, node)).to.equal(undefined)
21+
})
22+
23+
it('returns `role` when set to a literal expression', function () {
24+
// <Box role="generic" />
25+
const node = mockJSXOpeningElement('Box', [mockJSXAttribute('role', 'generic')])
26+
expect(getRole({}, node)).to.equal('generic')
27+
})
28+
929
it('returns generic role for <span> regardless of attribute', function () {
1030
const node = mockJSXOpeningElement('span', [mockJSXAttribute('aria-label', 'something')])
1131
expect(getRole({}, node)).to.equal('generic')

tests/utils/mocks.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,38 @@ function mockJSXAttribute(prop, propValue) {
1212
}
1313
}
1414

15+
/* Mocks conditional expression
16+
e.g. <Box as={isNavigationOpen ? 'generic' : 'navigation'} /> can be by calling
17+
mockJSXConditionalAttribute('as', 'isNavigationOpen', 'generic', 'navigation')
18+
*/
19+
function mockJSXConditionalAttribute(prop, expression, consequentValue, alternateValue) {
20+
return {
21+
type: 'JSXAttribute',
22+
name: {
23+
type: 'JSXIdentifier',
24+
name: prop,
25+
},
26+
value: {
27+
type: 'JSXExpressionContainer',
28+
value: prop,
29+
expression: {
30+
type: 'ConditionalExpression',
31+
test: {
32+
type: expression,
33+
},
34+
consequent: {
35+
type: 'Literal',
36+
value: consequentValue,
37+
},
38+
alternate: {
39+
type: 'Literal',
40+
value: alternateValue,
41+
},
42+
},
43+
},
44+
}
45+
}
46+
1547
function mockJSXOpeningElement(tagName, attributes = []) {
1648
return {
1749
type: 'JSXOpeningElement',
@@ -23,4 +55,4 @@ function mockJSXOpeningElement(tagName, attributes = []) {
2355
}
2456
}
2557

26-
module.exports = {mockJSXAttribute, mockJSXOpeningElement}
58+
module.exports = {mockJSXAttribute, mockJSXOpeningElement, mockJSXConditionalAttribute}

0 commit comments

Comments
 (0)