Skip to content
Permalink
Browse files

Switch from QUnit to Jasmine.

  • Loading branch information
Johann-S committed Mar 13, 2019
1 parent 08d81c8 commit c8c207465043d940aa031570f0bce5e8fff9ffcf
@@ -11,10 +11,5 @@ module.exports = {
],
plugins: [
'@babel/plugin-proposal-object-rest-spread'
],
env: {
test: {
plugins: [ 'istanbul' ]
}
}
]
};
@@ -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
@@ -33,7 +32,7 @@ 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'),
Alert: path.resolve(__dirname, '../js/src/alert/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'),
@@ -45,12 +44,7 @@ const bsPlugins = {
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')
}
const rootPath = '../js/dist/'

const defaultPluginConfig = {
external: [
@@ -5,7 +5,7 @@
* --------------------------------------------------------------------------
*/

import Alert from './src/alert'
import Alert from './src/alert/alert'
import Button from './src/button'
import Carousel from './src/carousel'
import Collapse from './src/collapse'
@@ -5,7 +5,7 @@
* --------------------------------------------------------------------------
*/

import Alert from './src/alert'
import Alert from './src/alert/alert'
import Button from './src/button'
import Carousel from './src/carousel'
import Collapse from './src/collapse'
@@ -0,0 +1,14 @@
{
"root": true,
"extends": [
"../../.eslintrc.json"
],
"overrides": [
{
"files": ["**/*.spec.js"],
"env": {
"jasmine": true
}
}
]
}
@@ -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'

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

if (this._element) {
Data.setData(element, DATA_KEY, this)
}
@@ -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)
}

@@ -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
@@ -0,0 +1,127 @@
import Alert from './alert'
import { makeArray, getTransitionDurationFromElement } from '../util/index'

/** Test helpers */
import { getFixture, clearFixture } 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()
})
})
})
@@ -0,0 +1,131 @@
import Data from './data'

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

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

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

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

describe('setData', () => {
it('should set data in an element by adding a key attribute', () => {
fixtureEl.innerHTML = '<div></div>'

const div = fixtureEl.querySelector('div')
const data = {
test: 'bsData'
}

Data.setData(div, 'test', data)
expect(div.key).toBeDefined()
})

it('should change data if something is already stored', () => {
fixtureEl.innerHTML = '<div></div>'

const div = fixtureEl.querySelector('div')
const data = {
test: 'bsData'
}

Data.setData(div, 'test', data)

data.test = 'bsData2'
Data.setData(div, 'test', data)

expect(div.key).toBeDefined()
})
})

describe('getData', () => {
it('should return stored data', () => {
fixtureEl.innerHTML = '<div></div>'

const div = fixtureEl.querySelector('div')
const data = {
test: 'bsData'
}

Data.setData(div, 'test', data)
expect(Data.getData(div, 'test')).toEqual(data)
})

it('should return null on undefined element', () => {
expect(Data.getData(null)).toEqual(null)
expect(Data.getData(undefined)).toEqual(null)
})

it('should return null when an element have nothing stored', () => {
fixtureEl.innerHTML = '<div></div>'

const div = fixtureEl.querySelector('div')

expect(Data.getData(div, 'test')).toEqual(null)
})

it('should return null when an element have nothing stored with the provided key', () => {
fixtureEl.innerHTML = '<div></div>'

const div = fixtureEl.querySelector('div')
const data = {
test: 'bsData'
}

Data.setData(div, 'test', data)

expect(Data.getData(div, 'test2')).toEqual(null)
})
})

describe('removeData', () => {
it('should do nothing when an element have nothing stored', () => {
fixtureEl.innerHTML = '<div></div>'

const div = fixtureEl.querySelector('div')

Data.removeData(div, 'test')
expect().nothing()
})

it('should should do nothing if it\'s not a valid key provided', () => {
fixtureEl.innerHTML = '<div></div>'

const div = fixtureEl.querySelector('div')
const data = {
test: 'bsData'
}

Data.setData(div, 'test', data)

expect(div.key).toBeDefined()

Data.removeData(div, 'test2')

expect(div.key).toBeDefined()
})

it('should remove data if something is stored', () => {
fixtureEl.innerHTML = '<div></div>'

const div = fixtureEl.querySelector('div')
const data = {
test: 'bsData'
}

Data.setData(div, 'test', data)

expect(div.key).toBeDefined()

Data.removeData(div, 'test')

expect(div.key).toBeUndefined()
})
})
})
@@ -192,7 +192,9 @@ function addHandler(element, originalTypeEvent, handler, delegationFn, oneOff) {
}

const uid = getUidEvent(originalHandler, originalTypeEvent.replace(namespaceRegex, ''))
const fn = delegation ? bootstrapDelegationHandler(element, handler, delegationFn) : bootstrapHandler(element, handler)
const fn = delegation ?
bootstrapDelegationHandler(element, handler, delegationFn) :
bootstrapHandler(element, handler)

fn.delegationSelector = delegation ? handler : null
fn.originalHandler = originalHandler

0 comments on commit c8c2074

Please sign in to comment.
You can’t perform that action at this time.