Skip to content

Commit

Permalink
move from qunit to jasmine
Browse files Browse the repository at this point in the history
  • Loading branch information
Johann-S committed Mar 18, 2019
1 parent b0f9189 commit 034e303
Show file tree
Hide file tree
Showing 17 changed files with 691 additions and 389 deletions.
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' ]
}
}
]
};
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ Thumbs.db
*.komodoproject

# Folders to ignore
/js/coverage/
/coverage/
/node_modules/
10 changes: 2 additions & 8 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 @@ -32,7 +31,7 @@ const bsPlugins = {
EventHandler: path.resolve(__dirname, '../js/src/dom/eventHandler.js'),
Manipulator: path.resolve(__dirname, '../js/src/dom/manipulator.js'),
SelectorEngine: path.resolve(__dirname, '../js/src/dom/selectorEngine.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'),
Expand All @@ -44,12 +43,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: [
Expand Down
2 changes: 1 addition & 1 deletion js/index.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion js/index.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
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/eventHandler'
import SelectorEngine from './dom/selectorEngine'
} from '../util/index'
import Data from '../dom/data'
import EventHandler from '../dom/eventHandler'
import SelectorEngine from '../dom/selectorEngine'

/**
* ------------------------------------------------------------------------
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
127 changes: 127 additions & 0 deletions js/src/alert/alert.spec.js
Original file line number Diff line number Diff line change
@@ -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()
})
})
})
Loading

0 comments on commit 034e303

Please sign in to comment.