1- const { $, describe, beforeEach, afterEach, it, expect } = window ;
1+ import './bootstrap-switch' ;
2+
3+ const { $ } = global ;
24
35describe ( '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} ) ;
0 commit comments