4 QUnit.module('affix plugin')
6 QUnit.test('should be defined on jquery object', function (assert) {
8 assert.ok($(document.body).affix, 'affix method is defined')
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()
16 afterEach: function () {
17 $.fn.affix = $.fn.bootstrapAffix
18 delete $.fn.bootstrapAffix
22 QUnit.test('should provide no conflict', function (assert) {
24 assert.strictEqual($.fn.affix, undefined, 'affix was set back to undefined (org value)')
27 QUnit.test('should return jquery collection containing the element', function (assert) {
30 var $affix = $el.bootstrapAffix()
31 assert.ok($affix instanceof $, 'returns jquery collection')
32 assert.strictEqual($affix[0], $el[0], 'collection contains element')
35 QUnit.test('should exit early if element is not visible', function (assert) {
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')
42 QUnit.test('should trigger affixed event after affix', function (assert) {
44 var done = assert.async()
46 var templateHTML = '<div id="affixTarget">'
48 + '<li>Please affix</li>'
49 + '<li>And unaffix</li>'
52 + '<div id="affixAfter" style="height: 20000px; display: block;"/>'
53 $(templateHTML).appendTo(document.body)
55 $('#affixTarget').bootstrapAffix({
56 offset: $('#affixTarget ul').position()
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()
68 setTimeout(function () {
69 window.scrollTo(0, document.body.scrollHeight)
71 setTimeout(function () {
73 }, 16) // for testing in a browser
77 QUnit.test('should affix-top when scrolling up to offset when parent has padding', function (assert) {
79 var done = assert.async()
81 var templateHTML = '<div id="padding-offset" style="padding-top: 20px;">'
82 + '<div id="affixTopTarget">'
83 + '<p>Testing affix-top class is added</p>'
85 + '<div style="height: 1000px; display: block;"/>'
87 $(templateHTML).appendTo(document.body)
91 offset: { top: 120, bottom: 0 }
93 .on('affixed-top.bs.affix', function () {
94 assert.ok($('#affixTopTarget').hasClass('affix-top'), 'affix-top class applied')
95 $('#padding-offset').remove()
99 setTimeout(function () {
100 window.scrollTo(0, document.body.scrollHeight)
102 setTimeout(function () {
103 window.scroll(0, 119)