/
OffcanvasSpec.js
246 lines (212 loc) · 6.31 KB
/
OffcanvasSpec.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
242
243
244
245
246
import { mount } from 'enzyme';
import * as React from 'react';
// import simulant from 'simulant';
import ModalManager from 'react-overlays/ModalManager';
import Offcanvas from '../src/Offcanvas';
describe('<Offcanvas>', () => {
afterEach(() => {
// make sure the dangling portal elements get cleaned up
document.body.innerHTML = '';
});
it('Should render the modal content', () => {
const noOp = () => {};
mount(
<Offcanvas show onHide={noOp}>
<strong>Message</strong>
</Offcanvas>,
)
.find('strong')
.text()
.should.equal('Message');
});
it('Should set `visibility: visible` to `div.offcanvas`', () => {
const node = mount(
<Offcanvas show>
<strong>Message</strong>
</Offcanvas>,
)
.find('div.offcanvas')
.getDOMNode();
expect(node.style.visibility).to.equal('visible');
});
it('Should close the offcanvas when the modal close button is clicked', (done) => {
const doneOp = () => {
done();
};
mount(
<Offcanvas show onHide={doneOp}>
<Offcanvas.Header closeButton />
<strong>Message</strong>
</Offcanvas>,
)
.find('.btn-close')
.simulate('click');
});
it('Should pass className to the offcanvas', () => {
const noOp = () => {};
mount(
<Offcanvas show className="myoffcanvas" onHide={noOp}>
<strong>Message</strong>
</Offcanvas>,
).assertSingle('div.offcanvas.myoffcanvas');
});
it('Should pass backdropClassName to the backdrop', () => {
const noOp = () => {};
mount(
<Offcanvas show backdropClassName="custom-backdrop" onHide={noOp}>
<strong>Message</strong>
</Offcanvas>,
).find('.modal-backdrop.custom-backdrop');
});
it('Should pass style to the offcanvas', () => {
const noOp = () => {};
const dialog = mount(
<Offcanvas show style={{ color: 'red' }} onHide={noOp}>
<strong>Message</strong>
</Offcanvas>,
)
.find('div.offcanvas')
.getDOMNode();
assert.ok(dialog.style.color === 'red');
});
it('Should pass transition callbacks to Transition', (done) => {
const increment = sinon.spy();
const instance = mount(
<Offcanvas
show
onHide={() => {}}
onExit={increment}
onExiting={increment}
onExited={() => {
increment();
expect(increment.callCount).to.equal(6);
done();
}}
onEnter={increment}
onEntering={increment}
onEntered={() => {
increment();
instance.setProps({ show: false });
}}
>
<strong>Message</strong>
</Offcanvas>,
);
});
it('Should close when backdrop clicked', () => {
const onHideSpy = sinon.spy();
mount(
<Offcanvas show onHide={onHideSpy}>
<strong>Message</strong>
</Offcanvas>,
)
.find('div.modal-backdrop')
.simulate('click');
expect(onHideSpy).to.have.been.called;
});
// TODO: unsure if we need this, since it seems like Offcanvas is still undergoing some
// changes upstream.
// it('Should close when anything outside offcanvas clicked and backdrop=false', () => {
// const onHideSpy = sinon.spy();
// mount(
// <>
// <Offcanvas show onHide={onHideSpy} backdrop={false}>
// <strong>Message</strong>
// </Offcanvas>
// <button type="button" id="mybutton">
// my button
// </button>
// </>,
// );
// simulant.fire(document.body, 'click');
// expect(onHideSpy).to.have.been.called;
// });
it('Should not call onHide if the click target comes from inside the offcanvas', () => {
const onHideSpy = sinon.spy();
const wrapper = mount(
<>
<Offcanvas show onHide={onHideSpy}>
<strong>Message</strong>
</Offcanvas>
<div id="outside">outside</div>
</>,
);
wrapper.find('div.offcanvas').simulate('click');
expect(onHideSpy).to.not.have.been.called;
});
it('Should set aria-labelledby to the role="dialog" element if aria-labelledby set', () => {
const noOp = () => {};
mount(
<Offcanvas show onHide={noOp} aria-labelledby="offcanvas-title">
<Offcanvas.Header closeButton>
<Offcanvas.Title id="offcanvas-title">
Offcanvas heading
</Offcanvas.Title>
</Offcanvas.Header>
</Offcanvas>,
).assertSingle(
'div.offcanvas.show[role="dialog"][aria-labelledby="offcanvas-title"]',
);
});
it('Should call onEscapeKeyDown when keyboard is true', () => {
const noOp = () => {};
const onEscapeKeyDownSpy = sinon.spy();
mount(
<Offcanvas
show
onHide={noOp}
keyboard
onEscapeKeyDown={onEscapeKeyDownSpy}
>
<strong>Message</strong>
</Offcanvas>,
);
const event = new KeyboardEvent('keydown', { keyCode: 27 });
document.dispatchEvent(event);
expect(onEscapeKeyDownSpy).to.have.been.called;
});
it('Should not call onEscapeKeyDown when keyboard is false', () => {
const noOp = () => {};
const onEscapeKeyDownSpy = sinon.spy();
mount(
<Offcanvas
show
onHide={noOp}
keyboard={false}
onEscapeKeyDown={onEscapeKeyDownSpy}
>
<strong>Message</strong>
</Offcanvas>,
);
const event = new KeyboardEvent('keydown', { keyCode: 27 });
document.dispatchEvent(event);
expect(onEscapeKeyDownSpy).to.not.have.been.called;
});
it('Should use custom props manager if specified', (done) => {
const noOp = () => {};
class MyModalManager extends ModalManager {
add() {
done();
}
}
const managerRef = React.createRef();
managerRef.current = new MyModalManager();
mount(
<Offcanvas show onHide={noOp} manager={managerRef.current}>
<strong>Message</strong>
</Offcanvas>,
);
});
it('should not change overflow style when scroll=true', () => {
const containerRef = React.createRef();
const noOp = () => {};
mount(
<div ref={containerRef} style={{ height: '2000px', overflow: 'scroll' }}>
<Offcanvas show onHide={noOp} container={containerRef} scroll>
<strong>Message</strong>
</Offcanvas>
</div>,
);
expect(containerRef.current.style.overflow).to.equal('scroll');
});
});