Policy 1707 commit to LF
[policy/engine.git] / POLICY-SDK-APP / src / main / webapp / app / policyApp / CSS / bootstrap / js / tests / unit / affix.js
1 $(function () {
2   'use strict';
3
4   QUnit.module('affix plugin')
5
6   QUnit.test('should be defined on jquery object', function (assert) {
7     assert.expect(1)
8     assert.ok($(document.body).affix, 'affix method is defined')
9   })
10
11   QUnit.module('affix', {
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.bootstrapAffix = $.fn.affix.noConflict()
15     },
16     afterEach: function () {
17       $.fn.affix = $.fn.bootstrapAffix
18       delete $.fn.bootstrapAffix
19     }
20   })
21
22   QUnit.test('should provide no conflict', function (assert) {
23     assert.expect(1)
24     assert.strictEqual($.fn.affix, undefined, 'affix was set back to undefined (org value)')
25   })
26
27   QUnit.test('should return jquery collection containing the element', function (assert) {
28     assert.expect(2)
29     var $el = $('<div/>')
30     var $affix = $el.bootstrapAffix()
31     assert.ok($affix instanceof $, 'returns jquery collection')
32     assert.strictEqual($affix[0], $el[0], 'collection contains element')
33   })
34
35   QUnit.test('should exit early if element is not visible', function (assert) {
36     assert.expect(1)
37     var $affix = $('<div style="display: none"/>').bootstrapAffix()
38     $affix.data('bs.affix').checkPosition()
39     assert.ok(!$affix.hasClass('affix'), 'affix class was not added')
40   })
41
42   QUnit.test('should trigger affixed event after affix', function (assert) {
43     assert.expect(2)
44     var done = assert.async()
45
46     var templateHTML = '<div id="affixTarget">'
47         + '<ul>'
48         + '<li>Please affix</li>'
49         + '<li>And unaffix</li>'
50         + '</ul>'
51         + '</div>'
52         + '<div id="affixAfter" style="height: 20000px; display: block;"/>'
53     $(templateHTML).appendTo(document.body)
54
55     $('#affixTarget').bootstrapAffix({
56       offset: $('#affixTarget ul').position()
57     })
58
59     $('#affixTarget')
60       .on('affix.bs.affix', function () {
61         assert.ok(true, 'affix event fired')
62       }).on('affixed.bs.affix', function () {
63         assert.ok(true, 'affixed event fired')
64         $('#affixTarget, #affixAfter').remove()
65         done()
66       })
67
68     setTimeout(function () {
69       window.scrollTo(0, document.body.scrollHeight)
70
71       setTimeout(function () {
72         window.scroll(0, 0)
73       }, 16) // for testing in a browser
74     }, 0)
75   })
76
77   QUnit.test('should affix-top when scrolling up to offset when parent has padding', function (assert) {
78     assert.expect(1)
79     var done = assert.async()
80
81     var templateHTML = '<div id="padding-offset" style="padding-top: 20px;">'
82         + '<div id="affixTopTarget">'
83         + '<p>Testing affix-top class is added</p>'
84         + '</div>'
85         + '<div style="height: 1000px; display: block;"/>'
86         + '</div>'
87     $(templateHTML).appendTo(document.body)
88
89     $('#affixTopTarget')
90       .bootstrapAffix({
91         offset: { top: 120, bottom: 0 }
92       })
93       .on('affixed-top.bs.affix', function () {
94         assert.ok($('#affixTopTarget').hasClass('affix-top'), 'affix-top class applied')
95         $('#padding-offset').remove()
96         done()
97       })
98
99     setTimeout(function () {
100       window.scrollTo(0, document.body.scrollHeight)
101
102       setTimeout(function () {
103         window.scroll(0, 119)
104       }, 250)
105     }, 250)
106   })
107 })