mirror of
https://github.com/twbs/bootstrap.git
synced 2025-08-13 00:54:04 +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:
@@ -3,7 +3,7 @@ import EventHandler from '../../src/dom/event-handler'
|
||||
import { noop } from '../../src/util/index'
|
||||
|
||||
/** Test helpers */
|
||||
import { getFixture, clearFixture, jQueryMock, createEvent } from '../helpers/fixture'
|
||||
import { clearFixture, createEvent, getFixture, jQueryMock } from '../helpers/fixture'
|
||||
|
||||
describe('Tooltip', () => {
|
||||
let fixtureEl
|
||||
@@ -1306,6 +1306,60 @@ describe('Tooltip', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('getOrCreateInstance', () => {
|
||||
it('should return tooltip instance', () => {
|
||||
fixtureEl.innerHTML = '<div></div>'
|
||||
|
||||
const div = fixtureEl.querySelector('div')
|
||||
const tooltip = new Tooltip(div)
|
||||
|
||||
expect(Tooltip.getOrCreateInstance(div)).toEqual(tooltip)
|
||||
expect(Tooltip.getInstance(div)).toEqual(Tooltip.getOrCreateInstance(div, {}))
|
||||
expect(Tooltip.getOrCreateInstance(div)).toBeInstanceOf(Tooltip)
|
||||
})
|
||||
|
||||
it('should return new instance when there is no tooltip instance', () => {
|
||||
fixtureEl.innerHTML = '<div></div>'
|
||||
|
||||
const div = fixtureEl.querySelector('div')
|
||||
|
||||
expect(Tooltip.getInstance(div)).toEqual(null)
|
||||
expect(Tooltip.getOrCreateInstance(div)).toBeInstanceOf(Tooltip)
|
||||
})
|
||||
|
||||
it('should return new instance when there is no tooltip instance with given configuration', () => {
|
||||
fixtureEl.innerHTML = '<div></div>'
|
||||
|
||||
const div = fixtureEl.querySelector('div')
|
||||
|
||||
expect(Tooltip.getInstance(div)).toEqual(null)
|
||||
const tooltip = Tooltip.getOrCreateInstance(div, {
|
||||
title: () => 'test'
|
||||
})
|
||||
expect(tooltip).toBeInstanceOf(Tooltip)
|
||||
|
||||
expect(tooltip.getTitle()).toEqual('test')
|
||||
})
|
||||
|
||||
it('should return the instance when exists without given configuration', () => {
|
||||
fixtureEl.innerHTML = '<div></div>'
|
||||
|
||||
const div = fixtureEl.querySelector('div')
|
||||
const tooltip = new Tooltip(div, {
|
||||
title: () => 'nothing'
|
||||
})
|
||||
expect(Tooltip.getInstance(div)).toEqual(tooltip)
|
||||
|
||||
const tooltip2 = Tooltip.getOrCreateInstance(div, {
|
||||
title: () => 'test'
|
||||
})
|
||||
expect(tooltip).toBeInstanceOf(Tooltip)
|
||||
expect(tooltip2).toEqual(tooltip)
|
||||
|
||||
expect(tooltip2.getTitle()).toEqual('nothing')
|
||||
})
|
||||
})
|
||||
|
||||
describe('jQueryInterface', () => {
|
||||
it('should create a tooltip', () => {
|
||||
fixtureEl.innerHTML = '<div></div>'
|
||||
|
Reference in New Issue
Block a user