2012-07-23 03:28:39 +02:00
|
|
|
$(function () {
|
2014-03-17 08:12:55 +01:00
|
|
|
'use strict';
|
2012-07-23 03:28:39 +02:00
|
|
|
|
2014-04-22 07:03:33 +02:00
|
|
|
module('affix plugin')
|
2014-02-13 08:55:12 +01:00
|
|
|
|
2015-02-24 06:55:07 +01:00
|
|
|
test('should be defined on jquery object', function (assert) {
|
|
|
|
assert.ok($(document.body).affix, 'affix method is defined')
|
2014-02-13 08:55:12 +01:00
|
|
|
})
|
|
|
|
|
2014-04-22 07:03:33 +02:00
|
|
|
module('affix', {
|
2014-03-17 08:12:55 +01:00
|
|
|
setup: function () {
|
2014-04-22 07:03:33 +02:00
|
|
|
// Run all tests in noConflict mode -- it's the only way to ensure that the plugin works in noConflict mode
|
|
|
|
$.fn.bootstrapAffix = $.fn.affix.noConflict()
|
|
|
|
},
|
2014-03-17 08:12:55 +01:00
|
|
|
teardown: function () {
|
2014-04-22 07:03:33 +02:00
|
|
|
$.fn.affix = $.fn.bootstrapAffix
|
|
|
|
delete $.fn.bootstrapAffix
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2015-02-24 06:55:07 +01:00
|
|
|
test('should provide no conflict', function (assert) {
|
|
|
|
assert.strictEqual($.fn.affix, undefined, 'affix was set back to undefined (org value)')
|
2014-04-22 07:03:33 +02:00
|
|
|
})
|
|
|
|
|
2015-02-24 06:55:07 +01:00
|
|
|
test('should return jquery collection containing the element', function (assert) {
|
2014-06-18 21:33:22 +02:00
|
|
|
var $el = $('<div/>')
|
|
|
|
var $affix = $el.bootstrapAffix()
|
2015-02-24 06:55:07 +01:00
|
|
|
assert.ok($affix instanceof $, 'returns jquery collection')
|
|
|
|
assert.strictEqual($affix[0], $el[0], 'collection contains element')
|
2014-02-13 08:55:12 +01:00
|
|
|
})
|
|
|
|
|
2015-02-24 06:55:07 +01:00
|
|
|
test('should exit early if element is not visible', function (assert) {
|
2014-06-18 21:33:22 +02:00
|
|
|
var $affix = $('<div style="display: none"/>').bootstrapAffix()
|
2014-02-13 08:55:12 +01:00
|
|
|
$affix.data('bs.affix').checkPosition()
|
2015-02-24 06:55:07 +01:00
|
|
|
assert.ok(!$affix.hasClass('affix'), 'affix class was not added')
|
2014-02-13 08:55:12 +01:00
|
|
|
})
|
|
|
|
|
2015-01-21 04:40:50 +01:00
|
|
|
test('should trigger affixed event after affix', function (assert) {
|
|
|
|
var done = assert.async()
|
2014-02-13 08:55:12 +01:00
|
|
|
|
2014-07-06 11:56:12 +02:00
|
|
|
var templateHTML = '<div id="affixTarget">'
|
|
|
|
+ '<ul>'
|
|
|
|
+ '<li>Please affix</li>'
|
|
|
|
+ '<li>And unaffix</li>'
|
|
|
|
+ '</ul>'
|
|
|
|
+ '</div>'
|
|
|
|
+ '<div id="affixAfter" style="height: 20000px; display: block;"/>'
|
2014-06-18 21:33:22 +02:00
|
|
|
$(templateHTML).appendTo(document.body)
|
2014-02-13 08:55:12 +01:00
|
|
|
|
2014-04-22 07:03:33 +02:00
|
|
|
$('#affixTarget').bootstrapAffix({
|
2014-02-13 08:55:12 +01:00
|
|
|
offset: $('#affixTarget ul').position()
|
|
|
|
})
|
|
|
|
|
|
|
|
$('#affixTarget')
|
2014-03-01 17:19:50 +01:00
|
|
|
.on('affix.bs.affix', function () {
|
2015-02-24 06:55:07 +01:00
|
|
|
assert.ok(true, 'affix event fired')
|
2014-03-01 17:19:50 +01:00
|
|
|
}).on('affixed.bs.affix', function () {
|
2015-02-24 06:55:07 +01:00
|
|
|
assert.ok(true, 'affixed event fired')
|
2014-06-18 21:33:22 +02:00
|
|
|
$('#affixTarget, #affixAfter').remove()
|
2015-01-21 04:40:50 +01:00
|
|
|
done()
|
2012-07-23 03:28:39 +02:00
|
|
|
})
|
|
|
|
|
2014-02-13 08:55:12 +01:00
|
|
|
setTimeout(function () {
|
|
|
|
window.scrollTo(0, document.body.scrollHeight)
|
2014-06-18 21:33:22 +02:00
|
|
|
|
2014-05-29 06:35:22 +02:00
|
|
|
setTimeout(function () {
|
|
|
|
window.scroll(0, 0)
|
2014-07-06 09:23:41 +02:00
|
|
|
}, 16) // for testing in a browser
|
2014-03-17 08:12:55 +01:00
|
|
|
}, 0)
|
2014-02-13 08:55:12 +01:00
|
|
|
})
|
2014-11-17 01:42:55 +01:00
|
|
|
|
2015-01-21 04:40:50 +01:00
|
|
|
test('should affix-top when scrolling up to offset when parent has padding', function (assert) {
|
|
|
|
var done = assert.async()
|
2014-11-17 01:42:55 +01:00
|
|
|
|
|
|
|
var templateHTML = '<div id="padding-offset" style="padding-top: 20px;">'
|
|
|
|
+ '<div id="affixTopTarget">'
|
|
|
|
+ '<p>Testing affix-top class is added</p>'
|
|
|
|
+ '</div>'
|
|
|
|
+ '<div style="height: 1000px; display: block;"/>'
|
|
|
|
+ '</div>'
|
|
|
|
$(templateHTML).appendTo(document.body)
|
|
|
|
|
|
|
|
$('#affixTopTarget')
|
|
|
|
.bootstrapAffix({
|
|
|
|
offset: { top: 120, bottom: 0 }
|
|
|
|
})
|
|
|
|
.on('affixed-top.bs.affix', function () {
|
2015-02-24 06:55:07 +01:00
|
|
|
assert.ok($('#affixTopTarget').hasClass('affix-top'), 'affix-top class applied')
|
2014-11-21 08:26:37 +01:00
|
|
|
$('#padding-offset').remove()
|
2015-01-21 04:40:50 +01:00
|
|
|
done()
|
2014-11-17 01:42:55 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
setTimeout(function () {
|
|
|
|
window.scrollTo(0, document.body.scrollHeight)
|
|
|
|
|
|
|
|
setTimeout(function () {
|
|
|
|
window.scroll(0, 119)
|
|
|
|
}, 250)
|
|
|
|
}, 250)
|
|
|
|
})
|
2013-04-23 09:34:27 +02:00
|
|
|
})
|