This repository was archived by the owner on Feb 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathBlockUi.spec.js
481 lines (421 loc) · 19.8 KB
/
BlockUi.spec.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
import React from 'react';
import { shallow, mount } from 'enzyme';
import BlockUi from 'react-block-ui';
import Loader from 'react-block-ui/Loader';
const tab = { key: 'Tab', shiftKey: false };
const shiftTab = { keyCode: 9, shiftKey: true };
describe('BlockUi', function() {
describe('not blocking', () => {
it('should render a "div" by default', () => {
const wrapper = shallow(<BlockUi>Yo!</BlockUi>);
expect(wrapper.type()).to.equal('div');
});
it('should render children', () => {
const wrapper = shallow(<BlockUi>Yo!</BlockUi>);
expect(wrapper.text()).to.equal('Yo!');
});
it('should render with the props passed in', () => {
const wrapper = shallow(<BlockUi style={{ textAlign: 'center' }}>Yo!</BlockUi>);
expect(wrapper.prop('style').textAlign).to.equal('center');
});
it('should render just the className passed in', () => {
const wrapper = shallow(<BlockUi className="myClass">Yo!</BlockUi>);
expect(wrapper.prop('className')).to.equal('myClass');
});
it('should render a the tag provided', () => {
const wrapper = shallow(<BlockUi tag="span">Yo!</BlockUi>);
expect(wrapper.type()).to.equal('span');
});
describe('renderChildren is false', () => {
it('should render children', () => {
const wrapper = shallow(<BlockUi renderChildren={false}>Yo!</BlockUi>);
expect(wrapper.text()).to.contain('Yo!');
});
});
});
describe('blocking', () => {
it('should render a "div" by default', () => {
const wrapper = shallow(<BlockUi blocking>Yo!</BlockUi>);
expect(wrapper.type()).to.equal('div');
});
it('should render children between focus blockers', () => {
const wrapper = shallow(<BlockUi blocking>Yo!</BlockUi>);
expect(wrapper.childAt(0).prop('tabIndex')).to.equal('0');
expect(wrapper.childAt(1).text()).to.equal('Yo!');
expect(wrapper.childAt(2).prop('tabIndex')).to.equal('0');
});
it('should render with the props passed in', () => {
const wrapper = shallow(<BlockUi blocking style={{ textAlign: 'center' }}>Yo!</BlockUi>);
expect(wrapper.prop('style').textAlign).to.equal('center');
});
it('should render the className "block-ui"', () => {
const wrapper = shallow(<BlockUi blocking className="myClass">Yo!</BlockUi>);
expect(wrapper.hasClass('block-ui')).to.be.true;
});
it('should render className passed in', () => {
const wrapper = shallow(<BlockUi blocking className="myClass">Yo!</BlockUi>);
expect(wrapper.hasClass('myClass')).to.be.true;
});
it('should render a the tag provided', () => {
const wrapper = shallow(<BlockUi blocking tag="span">Yo!</BlockUi>);
expect(wrapper.type()).to.equal('span');
});
it('should append "block-ui-container" div', () => {
const container = shallow(<BlockUi blocking>Yo!</BlockUi>).childAt(2);
expect(container.type()).to.equal('div');
expect(container.hasClass('block-ui-container')).to.be.true;
});
it('should append "block-ui-overlay" div', () => {
const overlay = shallow(<BlockUi blocking>Yo!</BlockUi>).childAt(2).childAt(0);
expect(overlay.type()).to.equal('div');
expect(overlay.hasClass('block-ui-overlay')).to.be.true;
});
it('should append "block-ui-message-container" div', () => {
const container = shallow(<BlockUi blocking>Yo!</BlockUi>).childAt(2).childAt(1);
expect(container.type()).to.equal('div');
expect(container.hasClass('block-ui-message-container')).to.be.true;
});
it('should append "block-ui-message" div', () => {
const message = shallow(<BlockUi blocking>Yo!</BlockUi>).childAt(2).childAt(1).childAt(0);
expect(message.type()).to.equal('div');
expect(message.hasClass('block-ui-message')).to.be.true;
});
describe('the loader', () => {
it('should append the Loader', () => {
const message = shallow(<BlockUi blocking>Yo!</BlockUi>).childAt(2).childAt(1).childAt(0).childAt(1).childAt(0);
expect(message.type()).to.equal(Loader);
});
it('should append a custom Loader (element) if provided', () => {
const message = shallow(<BlockUi blocking loader={<span />}>Yo!</BlockUi>).childAt(2).childAt(1).childAt(0).childAt(1).childAt(0);
expect(message.type()).to.equal('span');
});
it('should append a custom Loader (string) if provided', () => {
const message = shallow(<BlockUi blocking loader="span">Yo!</BlockUi>).childAt(2).childAt(1).childAt(0).childAt(1).childAt(0);
expect(message.type()).to.equal('span');
});
it('should append a custom Loader (component) if provided', () => {
const message = shallow(<BlockUi blocking loader={Loader}>Yo!</BlockUi>).childAt(2).childAt(1).childAt(0).childAt(1).childAt(0);
expect(message.type()).to.equal(Loader);
});
});
describe('renderChildren is false', () => {
it('should not render children', () => {
const wrapper = shallow(<BlockUi blocking renderChildren={false}>Yo!</BlockUi>);
expect(wrapper.text()).to.not.contain('Yo!');
});
});
describe('tabbing', () => {
describe('top blocker', () => {
describe('tab down', () => {
it('should do nothing with shift; letting the browser handle it', () => {
const wrapper = shallow(<BlockUi blocking><p>Yo</p></BlockUi>);
const instance = wrapper.instance();
instance.blocker = {
focus: sinon.spy().named('focus'),
};
const spy = sinon.spy().named('preventDefault');
wrapper.childAt(0).simulate('keyDown', { ...shiftTab, preventDefault: spy });
expect(instance.blocker.focus).to.not.have.been.called;
expect(spy).to.not.have.been.called;
});
it('should focus on the bottom blocker without shift', () => {
const wrapper = shallow(<BlockUi blocking><p>Yo</p></BlockUi>);
const instance = wrapper.instance();
instance.blocker = {
focus: sinon.spy().named('focus'),
};
const spy = sinon.spy().named('preventDefault');
wrapper.childAt(0).simulate('keyDown', { ...tab, preventDefault: spy });
expect(instance.blocker.focus).to.have.been.called;
expect(spy).to.have.been.called;
});
});
describe('tab up', () => {
it('should do nothing with shift; letting the browser handle it', () => {
const wrapper = shallow(<BlockUi blocking><p>Yo</p></BlockUi>);
const instance = wrapper.instance();
instance.blocker = {
focus: sinon.spy().named('focus'),
};
const spy = sinon.spy().named('preventDefault');
wrapper.childAt(0).simulate('keyUp', { ...shiftTab, preventDefault: spy });
expect(instance.blocker.focus).to.not.have.been.called;
expect(spy).to.not.have.been.called;
});
it('should focus on the bottom blocker without shift', () => {
const wrapper = shallow(<BlockUi blocking><p>Yo</p></BlockUi>);
const instance = wrapper.instance();
instance.blocker = {
focus: sinon.spy().named('focus'),
};
const spy = sinon.spy().named('preventDefault');
wrapper.childAt(0).simulate('keyUp', { ...tab, preventDefault: spy });
expect(instance.blocker.focus).to.have.been.called;
expect(spy).to.not.have.been.called;
});
});
});
describe('bottom blocker', () => {
describe('tab down', () => {
it('should do nothing without shift; letting the browser handle it', () => {
const wrapper = shallow(<BlockUi blocking><p>Yo</p></BlockUi>);
const instance = wrapper.instance();
instance.topFocus = {
focus: sinon.spy().named('focus'),
};
const spy = sinon.spy().named('preventDefault');
wrapper.childAt(2).simulate('keyDown', { ...tab, preventDefault: spy });
expect(instance.topFocus.focus).to.not.have.been.called;
expect(spy).to.not.have.been.called;
});
it('should focus on the top focus blocker with shift', () => {
const wrapper = shallow(<BlockUi blocking><p>Yo</p></BlockUi>);
const instance = wrapper.instance();
instance.topFocus = {
focus: sinon.spy().named('focus'),
};
const spy = sinon.spy().named('preventDefault');
wrapper.childAt(2).simulate('keyDown', { ...shiftTab, preventDefault: spy });
expect(instance.topFocus.focus).to.have.been.called;
expect(spy).to.have.been.called;
});
});
describe('tab up', () => {
it('should do nothing without shift; letting the browser handle it', () => {
const wrapper = shallow(<BlockUi blocking><p>Yo</p></BlockUi>);
const instance = wrapper.instance();
instance.topFocus = {
focus: sinon.spy().named('focus'),
};
const spy = sinon.spy().named('preventDefault');
wrapper.childAt(2).simulate('keyUp', { ...tab, preventDefault: spy });
expect(instance.topFocus.focus).to.not.have.been.called;
expect(spy).to.not.have.been.called;
});
it('should focus on the top focus blocker with shift', () => {
const wrapper = shallow(<BlockUi blocking><p>Yo</p></BlockUi>);
const instance = wrapper.instance();
instance.topFocus = {
focus: sinon.spy().named('focus'),
};
const spy = sinon.spy().named('preventDefault');
wrapper.childAt(2).simulate('keyUp', { ...shiftTab, preventDefault: spy });
expect(instance.topFocus.focus).to.have.been.called;
expect(spy).to.not.have.been.called;
});
});
});
});
});
describe('keepInView', () => {
describe('when not blocking', () => {
it('should do nothing', () => {
const wrapper = mount(<BlockUi keepInView><input /></BlockUi>);
const instance = wrapper.instance();
sinon.spy(instance, 'setState');
instance.keepInView();
expect(instance.setState).to.not.have.been.called;
expect(instance.container).to.not.exist;
});
});
describe('when keepInView prop is falsey', () => {
it('should do nothing', () => {
const wrapper = mount(<BlockUi blocking><input /></BlockUi>);
const instance = wrapper.instance();
sinon.spy(instance, 'setState');
sinon.spy(instance.container, 'getBoundingClientRect');
instance.keepInView();
expect(instance.setState).to.not.have.been.called;
expect(instance.container.getBoundingClientRect).to.not.have.been.called;
});
});
describe('when container is falsey', () => {
it('should do nothing', () => {
const wrapper = mount(<BlockUi blocking keepInView><input /></BlockUi>);
const instance = wrapper.instance();
sinon.spy(instance, 'setState');
instance.container = null;
instance.keepInView();
expect(instance.setState).to.not.have.been.called;
});
});
describe('container completely in viewport', () => {
it('should set state if state.top is not already "50%"', () => {
const wrapper = mount(<BlockUi blocking keepInView><input /></BlockUi>);
const instance = wrapper.instance();
sinon.spy(instance, 'setState');
wrapper.setState({top: 'something else'});
instance.keepInView();
expect(instance.setState).to.have.been.called;
expect(wrapper.state('top')).to.equal('50%');
});
it('should not set state if state.top is already "50%"', () => {
const wrapper = mount(<BlockUi blocking keepInView><input /></BlockUi>);
const instance = wrapper.instance();
sinon.spy(instance, 'setState');
expect(wrapper.state('top')).to.equal('50%');
instance.keepInView();
expect(instance.setState).to.not.have.been.called;
});
});
describe('container partially in viewport', () => {
it('should set state if state.top is needs to change', () => {
const wrapper = mount(<BlockUi blocking keepInView><input /></BlockUi>);
const instance = wrapper.instance();
sinon.spy(instance, 'setState');
wrapper.setState({top: 'something else'});
instance.container.getBoundingClientRect = () => ({top: 100, bottom: window.innerHeight + 100});
instance.keepInView();
expect(instance.setState).to.have.been.called;
expect(wrapper.state('top')).to.equal((window.innerHeight - 100) / 2);
});
it('should render when messageContainer is still null', () => {
const wrapper = mount(<BlockUi blocking keepInView><input /></BlockUi>);
const instance = wrapper.instance();
instance.container.getBoundingClientRect = () => ({top: -100, bottom: window.innerHeight - 100, height: 500});
instance.messageContainer = null;
instance.keepInView();
expect(wrapper.state('top')).to.equal(434);
});
it('should account for the top offset when container top is outside of viewport', () => {
const wrapper = mount(<BlockUi blocking keepInView><input /></BlockUi>);
const instance = wrapper.instance();
instance.container.getBoundingClientRect = () => ({top: -100, bottom: window.innerHeight - 100, height: 500});
instance.messageContainer = {
getBoundingClientRect: () => ({height: 100}),
};
instance.keepInView();
expect(wrapper.state('top')).to.equal(434);
});
it('should claim down so the message does not cut off at the top', () => {
const wrapper = mount(<BlockUi blocking keepInView><input /></BlockUi>);
const instance = wrapper.instance();
instance.container.getBoundingClientRect = () => ({top: window.innerHeight - 50, bottom: window.innerHeight + 100, height: 500});
instance.messageContainer = {
getBoundingClientRect: () => ({height: 100}),
};
instance.keepInView();
expect(wrapper.state('top')).to.equal(50);
});
it('should claim down so the message does not cut off at the bottom', () => {
const wrapper = mount(<BlockUi blocking keepInView><input /></BlockUi>);
const instance = wrapper.instance();
instance.container.getBoundingClientRect = () => ({top: -400, bottom: 100, height: 500});
instance.messageContainer = {
getBoundingClientRect: () => ({height: 100}),
};
instance.keepInView();
expect(wrapper.state('top')).to.equal(450);
});
it('should not set state if state.top is already set to the same value', () => {
const wrapper = mount(<BlockUi blocking keepInView><input /></BlockUi>);
const instance = wrapper.instance();
instance.container.getBoundingClientRect = () => ({top: 200, bottom: window.innerHeight + 200});
wrapper.setState({top: 284});
sinon.spy(instance, 'setState');
instance.keepInView();
expect(instance.setState).to.not.have.been.called;
});
});
describe('container is not in viewport', () => {
it('should do nothing when container is above', () => {
const wrapper = mount(<BlockUi blocking keepInView><input /></BlockUi>);
const instance = wrapper.instance();
sinon.spy(instance, 'setState');
instance.container.getBoundingClientRect = () => ({top: -200, bottom: -100});
instance.keepInView();
expect(instance.setState).to.not.have.been.called;
});
it('should do nothing when container is below', () => {
const wrapper = mount(<BlockUi blocking keepInView><input /></BlockUi>);
const instance = wrapper.instance();
sinon.spy(instance, 'setState');
instance.container.getBoundingClientRect = () => ({top: window.innerHeight + 100, bottom: window.innerHeight + 200});
instance.keepInView();
expect(instance.setState).to.not.have.been.called;
});
});
});
describe('handleScroll', () => {
it('should call keepInView', () => {
const wrapper = mount(<BlockUi blocking keepInView><input /></BlockUi>);
const instance = wrapper.instance();
sinon.spy(instance, 'keepInView');
instance.handleScroll();
expect(instance.keepInView).to.have.been.called;
});
});
describe('not blocking to blocking', () => {
describe('focused in blocking area', () => {
it('should store the focused element and focus on the top focus', () => {
const wrapper = shallow(<BlockUi blocking={false}><input /></BlockUi>);
const instance = wrapper.instance();
instance.helper = { parentNode: { contains: sinon.stub().returns(true) } };
wrapper.find('input').simulate('click');
instance.componentWillReceiveProps({ blocking: true });
expect(instance.focused).to.equal(document.activeElement);
expect(instance.helper.parentNode.contains).to.have.been.called;
});
});
describe('keepInView', () => {
it('should add a scroll listener', () => {
const wrapper = shallow(<BlockUi keepInView><input /></BlockUi>);
const instance = wrapper.instance();
sinon.spy(window, 'addEventListener');
instance.componentWillReceiveProps({ blocking: true, keepInView: true });
expect(window.addEventListener).to.have.been.calledWith('scroll', instance.handleScroll);
window.addEventListener.restore();
});
});
});
describe('not keepInView to keepInView', () => {
it('should add a scroll listener', () => {
const wrapper = shallow(<BlockUi blocking><input /></BlockUi>);
const instance = wrapper.instance();
sinon.spy(window, 'addEventListener');
instance.componentWillReceiveProps({ blocking: true, keepInView: true });
expect(window.addEventListener).to.have.been.calledWith('scroll', instance.handleScroll);
window.addEventListener.restore();
});
});
describe('blocking to not blocking', () => {
describe('previously focused in blocking area', () => {
it('should restore focus', () => {
const wrapper = shallow(<BlockUi blocking><input /></BlockUi>);
const instance = wrapper.instance();
const spy = sinon.spy();
instance.focused = { focus: spy };
instance.componentWillReceiveProps({ blocking: false });
expect(spy).to.have.been.called;
});
});
describe('not previously focused in blocking area', () => {
it('should not throw', () => {
const wrapper = shallow(<BlockUi blocking><input /></BlockUi>);
const instance = wrapper.instance();
instance.focused = null;
expect(instance.componentWillReceiveProps.bind(instance, { blocking: false })).to.not.throw;
});
});
describe('keepInView', () => {
it('should remove a scroll listener', () => {
const wrapper = shallow(<BlockUi blocking><input /></BlockUi>);
const instance = wrapper.instance();
sinon.spy(window, 'removeEventListener');
instance.componentWillReceiveProps({ blocking: false });
expect(window.removeEventListener).to.have.been.calledWith('scroll', instance.handleScroll);
window.removeEventListener.restore();
});
});
});
describe('componentWillUnmount', () => {
it('should remove the scroll listener', () => {
const wrapper = mount(<BlockUi blocking><input /></BlockUi>);
const instance = wrapper.instance();
sinon.spy(window, 'removeEventListener');
wrapper.unmount();
expect(window.removeEventListener).to.have.been.calledWith('scroll', instance.handleScroll);
window.removeEventListener.restore();
});
});
});