forked from react-bootstrap/react-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPanelSpec.js
221 lines (187 loc) · 8.37 KB
/
PanelSpec.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
209
210
211
212
213
214
215
216
217
218
219
220
221
import React from 'react';
import ReactTestUtils from 'react/lib/ReactTestUtils';
import ReactDOM from 'react-dom';
import Panel from '../src/Panel';
import Table from '../src/Table';
describe('Panel', () => {
it('Should have class and body', () => {
let instance = ReactTestUtils.renderIntoDocument(
<Panel>Panel content</Panel>
);
assert.ok(ReactDOM.findDOMNode(instance).className.match(/\bpanel\b/));
assert.ok(ReactTestUtils.findRenderedDOMComponentWithClass(instance, 'panel-body'));
});
it('Should have bootstrap style class', () => {
let instance = ReactTestUtils.renderIntoDocument(
<Panel bsStyle="default">Panel content</Panel>
);
assert.ok(ReactDOM.findDOMNode(instance).className.match(/\bpanel-default\b/));
});
it('Should honour additional classes passed in, adding not overriding', () => {
let instance = ReactTestUtils.renderIntoDocument(
<Panel className="bob"/>
);
assert.ok(ReactDOM.findDOMNode(instance).className.match(/\bbob\b/));
assert.ok(ReactDOM.findDOMNode(instance).className.match(/\bpanel\b/));
});
it('Should have unwrapped header', () => {
let instance = ReactTestUtils.renderIntoDocument(
<Panel header="Heading">Panel content</Panel>
);
let header = ReactTestUtils.findRenderedDOMComponentWithClass(instance, 'panel-heading');
assert.equal(header.innerHTML, 'Heading');
});
it('Should have custom component header', () => {
let header = <h3>Heading</h3>;
let instance = ReactTestUtils.renderIntoDocument(
<Panel header={header}>Panel content</Panel>
);
header = ReactTestUtils.findRenderedDOMComponentWithClass(instance, 'panel-heading');
assert.equal(header.firstChild.nodeName, 'H3');
assert.ok(header.firstChild.className.match(/\bpanel-title\b/));
assert.equal(header.firstChild.innerHTML, 'Heading');
});
it('Should have custom component header with anchor', () => {
let header = <h3>Heading</h3>;
let instance = ReactTestUtils.renderIntoDocument(
<Panel header={header} collapsible={true}>Panel content</Panel>
);
header = ReactTestUtils.findRenderedDOMComponentWithClass(instance, 'panel-heading');
assert.equal(header.firstChild.nodeName, 'H3');
assert.ok(header.firstChild.className.match(/\bpanel-title\b/));
assert.equal(header.firstChild.firstChild.nodeName, 'A');
assert.equal(header.firstChild.firstChild.innerHTML, 'Heading');
});
it('Should have custom component header with custom class', () => {
let header = <h3 className="custom-class">Heading</h3>;
let instance = ReactTestUtils.renderIntoDocument(
<Panel header={header}>Panel content</Panel>
);
header = ReactTestUtils.findRenderedDOMComponentWithClass(instance, 'panel-heading');
assert.equal(header.firstChild.nodeName, 'H3');
assert.ok(header.firstChild.className.match(/\bpanel-title\b/));
assert.ok(header.firstChild.className.match(/\bcustom-class\b/));
assert.equal(header.firstChild.innerHTML, 'Heading');
});
it('Should have footer', () => {
let instance = ReactTestUtils.renderIntoDocument(
<Panel footer="Footer">Panel content</Panel>
);
let footer = ReactTestUtils.findRenderedDOMComponentWithClass(instance, 'panel-footer');
assert.equal(footer.innerText, 'Footer');
});
it('Should have collapse classes', () => {
let instance = ReactTestUtils.renderIntoDocument(
<Panel collapsible={true} expanded={true}>Panel content</Panel>
);
assert.ok(ReactDOM.findDOMNode(instance).querySelector('.panel-collapse.collapse.in'));
});
it('Should pass through dom properties', () => {
let instance = ReactTestUtils.renderIntoDocument(
<Panel collapsible={false} id="testid">Panel content</Panel>
);
assert.equal(ReactDOM.findDOMNode(instance).id, 'testid');
});
it('Should pass id to panel-collapse', () => {
let instance = ReactTestUtils.renderIntoDocument(
<Panel collapsible={true} id="testid" header="Heading">Panel content</Panel>
);
assert.notOk(ReactDOM.findDOMNode(instance).id);
let collapse = ReactDOM.findDOMNode(instance).querySelector('.panel-collapse');
let anchor = ReactDOM.findDOMNode(instance).querySelector('.panel-title a');
assert.equal(collapse.id, 'testid');
assert.equal(anchor.getAttribute('href'), '#testid');
});
it('Should be open', () => {
let instance = ReactTestUtils.renderIntoDocument(
<Panel collapsible={true} expanded={true} header="Heading">Panel content</Panel>
);
let collapse = ReactDOM.findDOMNode(instance).querySelector('.panel-collapse');
let anchor = ReactDOM.findDOMNode(instance).querySelector('.panel-title a');
assert.ok(collapse.className.match(/\bin\b/));
assert.notOk(anchor.className.match(/\bcollapsed\b/));
});
it('Should be closed', () => {
let instance = ReactTestUtils.renderIntoDocument(
<Panel collapsible={true} expanded={false} header="Heading">Panel content</Panel>
);
let collapse = ReactDOM.findDOMNode(instance).querySelector('.panel-collapse');
let anchor = ReactDOM.findDOMNode(instance).querySelector('.panel-title a');
assert.notOk(collapse.className.match(/\bin\b/));
assert.ok(anchor.className.match(/\bcollapsed\b/));
});
it('Should call onSelect handler', (done) => {
function handleSelect(e, key) {
assert.equal(key, '1');
done();
}
let instance = ReactTestUtils.renderIntoDocument(
<Panel collapsible={true} onSelect={handleSelect} header="Click me" eventKey='1'>Panel content</Panel>
);
let title = ReactTestUtils.findRenderedDOMComponentWithClass(instance, 'panel-title');
ReactTestUtils.Simulate.click(title.firstChild);
});
it('Should toggle when uncontrolled', () => {
let instance = ReactTestUtils.renderIntoDocument(
<Panel collapsible={true} defaultExpanded={false} header="Click me">Panel content</Panel>
);
assert.notOk(instance.state.expanded);
ReactTestUtils.Simulate.click(
ReactTestUtils.findRenderedDOMComponentWithClass(instance, 'panel-title').firstChild
);
assert.ok(instance.state.expanded);
});
it('Should not wrap panel-filling tables in a panel body', () => {
let instance = ReactTestUtils.renderIntoDocument(
<Panel>
Panel content
<Table fill />
More panel content
</Panel>
);
let children = ReactDOM.findDOMNode(instance).children;
assert.equal(children.length, 3);
assert.equal(children[0].nodeName, 'DIV');
assert.ok(children[0].className.match(/\bpanel-body\b/));
assert.equal(children[1].nodeName, 'TABLE');
assert.notOk(children[1].className.match(/\bpanel-body\b/));
assert.equal(children[2].nodeName, 'DIV');
assert.ok(children[2].className.match(/\bpanel-body\b/));
});
it('Should not wrap single panel-fill table in a panel body', () => {
let instance = ReactTestUtils.renderIntoDocument(
<Panel>
<Table fill />
</Panel>
);
let children = ReactDOM.findDOMNode(instance).children;
assert.equal(children.length, 1);
assert.equal(children[0].nodeName, 'TABLE');
assert.notOk(children[0].className.match(/\bpanel-body\b/));
});
describe('Web Accessibility', () => {
it('Should be aria-expanded=true', () => {
let instance = ReactTestUtils.renderIntoDocument(
<Panel collapsible={true} expanded={true} header="Heading">Panel content</Panel>
);
let anchor = ReactDOM.findDOMNode(instance).querySelector('.panel-title a');
assert.equal(anchor.getAttribute('aria-expanded'), 'true');
});
it('Should be aria-expanded=false', () => {
let instance = ReactTestUtils.renderIntoDocument(
<Panel collapsible={true} expanded={false} header="Heading">Panel content</Panel>
);
let anchor = ReactDOM.findDOMNode(instance).querySelector('.panel-title a');
assert.equal(anchor.getAttribute('aria-expanded'), 'false');
});
it('Should add aria-controls with id', () => {
let instance = ReactTestUtils.renderIntoDocument(
<Panel id='panel-1' collapsible expanded header="Heading">Panel content</Panel>
);
let collapse = ReactDOM.findDOMNode(instance).querySelector('.panel-collapse');
let anchor = ReactDOM.findDOMNode(instance).querySelector('.panel-title a');
assert.equal(collapse.getAttribute('id'), 'panel-1');
assert.equal(anchor.getAttribute('aria-controls'), 'panel-1');
});
});
});