Skip to content

Commit faf936b

Browse files
author
Emanuele Marchi
committed
Fix tests
1 parent a8d7fe4 commit faf936b

File tree

6 files changed

+1480
-135
lines changed

6 files changed

+1480
-135
lines changed

.eslintrc.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"extends": "airbnb-base",
33
"parser": "babel-eslint",
4-
"env": { "browser": true }
4+
"env": {
5+
"browser": true,
6+
"jest": true
7+
}
58
}

package.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
"readmeFilename": "README.md",
2929
"devDependencies": {
3030
"babel-cli": "^6.22.2",
31+
"babel-core": "^6.26.0",
3132
"babel-eslint": "^7.2.3",
33+
"babel-jest": "^22.4.3",
3234
"babel-polyfill": "^6.22.0",
3335
"babel-preset-babili": "^0.0.10",
3436
"babel-preset-env": "^1.1.8",
@@ -41,6 +43,7 @@
4143
"headr": "0.0.4",
4244
"jasmine": "^2.5.3",
4345
"jasmine-core": "^2.5.2",
46+
"jest": "^22.4.3",
4447
"jquery": "^1.12.4",
4548
"karma": "^1.4.1",
4649
"karma-babel-preprocessor": "^6.0.1",
@@ -51,11 +54,12 @@
5154
"less": "^2.7.2",
5255
"less-plugin-clean-css": "^1.5.0",
5356
"npm-run-all": "^4.0.1",
57+
"regenerator-runtime": "^0.11.1",
5458
"wintersmith": "^2.3.6"
5559
},
5660
"scripts": {
5761
"js:lint": "eslint src/js/",
58-
"js:test": "mkdir -p test && babel -d test src/js && karma start karma.conf.js",
62+
"js:test": "jest",
5963
"js:build:dir": "mkdir -p dist/js",
6064
"js:build:base": "babel -o dist/js/bootstrap-switch.js src/js/bootstrap-switch.js",
6165
"js:build:min": "NODE_ENV=production babel -o dist/js/bootstrap-switch.min.js src/js/bootstrap-switch.js",
@@ -88,5 +92,10 @@
8892
]
8993
}
9094
],
91-
"dependencies": {}
95+
"dependencies": {},
96+
"jest": {
97+
"setupFiles": [
98+
"./src/setup-test.js"
99+
]
100+
}
92101
}

