defineJQueryPlugin,
getElementFromSelector
} from './util/index'
-import Data from './dom/data'
import EventHandler from './dom/event-handler'
import BaseComponent from './base-component'
static jQueryInterface(config) {
return this.each(function () {
- let data = Data.get(this, DATA_KEY)
-
- if (!data) {
- data = new Alert(this)
- }
+ const data = Alert.getOrCreateInstance(this)
if (config === 'close') {
data[config](this)
return Data.get(element, this.DATA_KEY)
}
+ static getOrCreateInstance(element, config = {}) {
+ return this.getInstance(element) || new this(element, typeof config === 'object' ? config : null)
+ }
+
static get VERSION() {
return VERSION
}
*/
import { defineJQueryPlugin } from './util/index'
-import Data from './dom/data'
import EventHandler from './dom/event-handler'
import BaseComponent from './base-component'
static jQueryInterface(config) {
return this.each(function () {
- let data = Data.get(this, DATA_KEY)
-
- if (!data) {
- data = new Button(this)
- }
+ const data = Button.getOrCreateInstance(this)
if (config === 'toggle') {
data[config]()
event.preventDefault()
const button = event.target.closest(SELECTOR_DATA_TOGGLE)
-
- let data = Data.get(button, DATA_KEY)
- if (!data) {
- data = new Button(button)
- }
+ const data = Button.getOrCreateInstance(button)
data.toggle()
})
triggerTransitionEnd,
typeCheckConfig
} from './util/index'
-import Data from './dom/data'
import EventHandler from './dom/event-handler'
import Manipulator from './dom/manipulator'
import SelectorEngine from './dom/selector-engine'
_getConfig(config) {
config = {
...Default,
- ...config
+ ...Manipulator.getDataAttributes(this._element),
+ ...(typeof config === 'object' ? config : {})
}
typeCheckConfig(NAME, config, DefaultType)
return config
// Static
static carouselInterface(element, config) {
- let data = Data.get(element, DATA_KEY)
- let _config = {
- ...Default,
- ...Manipulator.getDataAttributes(element)
- }
-
- if (typeof config === 'object') {
- _config = {
- ..._config,
- ...config
- }
- }
+ const data = Carousel.getOrCreateInstance(element, config)
+ const { _config } = data
const action = typeof config === 'string' ? config : _config.slide
- if (!data) {
- data = new Carousel(element, _config)
- }
-
if (typeof config === 'number') {
data.to(config)
} else if (typeof action === 'string') {
Carousel.carouselInterface(target, config)
if (slideIndex) {
- Data.get(target, DATA_KEY).to(slideIndex)
+ Carousel.getInstance(target).to(slideIndex)
}
event.preventDefault()
const carousels = SelectorEngine.find(SELECTOR_DATA_RIDE)
for (let i = 0, len = carousels.length; i < len; i++) {
- Carousel.carouselInterface(carousels[i], Data.get(carousels[i], DATA_KEY))
+ Carousel.carouselInterface(carousels[i], Carousel.getInstance(carousels[i]))
}
})
const container = SelectorEngine.findOne(this._selector)
if (actives) {
const tempActiveData = actives.find(elem => container !== elem)
- activesData = tempActiveData ? Data.get(tempActiveData, DATA_KEY) : null
+ activesData = tempActiveData ? Collapse.getInstance(tempActiveData) : null
if (activesData && activesData._isTransitioning) {
return
// Static
static collapseInterface(element, config) {
- let data = Data.get(element, DATA_KEY)
+ let data = Collapse.getInstance(element)
const _config = {
...Default,
...Manipulator.getDataAttributes(element),
const selectorElements = SelectorEngine.find(selector)
selectorElements.forEach(element => {
- const data = Data.get(element, DATA_KEY)
+ const data = Collapse.getInstance(element)
let config
if (data) {
// update parent attribute
getNextActiveElement,
typeCheckConfig
} from './util/index'
-import Data from './dom/data'
import EventHandler from './dom/event-handler'
import Manipulator from './dom/manipulator'
import SelectorEngine from './dom/selector-engine'
// Static
static dropdownInterface(element, config) {
- let data = Data.get(element, DATA_KEY)
- const _config = typeof config === 'object' ? config : null
-
- if (!data) {
- data = new Dropdown(element, _config)
- }
+ const data = Dropdown.getOrCreateInstance(element, config)
if (typeof config === 'string') {
if (typeof data[config] === 'undefined') {
const toggles = SelectorEngine.find(SELECTOR_DATA_TOGGLE)
for (let i = 0, len = toggles.length; i < len; i++) {
- const context = Data.get(toggles[i], DATA_KEY)
+ const context = Dropdown.getInstance(toggles[i])
if (!context || context._config.autoClose === false) {
continue
}
config = {
...Default,
...Manipulator.getDataAttributes(this._element),
- ...config
+ ...(typeof config === 'object' ? config : {})
}
typeCheckConfig(NAME, config, DefaultType)
return config
static jQueryInterface(config, relatedTarget) {
return this.each(function () {
- const data = Modal.getInstance(this) || new Modal(this, typeof config === 'object' ? config : {})
+ const data = Modal.getOrCreateInstance(this, config)
if (typeof config !== 'string') {
return
})
})
- const data = Modal.getInstance(target) || new Modal(target)
+ const data = Modal.getOrCreateInstance(target)
data.toggle(this)
})
typeCheckConfig
} from './util/index'
import { hide as scrollBarHide, reset as scrollBarReset } from './util/scrollbar'
-import Data from './dom/data'
import EventHandler from './dom/event-handler'
import BaseComponent from './base-component'
import SelectorEngine from './dom/selector-engine'
static jQueryInterface(config) {
return this.each(function () {
- const data = Data.get(this, DATA_KEY) || new Offcanvas(this, typeof config === 'object' ? config : {})
+ const data = Offcanvas.getOrCreateInstance(this, config)
if (typeof config !== 'string') {
return
Offcanvas.getInstance(allReadyOpen).hide()
}
- const data = Data.get(target, DATA_KEY) || new Offcanvas(target)
-
+ const data = Offcanvas.getOrCreateInstance(target)
data.toggle(this)
})
-EventHandler.on(window, EVENT_LOAD_DATA_API, () => {
- SelectorEngine.find(OPEN_SELECTOR).forEach(el => (Data.get(el, DATA_KEY) || new Offcanvas(el)).show())
-})
+EventHandler.on(window, EVENT_LOAD_DATA_API, () =>
+ SelectorEngine.find(OPEN_SELECTOR).forEach(el => Offcanvas.getOrCreateInstance(el).show())
+)
/**
* ------------------------------------------------------------------------
*/
import { defineJQueryPlugin } from './util/index'
-import Data from './dom/data'
import SelectorEngine from './dom/selector-engine'
import Tooltip from './tooltip'
static jQueryInterface(config) {
return this.each(function () {
- let data = Data.get(this, DATA_KEY)
- const _config = typeof config === 'object' ? config : null
-
- if (!data) {
- data = new Popover(this, _config)
- Data.set(this, DATA_KEY, data)
- }
+ const data = Popover.getOrCreateInstance(this, config)
if (typeof config === 'string') {
if (typeof data[config] === 'undefined') {
static jQueryInterface(config) {
return this.each(function () {
- const data = ScrollSpy.getInstance(this) || new ScrollSpy(this, typeof config === 'object' ? config : {})
+ const data = ScrollSpy.getOrCreateInstance(this, config)
if (typeof config !== 'string') {
return
isDisabled,
reflow
} from './util/index'
-import Data from './dom/data'
import EventHandler from './dom/event-handler'
import SelectorEngine from './dom/selector-engine'
import BaseComponent from './base-component'
static jQueryInterface(config) {
return this.each(function () {
- const data = Data.get(this, DATA_KEY) || new Tab(this)
+ const data = Tab.getOrCreateInstance(this)
if (typeof config === 'string') {
if (typeof data[config] === 'undefined') {
return
}
- const data = Data.get(this, DATA_KEY) || new Tab(this)
+ const data = Tab.getOrCreateInstance(this)
data.show()
})
reflow,
typeCheckConfig
} from './util/index'
-import Data from './dom/data'
import EventHandler from './dom/event-handler'
import Manipulator from './dom/manipulator'
import BaseComponent from './base-component'
static jQueryInterface(config) {
return this.each(function () {
- let data = Data.get(this, DATA_KEY)
- const _config = typeof config === 'object' && config
-
- if (!data) {
- data = new Toast(this, _config)
- }
+ const data = Toast.getOrCreateInstance(this, config)
if (typeof config === 'string') {
if (typeof data[config] === 'undefined') {
static jQueryInterface(config) {
return this.each(function () {
- let data = Data.get(this, DATA_KEY)
- const _config = typeof config === 'object' && config
-
- if (!data) {
- data = new Tooltip(this, _config)
- }
+ const data = Tooltip.getOrCreateInstance(this, config)
if (typeof config === 'string') {
if (typeof data[config] === 'undefined') {
expect(Alert.getInstance(div)).toEqual(null)
})
})
+
+ describe('getOrCreateInstance', () => {
+ it('should return alert instance', () => {
+ fixtureEl.innerHTML = '<div></div>'
+
+ const div = fixtureEl.querySelector('div')
+ const alert = new Alert(div)
+
+ expect(Alert.getOrCreateInstance(div)).toEqual(alert)
+ expect(Alert.getInstance(div)).toEqual(Alert.getOrCreateInstance(div, {}))
+ expect(Alert.getOrCreateInstance(div)).toBeInstanceOf(Alert)
+ })
+
+ it('should return new instance when there is no alert instance', () => {
+ fixtureEl.innerHTML = '<div></div>'
+
+ const div = fixtureEl.querySelector('div')
+
+ expect(Alert.getInstance(div)).toEqual(null)
+ expect(Alert.getOrCreateInstance(div)).toBeInstanceOf(Alert)
+ })
+ })
})
expect(DummyClass.getInstance(div)).toEqual(null)
})
})
+ describe('getOrCreateInstance', () => {
+ it('should return an instance', () => {
+ createInstance()
+
+ expect(DummyClass.getOrCreateInstance(element)).toEqual(instance)
+ expect(DummyClass.getInstance(element)).toEqual(DummyClass.getOrCreateInstance(element, {}))
+ expect(DummyClass.getOrCreateInstance(element)).toBeInstanceOf(DummyClass)
+ })
+
+ it('should return new instance when there is no alert instance', () => {
+ fixtureEl.innerHTML = '<div id="foo"></div>'
+ element = fixtureEl.querySelector('#foo')
+
+ expect(DummyClass.getInstance(element)).toEqual(null)
+ expect(DummyClass.getOrCreateInstance(element)).toBeInstanceOf(DummyClass)
+ })
+ })
})
})
expect(Button.getInstance(div)).toEqual(null)
})
})
+
+ describe('getOrCreateInstance', () => {
+ it('should return button instance', () => {
+ fixtureEl.innerHTML = '<div></div>'
+
+ const div = fixtureEl.querySelector('div')
+ const button = new Button(div)
+
+ expect(Button.getOrCreateInstance(div)).toEqual(button)
+ expect(Button.getInstance(div)).toEqual(Button.getOrCreateInstance(div, {}))
+ expect(Button.getOrCreateInstance(div)).toBeInstanceOf(Button)
+ })
+
+ it('should return new instance when there is no button instance', () => {
+ fixtureEl.innerHTML = '<div></div>'
+
+ const div = fixtureEl.querySelector('div')
+
+ expect(Button.getInstance(div)).toEqual(null)
+ expect(Button.getOrCreateInstance(div)).toBeInstanceOf(Button)
+ })
+ })
})
})
})
+ describe('getOrCreateInstance', () => {
+ it('should return carousel instance', () => {
+ fixtureEl.innerHTML = '<div></div>'
+
+ const div = fixtureEl.querySelector('div')
+ const carousel = new Carousel(div)
+
+ expect(Carousel.getOrCreateInstance(div)).toEqual(carousel)
+ expect(Carousel.getInstance(div)).toEqual(Carousel.getOrCreateInstance(div, {}))
+ expect(Carousel.getOrCreateInstance(div)).toBeInstanceOf(Carousel)
+ })
+
+ it('should return new instance when there is no carousel instance', () => {
+ fixtureEl.innerHTML = '<div></div>'
+
+ const div = fixtureEl.querySelector('div')
+
+ expect(Carousel.getInstance(div)).toEqual(null)
+ expect(Carousel.getOrCreateInstance(div)).toBeInstanceOf(Carousel)
+ })
+
+ it('should return new instance when there is no carousel instance with given configuration', () => {
+ fixtureEl.innerHTML = '<div></div>'
+
+ const div = fixtureEl.querySelector('div')
+
+ expect(Carousel.getInstance(div)).toEqual(null)
+ const carousel = Carousel.getOrCreateInstance(div, {
+ interval: 1
+ })
+ expect(carousel).toBeInstanceOf(Carousel)
+
+ expect(carousel._config.interval).toEqual(1)
+ })
+
+ it('should return the instance when exists without given configuration', () => {
+ fixtureEl.innerHTML = '<div></div>'
+
+ const div = fixtureEl.querySelector('div')
+ const carousel = new Carousel(div, {
+ interval: 1
+ })
+ expect(Carousel.getInstance(div)).toEqual(carousel)
+
+ const carousel2 = Carousel.getOrCreateInstance(div, {
+ interval: 2
+ })
+ expect(carousel).toBeInstanceOf(Carousel)
+ expect(carousel2).toEqual(carousel)
+
+ expect(carousel2._config.interval).toEqual(1)
+ })
+ })
+
describe('jQueryInterface', () => {
it('should create a carousel', () => {
fixtureEl.innerHTML = '<div></div>'
expect(Collapse.getInstance(div)).toEqual(null)
})
})
+
+ describe('getOrCreateInstance', () => {
+ it('should return collapse instance', () => {
+ fixtureEl.innerHTML = '<div></div>'
+
+ const div = fixtureEl.querySelector('div')
+ const collapse = new Collapse(div)
+
+ expect(Collapse.getOrCreateInstance(div)).toEqual(collapse)
+ expect(Collapse.getInstance(div)).toEqual(Collapse.getOrCreateInstance(div, {}))
+ expect(Collapse.getOrCreateInstance(div)).toBeInstanceOf(Collapse)
+ })
+
+ it('should return new instance when there is no collapse instance', () => {
+ fixtureEl.innerHTML = '<div></div>'
+
+ const div = fixtureEl.querySelector('div')
+
+ expect(Collapse.getInstance(div)).toEqual(null)
+ expect(Collapse.getOrCreateInstance(div)).toBeInstanceOf(Collapse)
+ })
+
+ it('should return new instance when there is no collapse instance with given configuration', () => {
+ fixtureEl.innerHTML = '<div></div>'
+
+ const div = fixtureEl.querySelector('div')
+
+ expect(Collapse.getInstance(div)).toEqual(null)
+ const collapse = Collapse.getOrCreateInstance(div, {
+ toggle: false
+ })
+ expect(collapse).toBeInstanceOf(Collapse)
+
+ expect(collapse._config.toggle).toEqual(false)
+ })
+
+ it('should return the instance when exists without given configuration', () => {
+ fixtureEl.innerHTML = '<div></div>'
+
+ const div = fixtureEl.querySelector('div')
+ const collapse = new Collapse(div, {
+ toggle: false
+ })
+ expect(Collapse.getInstance(div)).toEqual(collapse)
+
+ const collapse2 = Collapse.getOrCreateInstance(div, {
+ toggle: true
+ })
+ expect(collapse).toBeInstanceOf(Collapse)
+ expect(collapse2).toEqual(collapse)
+
+ expect(collapse2._config.toggle).toEqual(false)
+ })
+ })
})
})
})
+ describe('getOrCreateInstance', () => {
+ it('should return dropdown instance', () => {
+ fixtureEl.innerHTML = '<div></div>'
+
+ const div = fixtureEl.querySelector('div')
+ const dropdown = new Dropdown(div)
+
+ expect(Dropdown.getOrCreateInstance(div)).toEqual(dropdown)
+ expect(Dropdown.getInstance(div)).toEqual(Dropdown.getOrCreateInstance(div, {}))
+ expect(Dropdown.getOrCreateInstance(div)).toBeInstanceOf(Dropdown)
+ })
+
+ it('should return new instance when there is no dropdown instance', () => {
+ fixtureEl.innerHTML = '<div></div>'
+
+ const div = fixtureEl.querySelector('div')
+
+ expect(Dropdown.getInstance(div)).toEqual(null)
+ expect(Dropdown.getOrCreateInstance(div)).toBeInstanceOf(Dropdown)
+ })
+
+ it('should return new instance when there is no dropdown instance with given configuration', () => {
+ fixtureEl.innerHTML = '<div></div>'
+
+ const div = fixtureEl.querySelector('div')
+
+ expect(Dropdown.getInstance(div)).toEqual(null)
+ const dropdown = Dropdown.getOrCreateInstance(div, {
+ display: 'dynamic'
+ })
+ expect(dropdown).toBeInstanceOf(Dropdown)
+
+ expect(dropdown._config.display).toEqual('dynamic')
+ })
+
+ it('should return the instance when exists without given configuration', () => {
+ fixtureEl.innerHTML = '<div></div>'
+
+ const div = fixtureEl.querySelector('div')
+ const dropdown = new Dropdown(div, {
+ display: 'dynamic'
+ })
+ expect(Dropdown.getInstance(div)).toEqual(dropdown)
+
+ const dropdown2 = Dropdown.getOrCreateInstance(div, {
+ display: 'static'
+ })
+ expect(dropdown).toBeInstanceOf(Dropdown)
+ expect(dropdown2).toEqual(dropdown)
+
+ expect(dropdown2._config.display).toEqual('dynamic')
+ })
+ })
+
it('should open dropdown when pressing keydown or keyup', done => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
expect(Modal.getInstance(div)).toBeNull()
})
})
+
+ describe('getOrCreateInstance', () => {
+ it('should return modal instance', () => {
+ fixtureEl.innerHTML = '<div></div>'
+
+ const div = fixtureEl.querySelector('div')
+ const modal = new Modal(div)
+
+ expect(Modal.getOrCreateInstance(div)).toEqual(modal)
+ expect(Modal.getInstance(div)).toEqual(Modal.getOrCreateInstance(div, {}))
+ expect(Modal.getOrCreateInstance(div)).toBeInstanceOf(Modal)
+ })
+
+ it('should return new instance when there is no modal instance', () => {
+ fixtureEl.innerHTML = '<div></div>'
+
+ const div = fixtureEl.querySelector('div')
+
+ expect(Modal.getInstance(div)).toEqual(null)
+ expect(Modal.getOrCreateInstance(div)).toBeInstanceOf(Modal)
+ })
+
+ it('should return new instance when there is no modal instance with given configuration', () => {
+ fixtureEl.innerHTML = '<div></div>'
+
+ const div = fixtureEl.querySelector('div')
+
+ expect(Modal.getInstance(div)).toEqual(null)
+ const modal = Modal.getOrCreateInstance(div, {
+ backdrop: true
+ })
+ expect(modal).toBeInstanceOf(Modal)
+
+ expect(modal._config.backdrop).toEqual(true)
+ })
+
+ it('should return the instance when exists without given configuration', () => {
+ fixtureEl.innerHTML = '<div></div>'
+
+ const div = fixtureEl.querySelector('div')
+ const modal = new Modal(div, {
+ backdrop: true
+ })
+ expect(Modal.getInstance(div)).toEqual(modal)
+
+ const modal2 = Modal.getOrCreateInstance(div, {
+ backdrop: false
+ })
+ expect(modal).toBeInstanceOf(Modal)
+ expect(modal2).toEqual(modal)
+
+ expect(modal2._config.backdrop).toEqual(true)
+ })
+ })
})
}).toThrowError(TypeError, `No method named "${action}"`)
})
- it('should throw error on protected method', () => {
- fixtureEl.innerHTML = '<div></div>'
-
- const div = fixtureEl.querySelector('div')
- const action = '_getConfig'
-
- jQueryMock.fn.offcanvas = Offcanvas.jQueryInterface
- jQueryMock.elements = [div]
-
- try {
- jQueryMock.fn.offcanvas.call(jQueryMock, action)
- } catch (error) {
- expect(error.message).toEqual(`No method named "${action}"`)
- }
- })
-
- it('should throw error if method "constructor" is being called', () => {
- fixtureEl.innerHTML = '<div></div>'
-
- const div = fixtureEl.querySelector('div')
- const action = 'constructor'
-
- jQueryMock.fn.offcanvas = Offcanvas.jQueryInterface
- jQueryMock.elements = [div]
-
- try {
- jQueryMock.fn.offcanvas.call(jQueryMock, action)
- } catch (error) {
- expect(error.message).toEqual(`No method named "${action}"`)
- }
- })
-
it('should call offcanvas method', () => {
fixtureEl.innerHTML = '<div></div>'
jQueryMock.elements = [div]
jQueryMock.fn.offcanvas.call(jQueryMock, { scroll: true })
- spyOn(Offcanvas.prototype, 'constructor')
- expect(Offcanvas.prototype.constructor).not.toHaveBeenCalledWith(div, { scroll: true })
const offcanvas = Offcanvas.getInstance(div)
expect(offcanvas).not.toBeNull()
expect(Offcanvas.getInstance(div)).toBeNull()
})
})
+
+ describe('getOrCreateInstance', () => {
+ it('should return offcanvas instance', () => {
+ fixtureEl.innerHTML = '<div></div>'
+
+ const div = fixtureEl.querySelector('div')
+ const offcanvas = new Offcanvas(div)
+
+ expect(Offcanvas.getOrCreateInstance(div)).toEqual(offcanvas)
+ expect(Offcanvas.getInstance(div)).toEqual(Offcanvas.getOrCreateInstance(div, {}))
+ expect(Offcanvas.getOrCreateInstance(div)).toBeInstanceOf(Offcanvas)
+ })
+
+ it('should return new instance when there is no Offcanvas instance', () => {
+ fixtureEl.innerHTML = '<div></div>'
+
+ const div = fixtureEl.querySelector('div')
+
+ expect(Offcanvas.getInstance(div)).toEqual(null)
+ expect(Offcanvas.getOrCreateInstance(div)).toBeInstanceOf(Offcanvas)
+ })
+
+ it('should return new instance when there is no offcanvas instance with given configuration', () => {
+ fixtureEl.innerHTML = '<div></div>'
+
+ const div = fixtureEl.querySelector('div')
+
+ expect(Offcanvas.getInstance(div)).toEqual(null)
+ const offcanvas = Offcanvas.getOrCreateInstance(div, {
+ scroll: true
+ })
+ expect(offcanvas).toBeInstanceOf(Offcanvas)
+
+ expect(offcanvas._config.scroll).toEqual(true)
+ })
+
+ it('should return the instance when exists without given configuration', () => {
+ fixtureEl.innerHTML = '<div></div>'
+
+ const div = fixtureEl.querySelector('div')
+ const offcanvas = new Offcanvas(div, {
+ scroll: true
+ })
+ expect(Offcanvas.getInstance(div)).toEqual(offcanvas)
+
+ const offcanvas2 = Offcanvas.getOrCreateInstance(div, {
+ scroll: false
+ })
+ expect(offcanvas).toBeInstanceOf(Offcanvas)
+ expect(offcanvas2).toEqual(offcanvas)
+
+ expect(offcanvas2._config.scroll).toEqual(true)
+ })
+ })
})
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')
+ })
+ })
})
})
})
+ 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>'
})
})
+ describe('getOrCreateInstance', () => {
+ it('should return tab instance', () => {
+ fixtureEl.innerHTML = '<div></div>'
+
+ const div = fixtureEl.querySelector('div')
+ const tab = new Tab(div)
+
+ expect(Tab.getOrCreateInstance(div)).toEqual(tab)
+ expect(Tab.getInstance(div)).toEqual(Tab.getOrCreateInstance(div, {}))
+ expect(Tab.getOrCreateInstance(div)).toBeInstanceOf(Tab)
+ })
+
+ it('should return new instance when there is no tab instance', () => {
+ fixtureEl.innerHTML = '<div></div>'
+
+ const div = fixtureEl.querySelector('div')
+
+ expect(Tab.getInstance(div)).toEqual(null)
+ expect(Tab.getOrCreateInstance(div)).toBeInstanceOf(Tab)
+ })
+ })
+
describe('data-api', () => {
it('should create dynamically a tab', done => {
fixtureEl.innerHTML = [
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)
+ })
+ })
})
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
})
})
+ 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>'
Static method which allows you to get the alert instance associated to a DOM element, you can use it like this: <code>bootstrap.Alert.getInstance(alert)</code>
</td>
</tr>
+ <tr>
+ <td>
+ <code>getOrCreateInstance</code>
+ </td>
+ <td>
+ Static method which returns an alert instance associated to a DOM element or create a new one in case it wasn't initialised.
+ You can use it like this: <code>bootstrap.Alert.getOrCreateInstance(element)</code>
+ </td>
+ </tr>
</tbody>
</table>
Destroys an element's button. (Removes stored data on the DOM element)
</td>
</tr>
+ <tr>
+ <td>
+ <code>getInstance</code>
+ </td>
+ <td>
+ Static method which allows you to get the button instance associated to a DOM element, you can use it like this: <code>bootstrap.Button.getInstance(element)</code>
+ </td>
+ </tr>
+ <tr>
+ <td>
+ <code>getOrCreateInstance</code>
+ </td>
+ <td>
+ Static method which returns a button instance associated to a DOM element or create a new one in case it wasn't initialised.
+ You can use it like this: <code>bootstrap.Button.getOrCreateInstance(element)</code>
+ </td>
+ </tr>
</tbody>
</table>
<td>Destroys an element's carousel. (Removes stored data on the DOM element)</td>
</tr>
<tr>
- <td><code>getInstance</code></td>
- <td>Static method which allows you to get the carousel instance associated with a DOM element.</td>
+ <td>
+ <code>getInstance</code>
+ </td>
+ <td>
+ Static method which allows you to get the carousel instance associated to a DOM element, you can use it like this: <code>bootstrap.Carousel.getInstance(element)</code>
+ </td>
+ </tr>
+ <tr>
+ <td>
+ <code>getOrCreateInstance</code>
+ </td>
+ <td>
+ Static method which returns a carousel instance associated to a DOM element or create a new one in case it wasn't initialised.
+ You can use it like this: <code>bootstrap.Carousel.getOrCreateInstance(element)</code>
+ </td>
</tr>
</tbody>
</table>
<td>Destroys an element's collapse. (Removes stored data on the DOM element)</td>
</tr>
<tr>
- <td><code>getInstance</code></td>
- <td>Static method which allows you to get the collapse instance associated with a DOM element.</td>
+ <td>
+ <code>getInstance</code>
+ </td>
+ <td>
+ Static method which allows you to get the collapse instance associated to a DOM element, you can use it like this: <code>bootstrap.Collapse.getInstance(element)</code>
+ </td>
+ </tr>
+ <tr>
+ <td>
+ <code>getOrCreateInstance</code>
+ </td>
+ <td>
+ Static method which returns a collapse instance associated to a DOM element or create a new one in case it wasn't initialised.
+ You can use it like this: <code>bootstrap.Collapse.getOrCreateInstance(element)</code>
+ </td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
- <td><code>getInstance</code></td>
<td>
- Static method which allows you to get the dropdown instance associated with a DOM element.
+ <code>getInstance</code>
+ </td>
+ <td>
+ Static method which allows you to get the dropdown instance associated to a DOM element, you can use it like this: <code>bootstrap.Dropdown.getInstance(element)</code>
+ </td>
+ </tr>
+ <tr>
+ <td>
+ <code>getOrCreateInstance</code>
+ </td>
+ <td>
+ Static method which returns a dropdown instance associated to a DOM element or create a new one in case it wasn't initialised.
+ You can use it like this: <code>bootstrap.Dropdown.getOrCreateInstance(element)</code>
</td>
</tr>
</tbody>
var tab = bootstrap.Tab.getInstance(triggerEl) // Returns a Bootstrap tab instance
```
+#### getOrCreateInstance
+
+*Static* method which allows you to get the tab instance associated with a DOM element, or create a new one in case it wasn't initialised
+
+```js
+var triggerEl = document.querySelector('#trigger')
+var tab = bootstrap.Tab.getOrCreateInstance(triggerEl) // Returns a Bootstrap tab instance
+```
+
### Events
When showing a new tab, the events fire in the following order:
var modal = bootstrap.Modal.getInstance(myModalEl) // Returns a Bootstrap modal instance
```
+#### getOrCreateInstance
+
+*Static* method which allows you to get the modal instance associated with a DOM element, or create a new one in case it wasn't initialised
+
+```js
+var myModalEl = document.querySelector('#myModal')
+var modal = bootstrap.Modal.getOrCreateInstance(myModalEl) // Returns a Bootstrap modal instance
+```
+
### Events
Bootstrap's modal class exposes a few events for hooking into modal functionality. All modal events are fired at the modal itself (i.e. at the `<div class="modal">`).
var tab = bootstrap.Tab.getInstance(triggerEl) // Returns a Bootstrap tab instance
```
+#### getOrCreateInstance
+
+*Static* method which allows you to get the tab instance associated with a DOM element, or create a new one in case it wasn't initialised
+
+```js
+var triggerEl = document.querySelector('#trigger')
+var tab = bootstrap.Tab.getOrCreateInstance(triggerEl) // Returns a Bootstrap tab instance
+```
+
### Events
When showing a new tab, the events fire in the following order:
| `show` | Shows an offcanvas element. **Returns to the caller before the offcanvas element has actually been shown** (i.e. before the `shown.bs.offcanvas` event occurs).|
| `hide` | Hides an offcanvas element. **Returns to the caller before the offcanvas element has actually been hidden** (i.e. before the `hidden.bs.offcanvas` event occurs).|
| `getInstance` | *Static* method which allows you to get the offcanvas instance associated with a DOM element |
+| `getOrCreateInstance` | *Static* method which allows you to get the offcanvas instance associated with a DOM element, or create a new one in case it wasn't initialised |
{{< /bs-table >}}
### Events
var popover = bootstrap.Popover.getInstance(exampleTriggerEl) // Returns a Bootstrap popover instance
```
+#### getOrCreateInstance
+
+*Static* method which allows you to get the popover instance associated with a DOM element, or create a new one in case it wasn't initialised
+
+```js
+var exampleTriggerEl = document.getElementById('example')
+var popover = bootstrap.Popover.getOrCreateInstance(exampleTriggerEl) // Returns a Bootstrap popover instance
+```
+
### Events
<table class="table">
var scrollSpy = bootstrap.ScrollSpy.getInstance(scrollSpyContentEl) // Returns a Bootstrap scrollspy instance
```
+#### getOrCreateInstance
+
+*Static* method which allows you to get the scrollspy instance associated with a DOM element, or create a new one in case it wasn't initialised
+
+```js
+var scrollSpyContentEl = document.getElementById('content')
+var scrollSpy = bootstrap.ScrollSpy.getOrCreateInstance(scrollSpyContentEl) // Returns a Bootstrap scrollspy instance
+```
+
### Options
Options can be passed via data attributes or JavaScript. For data attributes, append the option name to `data-bs-`, as in `data-bs-offset=""`.
toast.dispose()
```
+#### getInstance
+
+*Static* method which allows you to get the scrollspy instance associated with a DOM element
+
+```js
+var myToastEl = document.getElementById('myToastEl')
+var myToast = bootstrap.Toast.getInstance(myToastEl) // Returns a Bootstrap toast instance
+```
+
+#### getOrCreateInstance
+
+*Static* method which allows you to get the scrollspy instance associated with a DOM element, or create a new one in case it wasn't initialised
+
+```js
+var myToastEl = document.getElementById('myToastEl')
+var myToast = bootstrap.Toast.getOrCreateInstance(myToastEl) // Returns a Bootstrap toast instance
+```
+
### Events
<table class="table">
var tooltip = bootstrap.Tooltip.getInstance(exampleTriggerEl) // Returns a Bootstrap tooltip instance
```
+#### getOrCreateInstance
+
+*Static* method which allows you to get the tooltip instance associated with a DOM element, or create a new one in case it wasn't initialised
+
+```js
+var exampleTriggerEl = document.getElementById('example')
+var tooltip = bootstrap.Tooltip.getOrCreateInstance(exampleTriggerEl) // Returns a Bootstrap tooltip instance
+```
+
### Events
<table class="table">