aboutsummaryrefslogtreecommitdiff
path: root/js/tests/unit/scrollspy.spec.js
diff options
context:
space:
mode:
authorXhmikosR <[email protected]>2021-08-18 07:29:56 +0300
committerGitHub <[email protected]>2021-08-18 07:29:56 +0300
commit433a148c9e61aa942801fd8101dfa5c4045fdaed (patch)
treef41db59fd06019169df5ea0338213ec0e298f226 /js/tests/unit/scrollspy.spec.js
parentb97cfa163b5098db70e03b27c91fca5dde9c267e (diff)
parent18b3e1ac71f73d006756684a285c5a818e2d1454 (diff)
downloadbootstrap-global-focus-vars.tar.xz
bootstrap-global-focus-vars.zip
Merge branch 'main' into global-focus-varsglobal-focus-vars
Diffstat (limited to 'js/tests/unit/scrollspy.spec.js')
-rw-r--r--js/tests/unit/scrollspy.spec.js91
1 files changed, 79 insertions, 12 deletions
diff --git a/js/tests/unit/scrollspy.spec.js b/js/tests/unit/scrollspy.spec.js
index a00da485f..ad44d5b3c 100644
--- a/js/tests/unit/scrollspy.spec.js
+++ b/js/tests/unit/scrollspy.spec.js
@@ -54,19 +54,15 @@ describe('ScrollSpy', () => {
})
describe('constructor', () => {
- it('should generate an id when there is not one', () => {
- fixtureEl.innerHTML = [
- '<nav></nav>',
- '<div class="content"></div>'
- ].join('')
+ it('should take care of element either passed as a CSS selector or DOM element', () => {
+ fixtureEl.innerHTML = '<nav id="navigation"></nav><div class="content"></div>'
- const navEl = fixtureEl.querySelector('nav')
- const scrollSpy = new ScrollSpy(fixtureEl.querySelector('.content'), {
- target: navEl
- })
+ const sSpyEl = fixtureEl.querySelector('#navigation')
+ const sSpyBySelector = new ScrollSpy('#navigation')
+ const sSpyByElement = new ScrollSpy(sSpyEl)
- expect(scrollSpy).toBeDefined()
- expect(navEl.getAttribute('id')).not.toEqual(null)
+ expect(sSpyBySelector._element).toEqual(sSpyEl)
+ expect(sSpyByElement._element).toEqual(sSpyEl)
})
it('should not process element without target', () => {
@@ -591,7 +587,24 @@ describe('ScrollSpy', () => {
jQueryMock.fn.scrollspy.call(jQueryMock)
- expect(ScrollSpy.getInstance(div)).toBeDefined()
+ expect(ScrollSpy.getInstance(div)).not.toBeNull()
+ })
+
+ it('should create a scrollspy with given config', () => {
+ fixtureEl.innerHTML = '<div></div>'
+
+ const div = fixtureEl.querySelector('div')
+
+ jQueryMock.fn.scrollspy = ScrollSpy.jQueryInterface
+ jQueryMock.elements = [div]
+
+ jQueryMock.fn.scrollspy.call(jQueryMock, { offset: 15 })
+ spyOn(ScrollSpy.prototype, 'constructor')
+ expect(ScrollSpy.prototype.constructor).not.toHaveBeenCalledWith(div, { offset: 15 })
+
+ const scrollspy = ScrollSpy.getInstance(div)
+ expect(scrollspy).not.toBeNull()
+ expect(scrollspy._config.offset).toBe(15)
})
it('should not re create a scrollspy', () => {
@@ -656,6 +669,60 @@ describe('ScrollSpy', () => {
})
})
+ describe('getOrCreateInstance', () => {
+ it('should return scrollspy instance', () => {
+ fixtureEl.innerHTML = '<div></div>'
+
+ const div = fixtureEl.querySelector('div')
+ const scrollspy = new ScrollSpy(div)
+
+ expect(ScrollSpy.getOrCreateInstance(div)).toEqual(scrollspy)
+ expect(ScrollSpy.getInstance(div)).toEqual(ScrollSpy.getOrCreateInstance(div, {}))
+ expect(ScrollSpy.getOrCreateInstance(div)).toBeInstanceOf(ScrollSpy)
+ })
+
+ it('should return new instance when there is no scrollspy instance', () => {
+ fixtureEl.innerHTML = '<div></div>'
+
+ const div = fixtureEl.querySelector('div')
+
+ expect(ScrollSpy.getInstance(div)).toEqual(null)
+ expect(ScrollSpy.getOrCreateInstance(div)).toBeInstanceOf(ScrollSpy)
+ })
+
+ it('should return new instance when there is no scrollspy instance with given configuration', () => {
+ fixtureEl.innerHTML = '<div></div>'
+
+ const div = fixtureEl.querySelector('div')
+
+ expect(ScrollSpy.getInstance(div)).toEqual(null)
+ const scrollspy = ScrollSpy.getOrCreateInstance(div, {
+ offset: 1
+ })
+ expect(scrollspy).toBeInstanceOf(ScrollSpy)
+
+ expect(scrollspy._config.offset).toEqual(1)
+ })
+
+ it('should return the instance when exists without given configuration', () => {
+ fixtureEl.innerHTML = '<div></div>'
+
+ const div = fixtureEl.querySelector('div')
+ const scrollspy = new ScrollSpy(div, {
+ offset: 1
+ })
+ expect(ScrollSpy.getInstance(div)).toEqual(scrollspy)
+
+ const scrollspy2 = ScrollSpy.getOrCreateInstance(div, {
+ offset: 2
+ })
+ expect(scrollspy).toBeInstanceOf(ScrollSpy)
+ expect(scrollspy2).toEqual(scrollspy)
+
+ expect(scrollspy2._config.offset).toEqual(1)
+ })
+ })
+
describe('event handler', () => {
it('should create scrollspy on window load event', () => {
fixtureEl.innerHTML = '<div data-bs-spy="scroll"></div>'