Skip to content

Commit

Permalink
Accept argument of different types in the getInstance method (#34333)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyvii authored Jun 29, 2021
1 parent b00355d commit d314466
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion js/src/base-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class BaseComponent {
/** Static */

static getInstance(element) {
return Data.get(element, this.DATA_KEY)
return Data.get(getElement(element), this.DATA_KEY)
}

static getOrCreateInstance(element, config = {}) {
Expand Down
14 changes: 14 additions & 0 deletions js/tests/unit/base-component.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,20 @@ describe('Base Component', () => {
expect(DummyClass.getInstance(element)).toBeInstanceOf(DummyClass)
})

it('should accept element, either passed as a CSS selector, jQuery element, or DOM element', () => {
createInstance()

expect(DummyClass.getInstance('#foo')).toEqual(instance)
expect(DummyClass.getInstance(element)).toEqual(instance)

const fakejQueryObject = {
0: element,
jquery: 'foo'
}

expect(DummyClass.getInstance(fakejQueryObject)).toEqual(instance)
})

it('should return null when there is no instance', () => {
fixtureEl.innerHTML = '<div></div>'

Expand Down

0 comments on commit d314466

Please sign in to comment.