Skip to content

Commit 16d80a4

Browse files
authored
Fix this reference for JavaScript functions (#38725)
1 parent 74891cb commit 16d80a4

File tree

8 files changed

+101
-12
lines changed

8 files changed

+101
-12
lines changed

js/src/dropdown.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ class Dropdown extends BaseComponent {
320320

321321
return {
322322
...defaultBsPopperConfig,
323-
...execute(this._config.popperConfig, [defaultBsPopperConfig])
323+
...execute(this._config.popperConfig, [undefined, defaultBsPopperConfig])
324324
}
325325
}
326326

js/src/tooltip.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ class Tooltip extends BaseComponent {
392392
}
393393

394394
_resolvePossibleFunction(arg) {
395-
return execute(arg, [this._element])
395+
return execute(arg, [this._element, this._element])
396396
}
397397

398398
_getPopperConfig(attachment) {
@@ -438,7 +438,7 @@ class Tooltip extends BaseComponent {
438438

439439
return {
440440
...defaultBsPopperConfig,
441-
...execute(this._config.popperConfig, [defaultBsPopperConfig])
441+
...execute(this._config.popperConfig, [undefined, defaultBsPopperConfig])
442442
}
443443
}
444444

js/src/util/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ const defineJQueryPlugin = plugin => {
223223
}
224224

225225
const execute = (possibleCallback, args = [], defaultValue = possibleCallback) => {
226-
return typeof possibleCallback === 'function' ? possibleCallback(...args) : defaultValue
226+
return typeof possibleCallback === 'function' ? possibleCallback.call(...args) : defaultValue
227227
}
228228

229229
const executeAfterTransition = (callback, transitionElement, waitForTransition = true) => {

js/src/util/template-factory.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ class TemplateFactory extends Config {
143143
}
144144

145145
_resolvePossibleFunction(arg) {
146-
return execute(arg, [this])
146+
return execute(arg, [undefined, this])
147147
}
148148

149149
_putElementInTemplate(element, templateElement) {

js/tests/unit/dropdown.spec.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,10 @@ describe('Dropdown', () => {
172172

173173
const popperConfig = dropdown._getPopperConfig()
174174

175-
expect(getPopperConfig).toHaveBeenCalled()
175+
// Ensure that the function was called with the default config.
176+
expect(getPopperConfig).toHaveBeenCalledWith(jasmine.objectContaining({
177+
placement: jasmine.any(String)
178+
}))
176179
expect(popperConfig.placement).toEqual('left')
177180
})
178181
})

js/tests/unit/popover.spec.js

+54
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,60 @@ describe('Popover', () => {
9595
})
9696
})
9797

