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

Switch from qunit to jasmine #28518

Merged
merged 17 commits into from
Jul 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 1 addition & 6 deletions .babelrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,5 @@ module.exports = {
],
plugins: [
'@babel/plugin-proposal-object-rest-spread'
],
env: {
test: {
plugins: [ 'istanbul' ]
}
}
]
};
30 changes: 12 additions & 18 deletions build/build-plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const rollup = require('rollup')
const babel = require('rollup-plugin-babel')
const banner = require('./banner.js')

const TEST = process.env.NODE_ENV === 'test'
const plugins = [
babel({
// Only transpile our source code
Expand All @@ -33,24 +32,19 @@ const bsPlugins = {
Manipulator: path.resolve(__dirname, '../js/src/dom/manipulator.js'),
Polyfill: path.resolve(__dirname, '../js/src/dom/polyfill.js'),
SelectorEngine: path.resolve(__dirname, '../js/src/dom/selector-engine.js'),
Alert: path.resolve(__dirname, '../js/src/alert.js'),
Button: path.resolve(__dirname, '../js/src/button.js'),
Carousel: path.resolve(__dirname, '../js/src/carousel.js'),
Collapse: path.resolve(__dirname, '../js/src/collapse.js'),
Dropdown: path.resolve(__dirname, '../js/src/dropdown.js'),
Modal: path.resolve(__dirname, '../js/src/modal.js'),
Popover: path.resolve(__dirname, '../js/src/popover.js'),
ScrollSpy: path.resolve(__dirname, '../js/src/scrollspy.js'),
Tab: path.resolve(__dirname, '../js/src/tab.js'),
Toast: path.resolve(__dirname, '../js/src/toast.js'),
Tooltip: path.resolve(__dirname, '../js/src/tooltip.js')
}
const rootPath = TEST ? '../js/coverage/dist/' : '../js/dist/'

if (TEST) {
bsPlugins.Util = path.resolve(__dirname, '../js/src/util/index.js')
bsPlugins.Sanitizer = path.resolve(__dirname, '../js/src/util/sanitizer.js')
Alert: path.resolve(__dirname, '../js/src/alert/alert.js'),
Button: path.resolve(__dirname, '../js/src/button/button.js'),
Carousel: path.resolve(__dirname, '../js/src/carousel/carousel.js'),
Collapse: path.resolve(__dirname, '../js/src/collapse/collapse.js'),
Dropdown: path.resolve(__dirname, '../js/src/dropdown/dropdown.js'),
Modal: path.resolve(__dirname, '../js/src/modal/modal.js'),
Popover: path.resolve(__dirname, '../js/src/popover/popover.js'),
ScrollSpy: path.resolve(__dirname, '../js/src/scrollspy/scrollspy.js'),
Tab: path.resolve(__dirname, '../js/src/tab/tab.js'),
Toast: path.resolve(__dirname, '../js/src/toast/toast.js'),
Tooltip: path.resolve(__dirname, '../js/src/tooltip/tooltip.js')
}
const rootPath = '../js/dist/'

const defaultPluginConfig = {
external: [
Expand Down
22 changes: 11 additions & 11 deletions js/index.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
* --------------------------------------------------------------------------
*/

import Alert from './src/alert'
import Button from './src/button'
import Carousel from './src/carousel'
import Collapse from './src/collapse'
import Dropdown from './src/dropdown'
import Modal from './src/modal'
import Popover from './src/popover'
import ScrollSpy from './src/scrollspy'
import Tab from './src/tab'
import Toast from './src/toast'
import Tooltip from './src/tooltip'
import Alert from './src/alert/alert'
import Button from './src/button/button'
import Carousel from './src/carousel/carousel'
import Collapse from './src/collapse/collapse'
import Dropdown from './src/dropdown/dropdown'
import Modal from './src/modal/modal'
import Popover from './src/popover/popover'
import ScrollSpy from './src/scrollspy/scrollspy'
import Tab from './src/tab/tab'
import Toast from './src/toast/toast'
import Tooltip from './src/tooltip/tooltip'

export {
Alert,
Expand Down
22 changes: 11 additions & 11 deletions js/index.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
* --------------------------------------------------------------------------
*/

import Alert from './src/alert'
import Button from './src/button'
import Carousel from './src/carousel'
import Collapse from './src/collapse'
import Dropdown from './src/dropdown'
import Modal from './src/modal'
import Popover from './src/popover'
import ScrollSpy from './src/scrollspy'
import Tab from './src/tab'
import Toast from './src/toast'
import Tooltip from './src/tooltip'
import Alert from './src/alert/alert'
import Button from './src/button/button'
import Carousel from './src/carousel/carousel'
import Collapse from './src/collapse/collapse'
import Dropdown from './src/dropdown/dropdown'
import Modal from './src/modal/modal'
import Popover from './src/popover/popover'
import ScrollSpy from './src/scrollspy/scrollspy'
import Tab from './src/tab/tab'
import Toast from './src/toast/toast'
import Tooltip from './src/tooltip/tooltip'

export default {
Alert,
Expand Down
14 changes: 14 additions & 0 deletions js/src/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"root": true,
"extends": [
"../../.eslintrc.json"
],
"overrides": [
{
"files": ["**/*.spec.js"],
"env": {
"jasmine": true
}
}
]
}
12 changes: 7 additions & 5 deletions js/src/alert.js → js/src/alert/alert.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import {
emulateTransitionEnd,
getSelectorFromElement,
getTransitionDurationFromElement
} from './util/index'
import Data from './dom/data'
import EventHandler from './dom/event-handler'
import SelectorEngine from './dom/selector-engine'
} from '../util/index'
import Data from '../dom/data'
import EventHandler from '../dom/event-handler'
import SelectorEngine from '../dom/selector-engine'

/**
* ------------------------------------------------------------------------
Expand Down Expand Up @@ -53,6 +53,7 @@ const ClassName = {
class Alert {
constructor(element) {
this._element = element

if (this._element) {
Data.setData(element, DATA_KEY, this)
}
Expand Down Expand Up @@ -118,7 +119,7 @@ class Alert {
const transitionDuration = getTransitionDurationFromElement(element)

EventHandler
.one(element, TRANSITION_END, event => this._destroyElement(element, event))
.one(element, TRANSITION_END, () => this._destroyElement(element))
emulateTransitionEnd(element, transitionDuration)
}

Expand Down Expand Up @@ -176,6 +177,7 @@ EventHandler
* add .alert to jQuery only if jQuery is present
*/

/* istanbul ignore if */
if (typeof $ !== 'undefined') {
const JQUERY_NO_CONFLICT = $.fn[NAME]
$.fn[NAME] = Alert._jQueryInterface
Expand Down
173 changes: 173 additions & 0 deletions js/src/alert/alert.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
import Alert from './alert'
import { makeArray, getTransitionDurationFromElement } from '../util/index'

/** Test helpers */
import { getFixture, clearFixture, jQueryMock } from '../../tests/helpers/fixture'

describe('Alert', () => {
let fixtureEl

beforeAll(() => {
fixtureEl = getFixture()
})

afterEach(() => {
clearFixture()
})

it('should return version', () => {
expect(typeof Alert.VERSION).toEqual('string')
})

describe('data-api', () => {
it('should close an alert without instanciate it manually', () => {
fixtureEl.innerHTML = [
'<div class="alert">',
' <button type="button" data-dismiss="alert">x</button>',
'</div>'
].join('')

const button = document.querySelector('button')

button.click()
expect(makeArray(document.querySelectorAll('.alert')).length).toEqual(0)
})

it('should close an alert without instanciate it manually with the parent selector', () => {
fixtureEl.innerHTML = [
'<div class="alert">',
' <button type="button" data-target=".alert" data-dismiss="alert">x</button>',
'</div>'
].join('')

const button = document.querySelector('button')

button.click()
expect(makeArray(document.querySelectorAll('.alert')).length).toEqual(0)
})
})

describe('close', () => {
it('should close an alert', done => {
const spy = jasmine.createSpy('spy', getTransitionDurationFromElement)
fixtureEl.innerHTML = '<div class="alert"></div>'

const alertEl = document.querySelector('.alert')
const alert = new Alert(alertEl)

alertEl.addEventListener('closed.bs.alert', () => {
expect(makeArray(document.querySelectorAll('.alert')).length).toEqual(0)
expect(spy).not.toHaveBeenCalled()
done()
})

alert.close()
})

it('should close alert with fade class', done => {
fixtureEl.innerHTML = '<div class="alert fade"></div>'

const alertEl = document.querySelector('.alert')
const alert = new Alert(alertEl)

alertEl.addEventListener('transitionend', () => {
expect().nothing()
})

alertEl.addEventListener('closed.bs.alert', () => {
expect(makeArray(document.querySelectorAll('.alert')).length).toEqual(0)
done()
})

alert.close()
})

it('should not remove alert if close event is prevented', done => {
fixtureEl.innerHTML = '<div class="alert"></div>'

const alertEl = document.querySelector('.alert')
const alert = new Alert(alertEl)

const endTest = () => {
setTimeout(() => {
expect(alert._removeElement).not.toHaveBeenCalled()
done()
}, 10)
}

spyOn(alert, '_removeElement')

alertEl.addEventListener('close.bs.alert', event => {
event.preventDefault()
endTest()
})

alertEl.addEventListener('closed.bs.alert', () => {
endTest()
})

alert.close()
})
})

describe('dispose', () => {
it('should dispose an alert', () => {
fixtureEl.innerHTML = '<div class="alert"></div>'

const alertEl = document.querySelector('.alert')
const alert = new Alert(alertEl)

expect(Alert._getInstance(alertEl)).toBeDefined()

alert.dispose()

expect(Alert._getInstance(alertEl)).toBeNull()
})
})

describe('_jQueryInterface', () => {
it('should handle config passed and toggle existing alert', () => {
fixtureEl.innerHTML = '<div class="alert"></div>'

const alertEl = fixtureEl.querySelector('.alert')
const alert = new Alert(alertEl)

spyOn(alert, 'close')

jQueryMock.fn.alert = Alert._jQueryInterface
jQueryMock.elements = [alertEl]

jQueryMock.fn.alert.call(jQueryMock, 'close')

expect(alert.close).toHaveBeenCalled()
})

it('should create new alert instance and call close', () => {
fixtureEl.innerHTML = '<div class="alert"></div>'

const alertEl = fixtureEl.querySelector('.alert')

jQueryMock.fn.alert = Alert._jQueryInterface
jQueryMock.elements = [alertEl]

jQueryMock.fn.alert.call(jQueryMock, 'close')

expect(Alert._getInstance(alertEl)).toBeDefined()
expect(fixtureEl.querySelector('.alert')).toBeNull()
})

it('should just create an alert instance without calling close', () => {
fixtureEl.innerHTML = '<div class="alert"></div>'

const alertEl = fixtureEl.querySelector('.alert')

jQueryMock.fn.alert = Alert._jQueryInterface
jQueryMock.elements = [alertEl]

jQueryMock.fn.alert.call(jQueryMock)

expect(Alert._getInstance(alertEl)).toBeDefined()
expect(fixtureEl.querySelector('.alert')).not.toBeNull()
})
})
})
11 changes: 5 additions & 6 deletions js/src/button.js → js/src/button/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
* --------------------------------------------------------------------------
*/

import { jQuery as $ } from './util/index'
import Data from './dom/data'
import EventHandler from './dom/event-handler'
import SelectorEngine from './dom/selector-engine'
import { jQuery as $ } from '../util/index'
import Data from '../dom/data'
import EventHandler from '../dom/event-handler'
import SelectorEngine from '../dom/selector-engine'

/**
* ------------------------------------------------------------------------
Expand Down Expand Up @@ -158,7 +158,6 @@ EventHandler.on(document, Event.CLICK_DATA_API, Selector.DATA_TOGGLE_CARROT, eve
let data = Data.getData(button, DATA_KEY)
if (!data) {
data = new Button(button)
Data.setData(button, DATA_KEY, data)
}

data.toggle()
Expand Down Expand Up @@ -186,7 +185,7 @@ EventHandler.on(document, Event.BLUR_DATA_API, Selector.DATA_TOGGLE_CARROT, even
* ------------------------------------------------------------------------
* add .button to jQuery only if jQuery is present
*/

/* istanbul ignore if */
if (typeof $ !== 'undefined') {
const JQUERY_NO_CONFLICT = $.fn[NAME]
$.fn[NAME] = Button._jQueryInterface
Expand Down
Loading