-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNavItemSpec.js
208 lines (179 loc) · 5.8 KB
/
NavItemSpec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import React from 'react';
import ReactDOM from 'react-dom';
import ReactTestUtils from 'react-dom/test-utils';
import NavItem from '../src/NavItem';
describe('<NavItem>', () => {
it('Should add active class', () => {
let instance = ReactTestUtils.renderIntoDocument(
<NavItem active>Item content</NavItem>
);
assert.ok(
ReactTestUtils.findRenderedDOMComponentWithClass(instance, 'active')
);
});
it('Should add disabled class', () => {
let instance = ReactTestUtils.renderIntoDocument(
<NavItem disabled>Item content</NavItem>
);
assert.ok(
ReactTestUtils.findRenderedDOMComponentWithClass(instance, 'disabled')
);
});
it('Should add DOM properties', () => {
let instance = ReactTestUtils.renderIntoDocument(
<NavItem href="/some/unique-thing/" title="content">
Item content
</NavItem>
);
let linkElement = ReactTestUtils.findRenderedDOMComponentWithTag(
instance,
'a'
);
assert.ok(linkElement.href.indexOf('/some/unique-thing/') >= 0);
assert.equal(linkElement.title, 'content');
});
it('Should not add anchor properties to li', () => {
let instance = ReactTestUtils.renderIntoDocument(
<NavItem href="/hi" title="boom!">
Item content
</NavItem>
);
assert.ok(!ReactDOM.findDOMNode(instance).hasAttribute('href'));
assert.ok(!ReactDOM.findDOMNode(instance).hasAttribute('title'));
});
it('Should pass tabIndex to the anchor', () => {
let instance = ReactTestUtils.renderIntoDocument(
<NavItem href="/hi" tabIndex="3" title="boom!">
Item content
</NavItem>
);
let node = ReactDOM.findDOMNode(instance);
expect(node.hasAttribute('tabindex')).to.equal(false);
expect(node.firstChild.getAttribute('tabindex')).to.equal('3');
});
it('Should call `onSelect` when item is selected', done => {
function handleSelect(key) {
assert.equal(key, '2');
done();
}
let instance = ReactTestUtils.renderIntoDocument(
<NavItem eventKey="2" onSelect={handleSelect}>
<span>Item content</span>
</NavItem>
);
ReactTestUtils.Simulate.click(
ReactTestUtils.findRenderedDOMComponentWithTag(instance, 'span')
);
});
it('Should not call `onSelect` when item disabled and is selected', () => {
function handleSelect() {
throw new Error('onSelect should not be called');
}
let instance = ReactTestUtils.renderIntoDocument(
<NavItem disabled onSelect={handleSelect}>
<span>Item content</span>
</NavItem>
);
ReactTestUtils.Simulate.click(
ReactTestUtils.findRenderedDOMComponentWithTag(instance, 'span')
);
});
it('Should set target attribute on anchor', () => {
let instance = ReactTestUtils.renderIntoDocument(
<NavItem href="/some/unique-thing/" target="_blank">
Item content
</NavItem>
);
let linkElement = ReactTestUtils.findRenderedDOMComponentWithTag(
instance,
'a'
);
assert.equal(linkElement.target, '_blank');
});
it('Should call `onSelect` with event', done => {
function handleSelect(key, event) {
assert.ok(event.target.tagName === 'SPAN');
done();
}
let instance = ReactTestUtils.renderIntoDocument(
<NavItem onSelect={handleSelect} target="_blank">
<span>Item content</span>
</NavItem>
);
ReactTestUtils.Simulate.click(
ReactTestUtils.findRenderedDOMComponentWithTag(instance, 'span')
);
});
it('Should set role="button" when href=="#"', () => {
let instance = ReactTestUtils.renderIntoDocument(
<NavItem href="#" target="_blank">
Item content
</NavItem>
);
let linkElement = ReactTestUtils.findRenderedDOMComponentWithTag(
instance,
'a'
);
assert(linkElement.outerHTML.match('role="button"'), true);
});
it('Should not set role when href!="#"', () => {
let instance = ReactTestUtils.renderIntoDocument(
<NavItem href="/path/to/stuff" target="_blank">
Item content
</NavItem>
);
let linkElement = ReactTestUtils.findRenderedDOMComponentWithTag(
instance,
'a'
);
assert.equal(linkElement.outerHTML.match('role="button"'), null);
});
describe('Web Accessibility', () => {
it('Should pass aria-controls to the link', () => {
let instance = ReactTestUtils.renderIntoDocument(
<NavItem href="/path/to/stuff" target="_blank" aria-controls="hi">
Item content
</NavItem>
);
let linkElement = ReactTestUtils.findRenderedDOMComponentWithTag(
instance,
'a'
);
assert.ok(linkElement.hasAttribute('aria-controls'));
});
it('Should add aria-selected to the link when role is "tab"', () => {
let instance = ReactTestUtils.renderIntoDocument(
<NavItem role="tab" active>
Item content
</NavItem>
);
let linkElement = ReactTestUtils.findRenderedDOMComponentWithTag(
instance,
'a'
);
expect(linkElement.getAttribute('aria-selected')).to.equal('true');
});
it('Should not add aria-selected to the link when role is not "tab"', () => {
let instance = ReactTestUtils.renderIntoDocument(
<NavItem role="button" active>
Item content
</NavItem>
);
let linkElement = ReactTestUtils.findRenderedDOMComponentWithTag(
instance,
'a'
);
expect(linkElement.getAttribute('aria-selected')).to.not.exist;
});
it('Should pass role down', () => {
let instance = ReactTestUtils.renderIntoDocument(
<NavItem role="tab">Item content</NavItem>
);
let linkElement = ReactTestUtils.findRenderedDOMComponentWithTag(
instance,
'a'
);
assert.equal(linkElement.getAttribute('role'), 'tab');
});
});
});