98+
it('should call content and title functions with trigger element', () => {
99+
return new Promise(resolve => {
100+
fixtureEl.innerHTML = '<a href="#" data-foo="bar">BS twitter</a>'
101+
102+
const popoverEl = fixtureEl.querySelector('a')
103+
const popover = new Popover(popoverEl, {
104+
title(el) {
105+
return el.dataset.foo
106+
},
107+
content(el) {
108+
return el.dataset.foo
109+
}
110+
})
111+
112+
popoverEl.addEventListener('shown.bs.popover', () => {
113+
const popoverDisplayed = document.querySelector('.popover')
114+
115+
expect(popoverDisplayed).not.toBeNull()
116+
expect(popoverDisplayed.querySelector('.popover-header').textContent).toEqual('bar')
117+
expect(popoverDisplayed.querySelector('.popover-body').textContent).toEqual('bar')
118+
resolve()
119+
})
120+
121+
popover.show()
122+
})
123+
})
124+
125+
it('should call content and title functions with correct this value', () => {
126+
return new Promise(resolve => {
127+
fixtureEl.innerHTML = '<a href="#" data-foo="bar">BS twitter</a>'
128+
129+
const popoverEl = fixtureEl.querySelector('a')
130+
const popover = new Popover(popoverEl, {
131+
title() {
132+
return this.dataset.foo
133+
},
134+
content() {
135+
return this.dataset.foo
136+
}
137+
})
138+
139+
popoverEl.addEventListener('shown.bs.popover', () => {
140+
const popoverDisplayed = document.querySelector('.popover')
141+
142+
expect(popoverDisplayed).not.toBeNull()
143+
expect(popoverDisplayed.querySelector('.popover-header').textContent).toEqual('bar')
144+
expect(popoverDisplayed.querySelector('.popover-body').textContent).toEqual('bar')
145+
resolve()
146+
})
147+
148+
popover.show()
149+
})
150+
})
151+
98152
it('should show a popover with just content without having header', () => {
99153
return new Promise(resolve => {
100154
fixtureEl.innerHTML = '<a href="#">Nice link</a>'

js/tests/unit/tooltip.spec.js

+36-4
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,10 @@ describe('Tooltip', () => {
177177

178178
const popperConfig = tooltip._getPopperConfig('top')
179179

180-
expect(getPopperConfig).toHaveBeenCalled()
180+
// Ensure that the function was called with the default config.
181+
expect(getPopperConfig).toHaveBeenCalledWith(jasmine.objectContaining({
182+
placement: jasmine.any(String)
183+
}))
181184
expect(popperConfig.placement).toEqual('left')
182185
})
183186

@@ -919,10 +922,12 @@ describe('Tooltip', () => {
919922

920923
it('should show a tooltip with custom class provided as a function in config', () => {
921924
return new Promise(resolve => {
922-
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip"></a>'
925+
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip" data-class-a="custom-class-a" data-class-b="custom-class-b"></a>'
923926

924-
const spy = jasmine.createSpy('customClass').and.returnValue('custom-class')
925927
const tooltipEl = fixtureEl.querySelector('a')
928+
const spy = jasmine.createSpy('customClass').and.callFake(function (el) {
929+
return `${el.dataset.classA} ${this.dataset.classB}`
930+
})
926931
const tooltip = new Tooltip(tooltipEl, {
927932
customClass: spy
928933
})
@@ -931,7 +936,8 @@ describe('Tooltip', () => {
931936
const tip = document.querySelector('.tooltip')
932937
expect(tip).not.toBeNull()
933938
expect(spy).toHaveBeenCalled()
934-
expect(tip).toHaveClass('custom-class')
939+
expect(tip).toHaveClass('custom-class-a')
940+
expect(tip).toHaveClass('custom-class-b')
935941
resolve()
936942
})
937943

@@ -1337,6 +1343,32 @@ describe('Tooltip', () => {
13371343

13381344
expect(tooltip._getTitle()).toEqual('test')
13391345
})
1346+
1347+
it('should call title function with trigger element', () => {
1348+
fixtureEl.innerHTML = '<a href="#" rel="tooltip" data-foo="bar"></a>'
1349+
1350+
const tooltipEl = fixtureEl.querySelector('a')
1351+
const tooltip = new Tooltip(tooltipEl, {
1352+
title(el) {
1353+
return el.dataset.foo
1354+
}
1355+
})
1356+
1357+
expect(tooltip._getTitle()).toEqual('bar')
1358+
})
1359+
1360+
it('should call title function with correct this value', () => {
1361+
fixtureEl.innerHTML = '<a href="#" rel="tooltip" data-foo="bar"></a>'
1362+
1363+
const tooltipEl = fixtureEl.querySelector('a')
1364+
const tooltip = new Tooltip(tooltipEl, {
1365+
title() {
1366+
return this.dataset.foo
1367+
}
1368+
})
1369+
1370+
expect(tooltip._getTitle()).toEqual('bar')
1371+
})
13401372
})
13411373

13421374
describe('getInstance', () => {

js/tests/unit/util/index.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -521,10 +521,10 @@ describe('Util', () => {
521521

522522
it('should execute if arg is function & return the result', () => {
523523
const functionFoo = (num1, num2 = 10) => num1 + num2
524-
const resultFoo = Util.execute(functionFoo, [4, 5])
524+
const resultFoo = Util.execute(functionFoo, [undefined, 4, 5])
525525
expect(resultFoo).toBe(9)
526526

527-
const resultFoo1 = Util.execute(functionFoo, [4])
527+
const resultFoo1 = Util.execute(functionFoo, [undefined, 4])
528528
expect(resultFoo1).toBe(14)
529529

530530
const functionBar = () => 'foo'

0 commit comments

Comments
 (0)