mirror of
https://github.com/twbs/bootstrap.git
synced 2025-09-30 07:19:13 +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:
@@ -591,4 +591,58 @@ describe('Toast', () => {
|
||||
expect(Toast.getInstance(div)).toEqual(null)
|
||||
})
|
||||
})
|
||||
|
||||
describe('getOrCreateInstance', () => {
|
||||
it('should return toast instance', () => {
|
||||
fixtureEl.innerHTML = '<div></div>'
|
||||
|
||||
const div = fixtureEl.querySelector('div')
|
||||
const toast = new Toast(div)
|
||||
|
||||
expect(Toast.getOrCreateInstance(div)).toEqual(toast)
|
||||
expect(Toast.getInstance(div)).toEqual(Toast.getOrCreateInstance(div, {}))
|
||||
expect(Toast.getOrCreateInstance(div)).toBeInstanceOf(Toast)
|
||||
})
|
||||
|
||||
it('should return new instance when there is no toast instance', () => {
|
||||
fixtureEl.innerHTML = '<div></div>'
|
||||
|
||||
const div = fixtureEl.querySelector('div')
|
||||
|
||||
expect(Toast.getInstance(div)).toEqual(null)
|
||||
expect(Toast.getOrCreateInstance(div)).toBeInstanceOf(Toast)
|
||||
})
|
||||
|
||||
it('should return new instance when there is no toast instance with given configuration', () => {
|
||||
fixtureEl.innerHTML = '<div></div>'
|
||||
|
||||
const div = fixtureEl.querySelector('div')
|
||||
|
||||
expect(Toast.getInstance(div)).toEqual(null)
|
||||
const toast = Toast.getOrCreateInstance(div, {
|
||||
delay: 1
|
||||
})
|
||||
expect(toast).toBeInstanceOf(Toast)
|
||||
|
||||
expect(toast._config.delay).toEqual(1)
|
||||
})
|
||||
|
||||
it('should return the instance when exists without given configuration', () => {
|
||||
fixtureEl.innerHTML = '<div></div>'
|
||||
|
||||
const div = fixtureEl.querySelector('div')
|
||||
const toast = new Toast(div, {
|
||||
delay: 1
|
||||
})
|
||||
expect(Toast.getInstance(div)).toEqual(toast)
|
||||
|
||||
const toast2 = Toast.getOrCreateInstance(div, {
|
||||
delay: 2
|
||||
})
|
||||
expect(toast).toBeInstanceOf(Toast)
|
||||
expect(toast2).toEqual(toast)
|
||||
|
||||
expect(toast2._config.delay).toEqual(1)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
Reference in New Issue
Block a user