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

Skip to content

Commit ba52aca

Browse files
committed
Add tests for getRole
1 parent 5c98a44 commit ba52aca

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

tests/utils/get-role.js

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
const {getRole} = require('../../lib/utils/get-role')
2+
const {mockJSXAttribute, mockJSXOpeningElement} = require('./helpers')
3+
const mocha = require('mocha')
4+
const describe = mocha.describe
5+
const it = mocha.it
6+
const expect = require('chai').expect
7+
8+
describe('getRole', function () {
9+
it('returns generic role for <span> regardless of attribute', function () {
10+
const node = mockJSXOpeningElement('span', [mockJSXAttribute('aria-label', 'something')])
11+
expect(getRole({}, node)).to.equal('generic')
12+
})
13+
14+
it('returns generic role for <div> regardless of attribute', function () {
15+
const node = mockJSXOpeningElement('div', [mockJSXAttribute('aria-describedby', 'something')])
16+
expect(getRole({}, node)).to.equal('generic')
17+
})
18+
19+
it('returns generic role for <a> without href', function () {
20+
const node = mockJSXOpeningElement('a')
21+
expect(getRole({}, node)).to.equal('generic')
22+
})
23+
24+
it('returns link role for <a> with href', function () {
25+
const node = mockJSXOpeningElement('a', [mockJSXAttribute('href', '#')])
26+
expect(getRole({}, node)).to.equal('link')
27+
})
28+
29+
it('returns region role for <section> with aria-label', function () {
30+
const node = mockJSXOpeningElement('section', [mockJSXAttribute('aria-label', 'something')])
31+
expect(getRole({}, node)).to.equal('region')
32+
})
33+
34+
it('returns region role for <section> with aria-labelledby', function () {
35+
const node = mockJSXOpeningElement('section', [mockJSXAttribute('aria-labelledby', 'something')])
36+
expect(getRole({}, node)).to.equal('region')
37+
})
38+
39+
it('returns complementary role for <aside> with aria-label', function () {
40+
const node = mockJSXOpeningElement('aside', [mockJSXAttribute('aria-label', 'something')])
41+
expect(getRole({}, node)).to.equal('complementary')
42+
})
43+
44+
it('returns complementary role for <aside> with aria-labelledby', function () {
45+
const node = mockJSXOpeningElement('aside', [mockJSXAttribute('aria-labelledby', 'something')])
46+
expect(getRole({}, node)).to.equal('complementary')
47+
})
48+
49+
it('returns img role for <img> with alt set explicitly', function () {
50+
const node = mockJSXOpeningElement('img', [mockJSXAttribute('alt', 'Cute cat')])
51+
expect(getRole({}, node)).to.equal('img')
52+
})
53+
54+
it('returns img role for <img> with no alt', function () {
55+
const node = mockJSXOpeningElement('img')
56+
expect(getRole({}, node)).to.equal('img')
57+
})
58+
59+
it('returns presentation role for <img> with alt set to empty', function () {
60+
const node = mockJSXOpeningElement('img', [mockJSXAttribute('alt', '')])
61+
expect(getRole({}, node)).to.equal('presentation')
62+
})
63+
64+
it('returns form role for <form> with aria-label', function () {
65+
const node = mockJSXOpeningElement('form', [mockJSXAttribute('aria-label', 'registration')])
66+
expect(getRole({}, node)).to.equal('form')
67+
})
68+
69+
it('returns form role for <form> with name attrribute', function () {
70+
const node = mockJSXOpeningElement('form', [mockJSXAttribute('name', 'registration')])
71+
expect(getRole({}, node)).to.equal('form')
72+
})
73+
74+
it('returns undefined role for <form> with no attributes', function () {
75+
const node = mockJSXOpeningElement('form')
76+
expect(getRole({}, node)).to.equal(undefined)
77+
})
78+
79+
it('returns explicitly set role', function () {
80+
const spanButton = mockJSXOpeningElement('span', [mockJSXAttribute('role', 'button')])
81+
expect(getRole({}, spanButton)).to.equal('button')
82+
83+
const divNav = mockJSXOpeningElement('div', [mockJSXAttribute('role', 'navigation')])
84+
expect(getRole({}, divNav)).to.equal('navigation')
85+
86+
const listMenu = mockJSXOpeningElement('ul', [mockJSXAttribute('role', 'menu')])
87+
expect(getRole({}, listMenu)).to.equal('menu')
88+
})
89+
90+
it('returns heading role for heading tags', function () {
91+
const h1 = mockJSXOpeningElement('h1')
92+
expect(getRole({}, h1)).to.equal('heading')
93+
94+
const h2 = mockJSXOpeningElement('h2')
95+
expect(getRole({}, h2)).to.equal('heading')
96+
97+
const h3 = mockJSXOpeningElement('h3')
98+
expect(getRole({}, h3)).to.equal('heading')
99+
100+
const h4 = mockJSXOpeningElement('h4')
101+
expect(getRole({}, h4)).to.equal('heading')
102+
103+
const h5 = mockJSXOpeningElement('h5')
104+
expect(getRole({}, h5)).to.equal('heading')
105+
106+
const h6 = mockJSXOpeningElement('h6')
107+
expect(getRole({}, h6)).to.equal('heading')
108+
})
109+
110+
// <link> does not map to anything.
111+
it('returns undefined role for <link>', function () {
112+
const node = mockJSXOpeningElement('link')
113+
expect(getRole({}, node)).to.equal(undefined)
114+
})
115+
116+
it('returns undefined role for <link href="#">', function () {
117+
const node = mockJSXOpeningElement('link', [mockJSXAttribute('href', '#')])
118+
expect(getRole({}, node)).to.equal(undefined)
119+
})
120+
})

0 commit comments

Comments
 (0)