mirror of
https://github.com/twbs/bootstrap.git
synced 2025-09-25 12:59:05 +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:
@@ -1991,6 +1991,60 @@ describe('Dropdown', () => {
|
||||
})
|
||||
})
|
||||
|
||||
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">',
|
||||
|
Reference in New Issue
Block a user