fix getOffset with function with test

This commit is contained in:
joke2k 2020-12-13 15:56:10 +00:00 committed by Rohit Sharma
parent 2090329d54
commit 7ab1dfea03
2 changed files with 17 additions and 13 deletions

View file

@ -290,18 +290,14 @@ class Dropdown extends BaseComponent {
}
_getOffset() {
let { offset } = this._config
if (!offset) {
return [0, 0]
}
const { offset } = this._config
if (typeof offset === 'string') {
offset = offset.split(',').map(val => Number.parseInt(val, 10))
} else if (typeof offset === 'function') {
offset = ({ popper, reference, placement }) => {
return offset({ popper, reference, placement }, this._element)
}
return offset.split(',').map(val => Number.parseInt(val, 10))
}
if (typeof offset === 'function') {
return popperData => offset(popperData, this._element)
}
return offset

View file

@ -54,7 +54,7 @@ describe('Dropdown', () => {
expect(dropdown.toggle).toHaveBeenCalled()
})
it('should create offset modifier correctly when offset option is a function', () => {
it('should create offset modifier correctly when offset option is a function', done => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
@ -64,14 +64,22 @@ describe('Dropdown', () => {
'</div>'
].join('')
const getOffset = () => [10, 20]
const getOffset = jasmine.createSpy('getOffset').and.returnValue([10, 20])
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdown = new Dropdown(btnDropdown, {
offset: getOffset
offset: getOffset,
popperConfig: {
onFirstUpdate: state => {
expect(getOffset).toHaveBeenCalledWith({ ...state.rects, placement: state.placement }, btnDropdown)
done()
}
}
})
const offset = dropdown._getOffset()
expect(typeof offset).toEqual('function')
dropdown.show()
})
it('should create offset modifier correctly when offset option is a string into data attribute', () => {