mirror of
https://github.com/twbs/bootstrap.git
synced 2025-09-25 21:09:06 +02:00
Add getOrCreateInstance
method in base-component (#33276)
Co-authored-by: Rohit Sharma <rohit2sharma95@gmail.com> Co-authored-by: XhmikosR <xhmikosr@gmail.com>
This commit is contained in:
@@ -684,6 +684,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>'
|
||||
|
Reference in New Issue
Block a user