Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract components jquery interface to an outer shareable function #35489

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
move getJquery function to another file
  • Loading branch information
GeoSot committed May 12, 2022
commit 30fd460c6d1f98346afaaf453343b85d1bf138ae
2 changes: 1 addition & 1 deletion js/src/dom/event-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* --------------------------------------------------------------------------
*/

import { getjQuery } from '../util/index'
import { getjQuery } from '../util/jquery-stuff'

/**
* Constants
Expand Down
11 changes: 1 addition & 10 deletions js/src/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
* --------------------------------------------------------------------------
*/
import { getJqueryInterfaceForPlugin } from './jquery-stuff'
import { getjQuery, getJqueryInterfaceForPlugin } from './jquery-stuff'

const MAX_UID = 1_000_000
const MILLISECONDS_MULTIPLIER = 1000
Expand Down Expand Up @@ -204,14 +204,6 @@ const reflow = element => {
element.offsetHeight // eslint-disable-line no-unused-expressions
}

const getjQuery = () => {
if (window.jQuery && !document.body.hasAttribute('data-bs-no-jquery')) {
return window.jQuery
}

return null
}

const DOMContentLoadedCallbacks = []

const onDOMContentLoaded = callback => {
Expand Down Expand Up @@ -321,7 +313,6 @@ export {
findShadowRoot,
getElement,
getElementFromSelector,
getjQuery,
getNextActiveElement,
getSelectorFromElement,
getTransitionDurationFromElement,
Expand Down
11 changes: 11 additions & 0 deletions js/src/util/jquery-stuff.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@
* --------------------------------------------------------------------------
*/

const getjQuery = () => {
const { jQuery } = window

if (jQuery && !document.body.hasAttribute('data-bs-no-jquery')) {
return jQuery
}

return null
}

const defaultJQueryInterface = plugin => {
return function (config) {
return this.each(function () {
Expand All @@ -26,5 +36,6 @@ const defaultJQueryInterface = plugin => {
const getJqueryInterfaceForPlugin = plugin => plugin.jQueryInterface || defaultJQueryInterface(plugin)

export {
getjQuery,
getJqueryInterfaceForPlugin
}
33 changes: 0 additions & 33 deletions js/tests/unit/util/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -538,39 +538,6 @@ describe('Util', () => {
})
})

describe('getjQuery', () => {
const fakejQuery = { trigger() {} }

beforeEach(() => {
Object.defineProperty(window, 'jQuery', {
value: fakejQuery,
writable: true
})
})

afterEach(() => {
window.jQuery = undefined
})

it('should return jQuery object when present', () => {
expect(Util.getjQuery()).toEqual(fakejQuery)
})

it('should not return jQuery object when present if data-bs-no-jquery', () => {
document.body.setAttribute('data-bs-no-jquery', '')

expect(window.jQuery).toEqual(fakejQuery)
expect(Util.getjQuery()).toBeNull()

document.body.removeAttribute('data-bs-no-jquery')
})

it('should not return jQuery if not present', () => {
window.jQuery = undefined
expect(Util.getjQuery()).toBeNull()
})
})

describe('onDOMContentLoaded', () => {
it('should execute callbacks when DOMContentLoaded is fired and should not add more than one listener', () => {
const spy = jasmine.createSpy()
Expand Down
41 changes: 37 additions & 4 deletions js/tests/unit/util/jquery-stuff.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getJqueryInterfaceForPlugin } from '../../../src/util/jquery-stuff'
import * as jQueryUtil from '../../../src/util/jquery-stuff'

describe('Jquery Stuff', () => {
const fakejQuery = { fn: {} }
Expand All @@ -14,21 +14,54 @@ describe('Jquery Stuff', () => {
window.jQuery = undefined
})

describe('getjQuery', () => {
const fakejQuery = { trigger() {} }

beforeEach(() => {
Object.defineProperty(window, 'jQuery', {
value: fakejQuery,
writable: true
})
})

afterEach(() => {
window.jQuery = undefined
})

it('should return jQuery object when present', () => {
expect(jQueryUtil.getjQuery()).toEqual(fakejQuery)
})

it('should not return jQuery object when present if data-bs-no-jquery', () => {
document.body.setAttribute('data-bs-no-jquery', '')

expect(window.jQuery).toEqual(fakejQuery)
expect(jQueryUtil.getjQuery()).toBeNull()

document.body.removeAttribute('data-bs-no-jquery')
})

it('should not return jQuery if not present', () => {
window.jQuery = undefined
expect(jQueryUtil.getjQuery()).toBeNull()
})
})

describe('getJqueryInterfaceForPlugin', () => {
it('should return a plugin jQueryInterface if exists', () => {
const pluginMock = function () {}
pluginMock.NAME = 'test'
pluginMock.jQueryInterface = function () {}

expect(getJqueryInterfaceForPlugin(pluginMock)).toEqual(pluginMock.jQueryInterface)
expect(jQueryUtil.getJqueryInterfaceForPlugin(pluginMock)).toEqual(pluginMock.jQueryInterface)
})

it('should return the default `defaultJQueryInterface`, if plugin jQueryInterface doesn\'t exists', () => {
const pluginMock = function () {}
pluginMock.NAME = 'test'

expect(getJqueryInterfaceForPlugin(pluginMock)).not.toEqual(pluginMock.jQueryInterface)
expect(getJqueryInterfaceForPlugin(pluginMock)).toEqual(jasmine.any(Function))
expect(jQueryUtil.getJqueryInterfaceForPlugin(pluginMock)).not.toEqual(pluginMock.jQueryInterface)
expect(jQueryUtil.getJqueryInterfaceForPlugin(pluginMock)).toEqual(jasmine.any(Function))
})
})
})