forked from react-bootstrap/react-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollapseSpec.js
241 lines (180 loc) · 5.93 KB
/
CollapseSpec.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import React from 'react';
import ReactTestUtils from 'react/lib/ReactTestUtils';
import ReactDOM from 'react-dom';
import Collapse from '../src/Collapse';
describe('Collapse', () => {
let Component, instance;
beforeEach(() => {
Component = React.createClass({
render() {
let { children, ...props } = this.props;
return (
<Collapse
ref={r => this.collapse = r}
getDimensionValue={()=> 15 }
{...props}
{...this.state}
>
<div>
<div ref="panel">
{children}
</div>
</div>
</Collapse>
);
}
});
});
it('Should default to collapsed', () => {
instance = ReactTestUtils.renderIntoDocument(
<Component>Panel content</Component>
);
assert.ok(
instance.collapse.props.in === false);
});
describe('collapsed', () => {
it('Should have collapse class', () => {
instance = ReactTestUtils.renderIntoDocument(
<Component>Panel content</Component>
);
assert.ok(ReactTestUtils.findRenderedDOMComponentWithClass(instance, 'collapse'));
});
});
describe('from collapsed to expanded', () => {
let scrollHeightStub;
beforeEach(() => {
instance = ReactTestUtils.renderIntoDocument(
<Component>Panel content</Component>
);
// since scrollHeight is gonna be 0 detached from the DOM
scrollHeightStub = sinon.stub(instance.collapse, '_getScrollDimensionValue');
scrollHeightStub.returns('15px');
});
it('Should have collapsing class', () => {
instance.setState({ in: true });
let node = ReactDOM.findDOMNode(instance);
assert.equal(node.className, 'collapsing');
});
it('Should set initial 0px height', (done) => {
let node = ReactDOM.findDOMNode(instance);
function onEnter() {
assert.equal(node.style.height, '0px');
done();
}
assert.equal(node.style.height, '');
instance.setState({ in: true, onEnter });
});
it('Should set node to height', () => {
let node = ReactDOM.findDOMNode(instance);
assert.equal(node.styled, undefined);
instance.setState({ in: true });
assert.equal(node.style.height, '15px');
});
it('Should transition from collapsing to not collapsing', (done) => {
let node = ReactDOM.findDOMNode(instance);
function onEntered() {
assert.equal(node.className, 'collapse in');
done();
}
instance.setState({ in: true, onEntered });
assert.equal(node.className, 'collapsing');
});
it('Should clear height after transition complete', (done) => {
let node = ReactDOM.findDOMNode(instance);
function onEntered() {
assert.equal(node.style.height, '');
done();
}
assert.equal(node.style.height, '');
instance.setState({ in: true, onEntered });
assert.equal(node.style.height, '15px');
});
});
describe('from expanded to collapsed', () => {
beforeEach(() => {
instance = ReactTestUtils.renderIntoDocument(
<Component in>Panel content</Component>
);
});
it('Should have collapsing class', () => {
instance.setState({ in: false });
let node = ReactDOM.findDOMNode(instance);
assert.equal(node.className, 'collapsing');
});
it('Should set initial height', () => {
let node = ReactDOM.findDOMNode(instance);
function onExit() {
assert.equal(node.style.height, '15px');
}
assert.equal(node.style.height, '');
instance.setState({ in: false, onExit });
});
it('Should set node to height', () => {
let node = ReactDOM.findDOMNode(instance);
assert.equal(node.style.height, '');
instance.setState({ in: false });
assert.equal(node.style.height, '0px');
});
it('Should transition from collapsing to not collapsing', (done) => {
let node = ReactDOM.findDOMNode(instance);
function onExited() {
assert.equal(node.className, 'collapse');
done();
}
instance.setState({ in: false, onExited });
assert.equal(node.className, 'collapsing');
});
it('Should have 0px height after transition complete', (done) => {
let node = ReactDOM.findDOMNode(instance);
function onExited() {
assert.ok(node.style.height === '0px');
done();
}
assert.equal(node.style.height, '');
instance.setState({ in: false, onExited });
});
});
describe('expanded', () => {
it('Should have collapse and in class', () => {
instance = ReactTestUtils.renderIntoDocument(
<Component in >Panel content</Component>
);
expect(ReactDOM.findDOMNode(instance.collapse).className)
.to.match(/\bcollapse in\b/);
});
});
describe('dimension', () => {
beforeEach(() => {
instance = ReactTestUtils.renderIntoDocument(
<Component>Panel content</Component>
);
});
it('Defaults to height', () => {
assert.equal(instance.collapse._dimension(), 'height');
});
it('Uses getCollapsibleDimension if exists', () => {
function dimension() {
return 'whatevs';
}
instance.setState({ dimension });
assert.equal(instance.collapse._dimension(), 'whatevs');
});
});
describe('with a role', () => {
beforeEach(() => {
instance = ReactTestUtils.renderIntoDocument(
<Component role="note">Panel content</Component>
);
});
it('sets aria-expanded true when expanded', () => {
let node = ReactDOM.findDOMNode(instance);
instance.setState({ in: true});
assert.equal(node.getAttribute('aria-expanded'), 'true');
});
it('sets aria-expanded false when collapsed', () => {
let node = ReactDOM.findDOMNode(instance);
instance.setState({ in: false});
assert.equal(node.getAttribute('aria-expanded'), 'false');
});
});
});