/
jquery.hideShow.spec.js
485 lines (375 loc) · 16.6 KB
/
jquery.hideShow.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
482
483
484
485
'use strict';
describe('hide-show', function() {
var markUp =
'<div>' +
'<p>Text content to be hidden text content to be hidden text content to be hidden text content to be hidden</p>' +
'</div>',
testElement;
var markUp2 =
'<div class="container">' +
'<h3 id="trigger">Contact details</h3>' +
'<div>' +
'<p>Telephone 01234 567 890</p>' +
'<p>Email <a href="mailto:test@test.com">test@test.com</a></p>' +
'</div>' +
'</div>',
testElement2;
beforeEach(function() {
testElement = $(markUp);
});
it('depends on jQuery', function() {
expect($).toBeDefined();
});
it('should be protected against multiple instantiations', function() {
var plugin = testElement.hideShow();
expect(plugin === testElement.hideShow()).toBe(true);
});
describe('- plugin init', function() {
beforeEach(function() {
testElement.hideShow();
});
it('should be available with the showHide identifier', function() {
expect($('<div></div>').hideShow).toBeDefined();
});
it('should add a class and ID to the content to be hidden', function() {
expect(testElement.hasClass('js-hide-show_content')).toBe(true);
expect(testElement.attr('id')).toContain('content');
});
it('should set the state to visible by default', function() {
expect(testElement.hasClass('js-hide-show_content--expanded')).toBe(true);
expect(testElement.attr('aria-hidden')).toBe('false');
expect(testElement.css('display')).toBe('block');
expect(testElement.siblings('.js-hide-show_btn').attr('aria-expanded')).toBe('true');
});
it('should insert the trigger element before the content by default', function() {
expect(testElement.prev('.js-hide-show_btn').length).toBe(1);
});
describe('- createTriggerElement function', function() {
it('should create the trigger element', function() {
var testElementId = testElement.attr('id');
expect(testElement.siblings('.js-hide-show_btn').attr('aria-controls')).toBe(testElementId);
expect(testElement.siblings('.js-hide-show_btn').hasClass('js-hide-show_btn')).toBe(true);
expect(testElement.siblings('.js-hide-show_btn').text()).toBe('Hide Content');
});
});
});
describe('- toggle function', function() {
beforeEach(function() {
testElement.hideShow();
spyOn(jQuery.fn, 'slideUp');
spyOn(jQuery.fn, 'slideDown');
});
it('should toggle the content open when closed', function() {
var button = testElement.siblings('.js-hide-show_btn');
button.click();
expect(jQuery.fn.slideUp).toHaveBeenCalled();
});
it('should toggle the content closed when open', function() {
var button = testElement.siblings('.js-hide-show_btn');
testElement.data('plugin_hideShow').close();
button.click();
expect(jQuery.fn.slideDown).toHaveBeenCalled();
});
});
describe('- open method', function() {
beforeEach(function() {
testElement.hideShow();
spyOn(jQuery.fn, 'slideDown');
});
it('should open the content when the trigger element is clicked', function() {
var button = testElement.siblings('.js-hide-show_btn');
testElement.data('plugin_hideShow').close();
button.click();
expect(testElement.hasClass('js-hide-show_content--expanded')).toBe(true);
expect(testElement.attr('aria-hidden')).toBe('false');
expect(jQuery.fn.slideDown).toHaveBeenCalled();
});
it('should update the trigger element when it is clicked', function() {
var button = testElement.siblings('.js-hide-show_btn');
testElement.data('plugin_hideShow').close();
button.click();
expect(button.hasClass('js-hide-show_btn--expanded')).toBe(true);
expect(button.attr('aria-expanded')).toBe('true');
expect(button.text()).toBe('Hide Content');
});
});
describe('- close method', function() {
beforeEach(function() {
testElement.hideShow();
spyOn(jQuery.fn, 'slideUp');
});
it('should close the content when the trigger element is clicked', function() {
var button = testElement.siblings('.js-hide-show_btn');
button.click();
expect(testElement.hasClass('js-hide-show_content--collapsed')).toBe(true);
expect(testElement.attr('aria-hidden')).toBe('true');
expect(jQuery.fn.slideUp).toHaveBeenCalled();
});
it('should update the trigger element when it is clicked', function() {
var button = testElement.siblings('.js-hide-show_btn');
button.click();
expect(button.hasClass('js-hide-show_btn--collapsed')).toBe(true);
expect(button.attr('aria-expanded')).toBe('false');
expect(button.text()).toBe('Show Content');
});
});
describe('- rebuild method', function() {
it('should reinitiate the plugin', function() {
var plugin = testElement.hideShow();
plugin.data('plugin_hideShow').destroy();
plugin.data('plugin_hideShow').rebuild();
expect(plugin === testElement.hideShow()).toBe(true);
});
});
describe('- destroy method', function() {
beforeEach(function() {
testElement2 = $(markUp2);
testElement.hideShow();
testElement.data('plugin_hideShow').destroy();
});
it('should reset the content element back to its original state', function() {
expect(testElement.attr('aria-hidden')).toBe(undefined);
expect(testElement.attr('id')).toBe(undefined);
expect(testElement.attr('style')).toBe(undefined);
expect(testElement.hasClass('js-hide-show_content')).toBe(false);
expect(testElement.hasClass('js-hide-show_content--collapsed')).toBe(false);
expect(testElement.hasClass('js-hide-show_content--expanded')).toBe(false);
});
it('should remove the trigger element', function() {
expect(testElement.siblings('.js-hide-show_btn').length).toBe(0);
});
it('should reset the trigger element back to its original state', function() {
var trigger = $('#trigger', testElement2);
testElement2.find('div').hideShow({
triggerElementTarget: trigger
});
testElement2.find('div').data('plugin_hideShow').destroy();
var testElementId = testElement2.find('div').attr('id');
expect(trigger.attr('aria-controls')).toBe(undefined);
expect(trigger.attr('aria-expanded')).toBe(undefined);
expect(trigger.attr('role')).toBe(undefined);
expect(trigger.attr('tabindex')).toBe(undefined);
expect(trigger.hasClass('js-hide-show_btn')).toBe(false);
expect(trigger.hasClass('js-hide-show_btn--expanded')).toBe(false);
expect(trigger.hasClass('js-hide-show_btn--collapsed')).toBe(false);
});
});
describe('- plugin options', function() {
beforeEach(function() {
testElement2 = $(markUp2);
});
it('should add a tabindex of -1 to the element if the trigger does not immediately precede it', function() {
testElement2.find('div').hideShow({
insertMethod: 'append',
insertTriggerLocation: testElement2
});
expect(testElement2.find('div').attr('tabindex')).toBe('-1');
});
it('should hide the content if the state is set to "hidden"', function() {
testElement.hideShow({
state: 'hidden'
});
expect(testElement.hasClass('js-hide-show_content--collapsed')).toBe(true);
expect(testElement.attr('aria-hidden')).toBe('true');
expect(testElement.css('display')).toBe('none');
expect(testElement.siblings('.js-hide-show_btn').attr('aria-expanded')).toBe('false');
expect(testElement.siblings('.js-hide-show_btn').text()).toBe('Show Content');
});
it('should insert the trigger element after the content if the insertMethod is set to "after"', function() {
testElement2.find('div').hideShow({
insertMethod: 'after',
insertTriggerLocation: testElement2
});
expect(testElement2.next('.js-hide-show_btn').length).toBe(1);
});
it('should insert the trigger element after the content if the insertMethod is set to "append"', function() {
testElement2.find('div').hideShow({
insertMethod: 'append',
insertTriggerLocation: testElement2
});
expect(testElement2.find('div').next('.js-hide-show_btn').length).toBe(1);
});
it('should insert the trigger element after the content if the insertMethod is set to "prepend"', function() {
testElement2.find('div').hideShow({
insertMethod: 'prepend',
insertTriggerLocation: testElement2
});
expect(testElement2.find('h3').prev('.js-hide-show_btn').length).toBe(1);
});
it('should default to inserting the trigger element before the content if the insertMethod is not "after", "append" or "prepend"', function() {
testElement2.find('div').hideShow({
insertMethod: '',
insertTriggerLocation: testElement2.find('div')
});
expect(testElement2.find('div').prev('.js-hide-show_btn').length).toBe(1);
});
it('should use an existing element if the triggerElementTarget option is given', function() {
var trigger = $('#trigger', testElement2);
testElement2.find('div').hideShow({
triggerElementTarget: trigger
});
var testElementId = testElement2.find('div').attr('id');
expect(trigger.attr('aria-controls')).toBe(testElementId);
expect(trigger.hasClass('js-hide-show_btn')).toBe(true);
expect(trigger.attr('role')).toBe('button');
expect(trigger.attr('tabindex')).toBe('0');
});
it('should customise the "hideText" used for the button', function() {
testElement.hideShow({
hideText: 'Collapse details'
});
expect(testElement.siblings('.js-hide-show_btn').text()).toBe('Collapse details');
});
it('should customise the "showText" used for the button', function() {
testElement.hideShow({
showText: 'Expand details',
state: 'hidden'
});
expect(testElement.siblings('.js-hide-show_btn').text()).toBe('Expand details');
});
it('should customise the class on the button', function() {
testElement.hideShow({
buttonClass: 'trigger-button'
});
expect(testElement.siblings('.trigger-button').length).toBe(1);
});
it('should customise the collapsed class on the button', function() {
testElement.hideShow({
buttonExpandedClass: 'collapsed'
});
expect(testElement.siblings('.js-hide-show_btn').hasClass('collapsed')).toBe(true);
});
it('should customise the expanded class on the button', function() {
testElement.hideShow({
buttonExpandedClass: 'expanded'
});
expect(testElement.siblings('.js-hide-show_btn').hasClass('expanded')).toBe(true);
});
it('should customise the class on the container', function() {
testElement.hideShow({
containerClass: 'container'
});
expect(testElement.hasClass('container')).toBe(true);
});
it('should customise the collapsed class on the container', function() {
testElement.hideShow({
containerCollapsedClass: 'collapsed',
state: 'hidden'
});
expect(testElement.hasClass('collapsed')).toBe(true);
});
it('should customise the expanded class on the container', function() {
testElement.hideShow({
containerExpandedClass: 'expanded'
});
expect(testElement.hasClass('expanded')).toBe(true);
});
it('should trigger "callbackAfterClose" when the close public method is called', function() {
var mocks,
el,
afterClose = false;
mocks = {
callbackAfterClose: function(testElement) {
afterClose = true;
}
},
el = testElement.hideShow({
callbackAfterClose: mocks.callbackAfterClose
});
el.data('plugin_hideShow').close();
expect(afterClose).toBe(true);
});
it('should trigger "callbackAfterOpen" when the open public method is called', function() {
var mocks,
el,
afterOpen = false;
mocks = {
callbackAfterOpen: function(testElement) {
afterOpen = true;
}
},
el = testElement.hideShow({
callbackAfterOpen: mocks.callbackAfterOpen
});
el.data('plugin_hideShow').open();
expect(afterOpen).toBe(true);
});
it('should trigger "callbackBeforeClose" when the close public method is called', function() {
var mocks,
el,
beforeClose = false;
mocks = {
callbackBeforeClose: function(testElement) {
beforeClose = true;
}
},
el = testElement.hideShow({
callbackBeforeClose: mocks.callbackBeforeClose
});
el.data('plugin_hideShow').close();
expect(beforeClose).toBe(true);
});
it('should trigger "callbackBeforeOpen" when the open public method is called', function() {
var mocks,
el,
beforeOpen = false;
mocks = {
callbackBeforeOpen: function(testElement) {
beforeOpen = true;
}
},
el = testElement.hideShow({
callbackBeforeOpen: mocks.callbackBeforeOpen
});
el.data('plugin_hideShow').open();
expect(beforeOpen).toBe(true);
});
it('should trigger "callbackCreate" once the plugin has been created', function() {
var mocks,
el,
created = false;
mocks = {
callbackCreate: function(testElement) {
created = true;
}
},
el = testElement.hideShow({
callbackCreate: mocks.callbackCreate
});
expect(created).toBe(true);
});
it('should trigger "callbackDestroy" once the plugin has been destroyed', function() {
var mocks,
el,
button;
el = testElement.hideShow({
closeOnClick: true
});
button = testElement.siblings('.js-hide-show_btn');
// Click to close the content that is open by default
button.click();
// Click to open the content
button.click();
// Move the content so that it is off the page
el.hide();
// Click elsewhere on the page
$(document).mouseup();
expect(testElement.attr('aria-hidden')).toBe('true');
});
it('should hide the content if clicked elsewhere in the document', function() {
var mocks,
el,
destroyed = false;
mocks = {
callbackDestroy: function(testElement) {
destroyed = true;
}
},
el = testElement.hideShow({
callbackDestroy: mocks.callbackDestroy
});
el.data('plugin_hideShow').destroy();
expect(destroyed).toBe(true);
});
});
});