mirror of
https://github.com/twbs/bootstrap.git
synced 2025-08-18 11:21:23 +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:
@@ -287,4 +287,58 @@ describe('Popover', () => {
|
||||
expect(Popover.getInstance(popoverEl)).toEqual(null)
|
||||
})
|
||||
})
|
||||
|
||||
describe('getOrCreateInstance', () => {
|
||||
it('should return popover instance', () => {
|
||||
fixtureEl.innerHTML = '<div></div>'
|
||||
|
||||
const div = fixtureEl.querySelector('div')
|
||||
const popover = new Popover(div)
|
||||
|
||||
expect(Popover.getOrCreateInstance(div)).toEqual(popover)
|
||||
expect(Popover.getInstance(div)).toEqual(Popover.getOrCreateInstance(div, {}))
|
||||
expect(Popover.getOrCreateInstance(div)).toBeInstanceOf(Popover)
|
||||
})
|
||||
|
||||
it('should return new instance when there is no popover instance', () => {
|
||||
fixtureEl.innerHTML = '<div></div>'
|
||||
|
||||
const div = fixtureEl.querySelector('div')
|
||||
|
||||
expect(Popover.getInstance(div)).toEqual(null)
|
||||
expect(Popover.getOrCreateInstance(div)).toBeInstanceOf(Popover)
|
||||
})
|
||||
|
||||
it('should return new instance when there is no popover instance with given configuration', () => {
|
||||
fixtureEl.innerHTML = '<div></div>'
|
||||
|
||||
const div = fixtureEl.querySelector('div')
|
||||
|
||||
expect(Popover.getInstance(div)).toEqual(null)
|
||||
const popover = Popover.getOrCreateInstance(div, {
|
||||
placement: 'top'
|
||||
})
|
||||
expect(popover).toBeInstanceOf(Popover)
|
||||
|
||||
expect(popover._config.placement).toEqual('top')
|
||||
})
|
||||
|
||||
it('should return the instance when exists without given configuration', () => {
|
||||
fixtureEl.innerHTML = '<div></div>'
|
||||
|
||||
const div = fixtureEl.querySelector('div')
|
||||
const popover = new Popover(div, {
|
||||
placement: 'top'
|
||||
})
|
||||
expect(Popover.getInstance(div)).toEqual(popover)
|
||||
|
||||
const popover2 = Popover.getOrCreateInstance(div, {
|
||||
placement: 'bottom'
|
||||
})
|
||||
expect(popover).toBeInstanceOf(Popover)
|
||||
expect(popover2).toEqual(popover)
|
||||
|
||||
expect(popover2._config.placement).toEqual('top')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
Reference in New Issue
Block a user