0
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-02-19 16:54:24 +01:00

scrollspy.js add more tests

This commit is contained in:
XhmikosR 2020-05-08 09:08:39 +03:00
parent fa8a7eca06
commit 195585f5a6

View File

@ -138,6 +138,104 @@ $(function () {
$scrollspy.scrollTop(350)
})
// This could be simplified/improved later
QUnit.test('should only switch "active" class on current target specified w jQuery element', function (assert) {
assert.expect(1)
var done = assert.async()
var sectionHTML = '<div id="root" class="active">' +
'<div class="topbar">' +
'<div class="topbar-inner">' +
'<div class="container" id="ss-target">' +
'<ul class="nav">' +
'<li class="nav-item"><a href="#masthead">Overview</a></li>' +
'<li class="nav-item"><a href="#detail">Detail</a></li>' +
'</ul>' +
'</div>' +
'</div>' +
'</div>' +
'<div id="scrollspy-example" style="height: 100px; overflow: auto;">' +
'<div style="height: 200px;">' +
'<h4 id="masthead">Overview</h4>' +
'<p style="height: 200px">' +
'Ad leggings keytar, brunch id art party dolor labore.' +
'</p>' +
'</div>' +
'<div style="height: 200px;">' +
'<h4 id="detail">Detail</h4>' +
'<p style="height: 200px">' +
'Veniam marfa mustache skateboard, adipisicing fugiat velit pitchfork beard.' +
'</p>' +
'</div>' +
'</div>' +
'</div>'
var $section = $(sectionHTML).appendTo('#qunit-fixture')
var $scrollspy = $section
.show()
.find('#scrollspy-example')
.bootstrapScrollspy({
target: $('#ss-target')
})
$scrollspy.one('scroll', function () {
assert.ok($section.hasClass('active'), '"active" class still on root node')
done()
})
$scrollspy.scrollTop(350)
})
// This could be simplified/improved later
QUnit.test('should only switch "active" class on current target specified without ID', function (assert) {
assert.expect(2)
var done = assert.async()
var sectionHTML = '<div id="root" class="active">' +
'<div class="topbar">' +
'<div class="topbar-inner">' +
'<div class="container">' +
'<ul class="nav">' +
'<li class="nav-item"><a href="#masthead">Overview</a></li>' +
'<li class="nav-item"><a href="#detail">Detail</a></li>' +
'</ul>' +
'</div>' +
'</div>' +
'</div>' +
'<div id="scrollspy-example" style="height: 100px; overflow: auto;">' +
'<div style="height: 200px;">' +
'<h4 id="masthead">Overview</h4>' +
'<p style="height: 200px">' +
'Ad leggings keytar, brunch id art party dolor labore.' +
'</p>' +
'</div>' +
'<div style="height: 200px;">' +
'<h4 id="detail">Detail</h4>' +
'<p style="height: 200px">' +
'Veniam marfa mustache skateboard, adipisicing fugiat velit pitchfork beard.' +
'</p>' +
'</div>' +
'</div>' +
'</div>'
var $section = $(sectionHTML).appendTo('#qunit-fixture')
var $scrollspy = $section
.show()
.find('#scrollspy-example')
.bootstrapScrollspy({
target: $('.container')
})
assert.ok($('.container').attr('id').length > 0, '`target` has an ID attribute')
$scrollspy.one('scroll', function () {
assert.ok($section.hasClass('active'), '"active" class still on root node')
done()
})
$scrollspy.scrollTop(350)
})
QUnit.test('should correctly select middle navigation option when large offset is used', function (assert) {
assert.expect(3)
var done = assert.async()
@ -733,4 +831,43 @@ $(function () {
testOffsetMethod('js')
testOffsetMethod('data')
})
// This could be simplified/improved later
QUnit.test('should raise exception to avoid xss on target', function (assert) {
assert.expect(1)
assert.throws(function () {
var templateHTML = '<div id="root" class="active">' +
'<div class="topbar">' +
'<div class="topbar-inner">' +
'<div class="container" id="ss-target">' +
'<ul class="nav">' +
'<li class="nav-item"><a href="#masthead">Overview</a></li>' +
'<li class="nav-item"><a href="#detail">Detail</a></li>' +
'</ul>' +
'</div>' +
'</div>' +
'</div>' +
'<div id="scrollspy-example" style="height: 100px; overflow: auto;">' +
'<div style="height: 200px;">' +
'<h4 id="masthead">Overview</h4>' +
'<p style="height: 200px">' +
'Ad leggings keytar, brunch id art party dolor labore.' +
'</p>' +
'</div>' +
'<div style="height: 200px;">' +
'<h4 id="detail">Detail</h4>' +
'<p style="height: 200px">' +
'Veniam marfa mustache skateboard, adipisicing fugiat velit pitchfork beard.' +
'</p>' +
'</div>' +
'</div>' +
'</div>'
$(templateHTML).appendTo(document.body)
$('#ss-target').bootstrapScrollspy({
target: '<img src=1 onerror=\'alert(0)\'>'
})
}, /SyntaxError/)
})
})