src/js/bootstrap-switch.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,8 @@ class BootstrapSwitch {
304304

305305
this.$element.on('init.bootstrapSwitch', () => this.options.onInit(element));
306306
this.$element.on('switchChange.bootstrapSwitch', (...args) => {
307-
if (this.options.onSwitchChange.apply(element, args) === false) {
307+
const changeState = this.options.onSwitchChange.apply(element, args);
308+
if (changeState === false) {
308309
if (this.$element.is(':radio')) {
309310
$(`[name="${this.$element.attr('name')}"]`).trigger('previousState.bootstrapSwitch', true);
310311
} else {

src/js/bootstrap-switch.test.js

Lines changed: 64 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,31 @@
1-
const { $, describe, beforeEach, afterEach, it, expect } = window;
1+
import './bootstrap-switch';
2+
3+
const { $ } = global;
24

35
describe('Bootstrap Switch:', () => {
46
beforeEach(() => {
57
$.support.transition = false;
68
$.fx.off = true;
79
});
10+
811
afterEach(() => {
912
$(`.${$.fn.bootstrapSwitch.defaults.baseClass}`).bootstrapSwitch('destroy');
1013
});
1114

12-
function createCheckbox() {
13-
return $('<input>', {
15+
const createCheckbox = () =>
16+
$('<input>', {
1417
type: 'checkbox',
1518
class: 'switch',
1619
}).appendTo('body');
17-
}
1820

19-
function createRadio() {
20-
return $('<input>', {
21+
const createRadio = () =>
22+
$('<input>', {
2123
type: 'radio',
2224
name: 'name',
2325
class: 'switch',
2426
}).appendTo('body');
25-
}
2627

27-
function getOptions($element) {
28-
return $element.data('bootstrap-switch').options;
29-
}
28+
const getOptions = $element => $element.data('bootstrap-switch').options;
3029

3130
it('should set the default options as element options, except state', () => {
3231
const $switch = createCheckbox().prop('checked', true).bootstrapSwitch();
@@ -40,78 +39,95 @@ describe('Bootstrap Switch:', () => {
4039
expect(getOptions($switch2).state).toBe(false);
4140
});
4241

43-
it('should something', () => {
42+
it('should trigger the same events on element and document', () => {
4443
const $switch = createCheckbox().bootstrapSwitch();
45-
let eventDoc = 0;
46-
let eventElement = 0;
47-
$(document).on('switchChange.bootstrapSwitch', ':checkbox', () => { eventDoc += 1; });
48-
$(':checkbox').on('switchChange.bootstrapSwitch', () => { eventElement += 1; });
49-
$switch.click();
50-
expect(eventElement).toEqual(eventDoc);
51-
expect(eventElement).toEqual(1);
44+
let doc = 0;
45+
let element = 0;
46+
$(document).on('switchChange.bootstrapSwitch', ':checkbox', () => { doc += 1; });
47+
$switch.on('switchChange.bootstrapSwitch', () => { element += 1; });
48+
$switch.bootstrapSwitch('state', true);
49+
expect(element).toBe(doc);
50+
expect(element).toBe(1);
5251
});
5352

54-
describe('The Checkbox Bootstrap Switch', () => {
55-
it('should conserve its state if onSwitchChange returns false', () => {
53+
describe('Checkbox', () => {
54+
it('should retain state if `onSwitchChange` returns false', () => {
55+
let shadowState = null;
5656
const $switch = createCheckbox().bootstrapSwitch({
57+
state: false,
5758
onSwitchChange(event, state) {
58-
expect(state).toEqual(true);
59+
shadowState = state;
5960
return false;
6061
},
6162
});
62-
const $indeterminateSwitch = createCheckbox().data('indeterminate', true).bootstrapSwitch({
63+
$switch.bootstrapSwitch('state', true);
64+
expect(shadowState).toBe(true);
65+
expect($switch.bootstrapSwitch('state')).toBe(false);
66+
});
67+
68+
it('should retain state if `onSwitchChange` returns false when intederminate is true', () => {
69+
let shadowState;
70+
const $indeterminate = createCheckbox().bootstrapSwitch({
71+
state: false,
6372
onSwitchChange(event, state) {
64-
expect(state).toEqual(true);
73+
shadowState = state;
6574
return false;
6675
},
6776
});
68-
$switch.click();
69-
$indeterminateSwitch.click();
70-
expect($switch.bootstrapSwitch('state')).toEqual(false);
71-
expect($indeterminateSwitch.bootstrapSwitch('state')).toEqual(false);
77+
$indeterminate.data('indeterminate', true);
78+
$indeterminate.bootstrapSwitch('state', true);
79+
expect(shadowState).toBe(true);
80+
expect($indeterminate.bootstrapSwitch('state')).toBe(false);
7281
});
7382

74-
it('should change its state if onSwitchChange does not return false', () => {
83+
it('should change state if `onSwitchChange` does not return false', () => {
84+
let shadowState = null;
7585
const $switch = createCheckbox().bootstrapSwitch({
76-
onSwitchChange(event, state) {
77-
expect(state).toEqual(true);
86+
onSwitchChange: (event, state) => {
87+
shadowState = state;
7888
},
7989
});
80-
$switch.click();
81-
expect($switch.bootstrapSwitch('state')).toEqual(true);
90+
$switch.bootstrapSwitch('state', true);
91+
expect(shadowState).toBe(true);
92+
expect($switch.bootstrapSwitch('state')).toBe(true);
8293
});
8394
});
8495

85-
describe('The Radio Bootstrap Switch', () => {
86-
it('should conserve its state if onSwitchChange returns false', () => {
96+
describe('Radio', () => {
97+
it('should retain state if `onSwitchChange` returns false', () => {
8798
const $radio1 = createRadio().prop('checked', true);
8899
const $radio2 = createRadio().prop('checked', false);
89100
const $radio3 = createRadio().prop('checked', false);
90-
$('[name="name"]').bootstrapSwitch({
91-
onSwitchChange(e, s) {
92-
expect(s).toEqual(true);
101+
let shadowState = null;
102+
$radio1.add($radio2).add($radio3).bootstrapSwitch({
103+
onSwitchChange(event, state) {
104+
shadowState = state;
93105
return false;
94106
},
95107
});
96-
$radio2.click();
97-
expect($radio1.bootstrapSwitch('state')).toEqual(true);
98-
expect($radio2.bootstrapSwitch('state')).toEqual(false);
99-
expect($radio3.bootstrapSwitch('state')).toEqual(false);
108+
$radio2.bootstrapSwitch('state', true);
109+
expect(shadowState).toBe(true);
110+
expect($radio1.bootstrapSwitch('state')).toBe(true);
111+
expect($radio2.bootstrapSwitch('state')).toBe(false);
112+
expect($radio3.bootstrapSwitch('state')).toBe(false);
100113
});
101114

102-
it('should change its state if onSwitchChange not returns false', () => {
115+
it('should change its state if `onSwitchChange` does not return false', () => {
103116
const $radio1 = createRadio().prop('checked', true);
104117
const $radio2 = createRadio().prop('checked', false);
105118
const $radio3 = createRadio().prop('checked', false);
106-
$('[name="name"]').bootstrapSwitch({
107-
onSwitchChange(e, s) {
108-
expect(s).toEqual(true);
119+
let shadowState = null;
120+
$radio2.bootstrapSwitch({
121+
onSwitchChange(event, state) {
122+
shadowState = state;
123+
return false;
109124
},
110125
});
111126
$radio2.click();
112-
expect($radio1.bootstrapSwitch('state')).toEqual(false);
113-
expect($radio2.bootstrapSwitch('state')).toEqual(true);
114-
expect($radio3.bootstrapSwitch('state')).toEqual(false);
127+
expect(shadowState).toBe(true);
128+
expect($radio1.bootstrapSwitch('state')).toBe(false);
129+
expect($radio2.bootstrapSwitch('state')).toBe(true);
130+
expect($radio3.bootstrapSwitch('state')).toBe(false);
115131
});
116132
});
117133
});

src/setup-test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import $ from 'jquery';
2+
3+
global.jQuery = $;
4+
global.$ = global.jQuery;

0 commit comments

Comments
 (0)