Policy 1707 commit to LF
[policy/engine.git] / POLICY-SDK-APP / src / main / webapp / app / policyApp / CSS / bootstrap / js / tests / unit / modal.js
1 $(function () {
2   'use strict';
3
4   QUnit.module('modal plugin')
5
6   QUnit.test('should be defined on jquery object', function (assert) {
7     assert.expect(1)
8     assert.ok($(document.body).modal, 'modal method is defined')
9   })
10
11   QUnit.module('modal', {
12     beforeEach: function () {
13       // Run all tests in noConflict mode -- it's the only way to ensure that the plugin works in noConflict mode
14       $.fn.bootstrapModal = $.fn.modal.noConflict()
15     },
16     afterEach: function () {
17       $.fn.modal = $.fn.bootstrapModal
18       delete $.fn.bootstrapModal
19     }
20   })
21
22   QUnit.test('should provide no conflict', function (assert) {
23     assert.expect(1)
24     assert.strictEqual($.fn.modal, undefined, 'modal was set back to undefined (orig value)')
25   })
26
27   QUnit.test('should return jquery collection containing the element', function (assert) {
28     assert.expect(2)
29     var $el = $('<div id="modal-test"/>')
30     var $modal = $el.bootstrapModal()
31     assert.ok($modal instanceof $, 'returns jquery collection')
32     assert.strictEqual($modal[0], $el[0], 'collection contains element')
33   })
34
35   QUnit.test('should expose defaults var for settings', function (assert) {
36     assert.expect(1)
37     assert.ok($.fn.bootstrapModal.Constructor.DEFAULTS, 'default object exposed')
38   })
39
40   QUnit.test('should insert into dom when show method is called', function (assert) {
41     assert.expect(1)
42     var done = assert.async()
43
44     $('<div id="modal-test"/>')
45       .on('shown.bs.modal', function () {
46         assert.notEqual($('#modal-test').length, 0, 'modal inserted into dom')
47         done()
48       })
49       .bootstrapModal('show')
50   })
51
52   QUnit.test('should set aria-hidden to false when show method is called', function (assert) {
53     assert.expect(1)
54     var done = assert.async()
55
56     $('<div id="modal-test"/>')
57       .on('shown.bs.modal', function () {
58         assert.strictEqual($('#modal-test').attr('aria-hidden'), 'false', 'aria-hidden is set to string "false" when modal shown')
59         done()
60       })
61       .bootstrapModal('show')
62   })
63
64   QUnit.test('should fire show event', function (assert) {
65     assert.expect(1)
66     var done = assert.async()
67
68     $('<div id="modal-test"/>')
69       .on('show.bs.modal', function () {
70         assert.ok(true, 'show event fired')
71         done()
72       })
73       .bootstrapModal('show')
74   })
75
76   QUnit.test('should not fire shown when show was prevented', function (assert) {
77     assert.expect(1)
78     var done = assert.async()
79
80     $('<div id="modal-test"/>')
81       .on('show.bs.modal', function (e) {
82         e.preventDefault()
83         assert.ok(true, 'show event fired')
84         done()
85       })
86       .on('shown.bs.modal', function () {
87         assert.ok(false, 'shown event fired')
88       })
89       .bootstrapModal('show')
90   })
91
92   QUnit.test('should hide modal when hide is called', function (assert) {
93     assert.expect(3)
94     var done = assert.async()
95
96     $('<div id="modal-test"/>')
97       .on('shown.bs.modal', function () {
98         assert.ok($('#modal-test').is(':visible'), 'modal visible')
99         assert.notEqual($('#modal-test').length, 0, 'modal inserted into dom')
100         $(this).bootstrapModal('hide')
101       })
102       .on('hidden.bs.modal', function () {
103         assert.ok(!$('#modal-test').is(':visible'), 'modal hidden')
104         done()
105       })
106       .bootstrapModal('show')
107   })
108
109   QUnit.test('should set aria-hidden to true when hide is called', function (assert) {
110     assert.expect(2)
111     var done = assert.async()
112
113     $('<div id="modal-test"/>')
114       .on('shown.bs.modal', function () {
115         assert.strictEqual($('#modal-test').length, 1, 'modal has been inserted into the dom')
116         $(this).bootstrapModal('hide')
117       })
118       .on('hidden.bs.modal', function () {
119         assert.strictEqual($('#modal-test').attr('aria-hidden'), 'true', 'aria-hidden is set to string "true" when modal shown')
120         done()
121       })
122       .bootstrapModal('show')
123   })
124
125   QUnit.test('should toggle when toggle is called', function (assert) {
126     assert.expect(3)
127     var done = assert.async()
128
129     $('<div id="modal-test"/>')
130       .on('shown.bs.modal', function () {
131         assert.ok($('#modal-test').is(':visible'), 'modal visible')
132         assert.notEqual($('#modal-test').length, 0, 'modal inserted into dom')
133         $(this).bootstrapModal('toggle')
134       })
135       .on('hidden.bs.modal', function () {
136         assert.ok(!$('#modal-test').is(':visible'), 'modal hidden')
137         done()
138       })
139       .bootstrapModal('toggle')
140   })
141
142   QUnit.test('should remove from dom when click [data-dismiss="modal"]', function (assert) {
143     assert.expect(3)
144     var done = assert.async()
145
146     $('<div id="modal-test"><span class="close" data-dismiss="modal"/></div>')
147       .on('shown.bs.modal', function () {
148         assert.ok($('#modal-test').is(':visible'), 'modal visible')
149         assert.notEqual($('#modal-test').length, 0, 'modal inserted into dom')
150         $(this).find('.close').trigger('click')
151       })
152       .on('hidden.bs.modal', function () {
153         assert.ok(!$('#modal-test').is(':visible'), 'modal hidden')
154         done()
155       })
156       .bootstrapModal('toggle')
157   })
158
159   QUnit.test('should allow modal close with "backdrop:false"', function (assert) {
160     assert.expect(2)
161     var done = assert.async()
162
163     $('<div id="modal-test" data-backdrop="false"/>')
164       .on('shown.bs.modal', function () {
165         assert.ok($('#modal-test').is(':visible'), 'modal visible')
166         $(this).bootstrapModal('hide')
167       })
168       .on('hidden.bs.modal', function () {
169         assert.ok(!$('#modal-test').is(':visible'), 'modal hidden')
170         done()
171       })
172       .bootstrapModal('show')
173   })
174
175   QUnit.test('should close modal when clicking outside of modal-content', function (assert) {
176     assert.expect(3)
177     var done = assert.async()
178
179     $('<div id="modal-test"><div class="contents"/></div>')
180       .on('shown.bs.modal', function () {
181         assert.notEqual($('#modal-test').length, 0, 'modal inserted into dom')
182         $('.contents').trigger('click')
183         assert.ok($('#modal-test').is(':visible'), 'modal visible')
184         $('#modal-test').trigger('click')
185       })
186       .on('hidden.bs.modal', function () {
187         assert.ok(!$('#modal-test').is(':visible'), 'modal hidden')
188         done()
189       })
190       .bootstrapModal('show')
191   })
192
193   QUnit.test('should close modal when escape key is pressed via keydown', function (assert) {
194     assert.expect(3)
195     var done = assert.async()
196
197     var div = $('<div id="modal-test"/>')
198     div
199       .on('shown.bs.modal', function () {
200         assert.ok($('#modal-test').length, 'modal insterted into dom')
201         assert.ok($('#modal-test').is(':visible'), 'modal visible')
202         div.trigger($.Event('keydown', { which: 27 }))
203
204         setTimeout(function () {
205           assert.ok(!$('#modal-test').is(':visible'), 'modal hidden')
206           div.remove()
207           done()
208         }, 0)
209       })
210       .bootstrapModal('show')
211   })
212
213   QUnit.test('should not close modal when escape key is pressed via keyup', function (assert) {
214     assert.expect(3)
215     var done = assert.async()
216
217     var div = $('<div id="modal-test"/>')
218     div
219       .on('shown.bs.modal', function () {
220         assert.ok($('#modal-test').length, 'modal inserted into dom')
221         assert.ok($('#modal-test').is(':visible'), 'modal visible')
222         div.trigger($.Event('keyup', { which: 27 }))
223
224         setTimeout(function () {
225           assert.ok($('#modal-test').is(':visible'), 'modal still visible')
226           div.remove()
227           done()
228         }, 0)
229       })
230       .bootstrapModal('show')
231   })
232
233   QUnit.test('should trigger hide event once when clicking outside of modal-content', function (assert) {
234     assert.expect(1)
235     var done = assert.async()
236
237     var triggered
238
239     $('<div id="modal-test"><div class="contents"/></div>')
240       .on('shown.bs.modal', function () {
241         triggered = 0
242         $('#modal-test').trigger('click')
243       })
244       .on('hide.bs.modal', function () {
245         triggered += 1
246         assert.strictEqual(triggered, 1, 'modal hide triggered once')
247         done()
248       })
249       .bootstrapModal('show')
250   })
251
252   QUnit.test('should close reopened modal with [data-dismiss="modal"] click', function (assert) {
253     assert.expect(2)
254     var done = assert.async()
255
256     $('<div id="modal-test"><div class="contents"><div id="close" data-dismiss="modal"/></div></div>')
257       .one('shown.bs.modal', function () {
258         $('#close').trigger('click')
259       })
260       .one('hidden.bs.modal', function () {
261         // after one open-close cycle
262         assert.ok(!$('#modal-test').is(':visible'), 'modal hidden')
263         $(this)
264           .one('shown.bs.modal', function () {
265             $('#close').trigger('click')
266           })
267           .one('hidden.bs.modal', function () {
268             assert.ok(!$('#modal-test').is(':visible'), 'modal hidden')
269             done()
270           })
271           .bootstrapModal('show')
272       })
273       .bootstrapModal('show')
274   })
275
276   QUnit.test('should restore focus to toggling element when modal is hidden after having been opened via data-api', function (assert) {
277     assert.expect(1)
278     var done = assert.async()
279
280     var $toggleBtn = $('<button data-toggle="modal" data-target="#modal-test"/>').appendTo('#qunit-fixture')
281
282     $('<div id="modal-test"><div class="contents"><div id="close" data-dismiss="modal"/></div></div>')
283       .on('hidden.bs.modal', function () {
284         setTimeout(function () {
285           assert.ok($(document.activeElement).is($toggleBtn), 'toggling element is once again focused')
286           done()
287         }, 0)
288       })
289       .on('shown.bs.modal', function () {
290         $('#close').trigger('click')
291       })
292       .appendTo('#qunit-fixture')
293
294     $toggleBtn.trigger('click')
295   })
296
297   QUnit.test('should not restore focus to toggling element if the associated show event gets prevented', function (assert) {
298     assert.expect(1)
299     var done = assert.async()
300     var $toggleBtn = $('<button data-toggle="modal" data-target="#modal-test"/>').appendTo('#qunit-fixture')
301     var $otherBtn = $('<button id="other-btn"/>').appendTo('#qunit-fixture')
302
303     $('<div id="modal-test"><div class="contents"><div id="close" data-dismiss="modal"/></div>')
304       .one('show.bs.modal', function (e) {
305         e.preventDefault()
306         $otherBtn.trigger('focus')
307         setTimeout($.proxy(function () {
308           $(this).bootstrapModal('show')
309         }, this), 0)
310       })
311       .on('hidden.bs.modal', function () {
312         setTimeout(function () {
313           assert.ok($(document.activeElement).is($otherBtn), 'focus returned to toggling element')
314           done()
315         }, 0)
316       })
317       .on('shown.bs.modal', function () {
318         $('#close').trigger('click')
319       })
320       .appendTo('#qunit-fixture')
321
322     $toggleBtn.trigger('click')
323   })
324
325   QUnit.test('should restore inline body padding after closing', function (assert) {
326     assert.expect(2)
327     var done = assert.async()
328     var originalBodyPad = 0
329     var $body = $(document.body)
330
331     $body.css('padding-right', originalBodyPad)
332
333     $('<div id="modal-test"/>')
334       .on('hidden.bs.modal', function () {
335         var currentBodyPad = parseInt($body.css('padding-right'), 10)
336         assert.notStrictEqual($body.attr('style'), '', 'body has non-empty style attribute')
337         assert.strictEqual(currentBodyPad, originalBodyPad, 'original body padding was not changed')
338         $body.removeAttr('style')
339         done()
340       })
341       .on('shown.bs.modal', function () {
342         $(this).bootstrapModal('hide')
343       })
344       .bootstrapModal('show')
345   })
346
347   QUnit.test('should ignore values set via CSS when trying to restore body padding after closing', function (assert) {
348     assert.expect(1)
349     var done = assert.async()
350     var $body = $(document.body)
351     var $style = $('<style>body { padding-right: 42px; }</style>').appendTo('head')
352
353     $('<div id="modal-test"/>')
354       .on('hidden.bs.modal', function () {
355         assert.ok(!$body.attr('style'), 'body does not have inline padding set')
356         $style.remove()
357         done()
358       })
359       .on('shown.bs.modal', function () {
360         $(this).bootstrapModal('hide')
361       })
362       .bootstrapModal('show')
363   })
364
365   QUnit.test('should ignore other inline styles when trying to restore body padding after closing', function (assert) {
366     assert.expect(2)
367     var done = assert.async()
368     var $body = $(document.body)
369     var $style = $('<style>body { padding-right: 42px; }</style>').appendTo('head')
370
371     $body.css('color', 'red')
372
373     $('<div id="modal-test"/>')
374       .on('hidden.bs.modal', function () {
375         assert.strictEqual($body[0].style.paddingRight, '', 'body does not have inline padding set')
376         assert.strictEqual($body[0].style.color, 'red', 'body still has other inline styles set')
377         $body.removeAttr('style')
378         $style.remove()
379         done()
380       })
381       .on('shown.bs.modal', function () {
382         $(this).bootstrapModal('hide')
383       })
384       .bootstrapModal('show')
385   })
386
387   QUnit.test('should properly restore non-pixel inline body padding after closing', function (assert) {
388     assert.expect(1)
389     var done = assert.async()
390     var $body = $(document.body)
391
392     $body.css('padding-right', '5%')
393
394     $('<div id="modal-test"/>')
395       .on('hidden.bs.modal', function () {
396         assert.strictEqual($body[0].style.paddingRight, '5%', 'body does not have inline padding set')
397         $body.removeAttr('style')
398         done()
399       })
400       .on('shown.bs.modal', function () {
401         $(this).bootstrapModal('hide')
402       })
403       .bootstrapModal('show')
404   })
405 })