diff options
| author | GeoSot <[email protected]> | 2022-07-28 11:58:28 +0300 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-07-28 11:58:28 +0300 |
| commit | db86607c088bd307aa21f4b4bd0258262262a4e4 (patch) | |
| tree | 7acb80965491822be470802088d0ce85d4087e5c /js/tests | |
| parent | 90c50ab198a4ecffdda6a5ff10fe58cab2c816b2 (diff) | |
| download | bootstrap-db86607c088bd307aa21f4b4bd0258262262a4e4.tar.xz bootstrap-db86607c088bd307aa21f4b4bd0258262262a4e4.zip | |
ScrollSpy: make the threshold option configurable (#36750)
* feat(ScrollSpy): make the threshold option configurable
Diffstat (limited to 'js/tests')
| -rw-r--r-- | js/tests/unit/scrollspy.spec.js | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/js/tests/unit/scrollspy.spec.js b/js/tests/unit/scrollspy.spec.js index 2bdeb5830..c7951e6ff 100644 --- a/js/tests/unit/scrollspy.spec.js +++ b/js/tests/unit/scrollspy.spec.js @@ -126,6 +126,50 @@ describe('ScrollSpy', () => { expect(scrollSpy._rootElement).toBeNull() }) + it('should respect threshold option', () => { + fixtureEl.innerHTML = [ + '<ul id="navigation" class="navbar">', + ' <a class="nav-link active" id="one-link" href="#">One</a>' + + '</ul>', + '<div id="content">', + ' <div id="one-link">test</div>', + '</div>' + ].join('') + + const scrollSpy = new ScrollSpy('#content', { + target: '#navigation', + threshold: [1] + }) + + expect(scrollSpy._observer.thresholds).toEqual([1]) + }) + + it('should respect threshold option markup', () => { + fixtureEl.innerHTML = [ + '<ul id="navigation" class="navbar">', + ' <a class="nav-link active" id="one-link" href="#">One</a>' + + '</ul>', + '<div id="content" data-bs-threshold="0,0.2,1">', + ' <div id="one-link">test</div>', + '</div>' + ].join('') + + const scrollSpy = new ScrollSpy('#content', { + target: '#navigation' + }) + + // See https://stackoverflow.com/a/45592926 + const expectToBeCloseToArray = (actual, expected) => { + expect(actual.length).toBe(expected.length) + for (const x of actual) { + const i = actual.indexOf(x) + expect(x).withContext(`[${i}]`).toBeCloseTo(expected[i]) + } + } + + expectToBeCloseToArray(scrollSpy._observer.thresholds, [0, 0.2, 1]) + }) + it('should not take count to not visible sections', () => { fixtureEl.innerHTML = [ '<nav id="navigation" class="navbar">', |
