From 195585f5a6ed48735005b088104e9ed45f02700b Mon Sep 17 00:00:00 2001 From: XhmikosR Date: Fri, 8 May 2020 09:08:39 +0300 Subject: [PATCH] scrollspy.js add more tests --- js/tests/unit/scrollspy.js | 137 +++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) diff --git a/js/tests/unit/scrollspy.js b/js/tests/unit/scrollspy.js index 6f5c92caba..2e079bc8cd 100644 --- a/js/tests/unit/scrollspy.js +++ b/js/tests/unit/scrollspy.js @@ -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 = '
' + + '
' + + '
' + + '
' + + '' + + '
' + + '
' + + '
' + + '
' + + '
' + + '

Overview

' + + '

' + + 'Ad leggings keytar, brunch id art party dolor labore.' + + '

' + + '
' + + '
' + + '

Detail

' + + '

' + + 'Veniam marfa mustache skateboard, adipisicing fugiat velit pitchfork beard.' + + '

' + + '
' + + '
' + + '
' + 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 = '
' + + '
' + + '
' + + '
' + + '' + + '
' + + '
' + + '
' + + '
' + + '
' + + '

Overview

' + + '

' + + 'Ad leggings keytar, brunch id art party dolor labore.' + + '

' + + '
' + + '
' + + '

Detail

' + + '

' + + 'Veniam marfa mustache skateboard, adipisicing fugiat velit pitchfork beard.' + + '

' + + '
' + + '
' + + '
' + 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 = '
' + + '
' + + '
' + + '
' + + '' + + '
' + + '
' + + '
' + + '
' + + '
' + + '

Overview

' + + '

' + + 'Ad leggings keytar, brunch id art party dolor labore.' + + '

' + + '
' + + '
' + + '

Detail

' + + '

' + + 'Veniam marfa mustache skateboard, adipisicing fugiat velit pitchfork beard.' + + '

' + + '
' + + '
' + + '
' + + $(templateHTML).appendTo(document.body) + + $('#ss-target').bootstrapScrollspy({ + target: '' + }) + }, /SyntaxError/) + }) })