forked from cgarvis/angular-toggle-switch
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathangular-toggle-switch.spec.js
204 lines (177 loc) · 6.51 KB
/
angular-toggle-switch.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
describe('Toggle Switch', function() {
var $scope, $compile;
var baseTemplate = '<toggle-switch ng-model="switchState">\n</toggle-switch>';
var onLabelTemplate = '<toggle-switch ng-model="switchState" on-label="CUSTOM-ON">\n</toggle-switch>';
var offLabelTemplate = '<toggle-switch ng-model="switchState" off-label="CUSTOM-OFF">\n</toggle-switch>';
var knobLabelTemplate = '<toggle-switch ng-model="switchState" knob-label="CUSTOM">\n</toggle-switch>';
var htmlLabelsTemplate = '<toggle-switch ng-model="switchState" on-label="<i class=\'icon-ok icon-white\'></i>" off-label="<i class=\'icon-remove\'></i>">\n</toggle-switch>';
var disabledTemplate = '<toggle-switch ng-model="switchState" is-disabled="isDisabled">\n</toggle-switch>';
var changeTemplate = '<toggle-switch ng-model="switchState" ng-change="changedState()">\n</toggle-switch>';
var tabindexTemplate = '<toggle-switch ng-model="switchState" tabindex="2">\n</toggle-switch>';
// Load up just our module
beforeEach(module('toggle-switch'));
beforeEach(inject(function($rootScope, _$compile_) {
// Get an isolated scope
$scope = $rootScope.$new();
$compile = _$compile_;
}));
function compileDirective(template, scope) {
// Compile Directive
var elm = angular.element(template);
$compile(elm)(scope);
scope.$apply();
return elm;
}
describe('default labels', function() {
var elm;
beforeEach(function() {
elm = compileDirective(baseTemplate, $scope);
});
it('onLabel', function() {
expect(elm.text()).toContain('On');
});
it('offLabel', function() {
expect(elm.text()).toContain('Off');
});
});
describe('when state is null', function() {
it('changes model to true when clicked', function() {
var elm = compileDirective(baseTemplate, $scope);
elm.triggerHandler('click');
expect($scope.switchState).toEqual(true);
});
});
describe('when state is true', function() {
// Change state to true
beforeEach(function() {
$scope.$apply(function() {
$scope.switchState = true;
});
$scope.changedState = function(){};
spyOn($scope, 'changedState');
});
it('changes model to false when clicked', function() {
var elm = compileDirective(baseTemplate, $scope);
elm.triggerHandler('click');
expect($scope.switchState).toEqual(false);
});
it('change handler is called when clicked', function() {
var elm = compileDirective(changeTemplate, $scope);
elm.triggerHandler('click');
expect($scope.changedState).toHaveBeenCalled();
});
});
describe('when state is false', function() {
// Change state to true
beforeEach(function() {
$scope.$apply(function() {
$scope.switchState = false;
});
$scope.changedState = function(){};
spyOn($scope, 'changedState');
});
it('changes model to true when clicked', function() {
var elm = compileDirective(baseTemplate, $scope);
elm.triggerHandler('click');
expect($scope.switchState).toEqual(true);
});
it('change handler is called when clicked', function() {
var elm = compileDirective(changeTemplate, $scope);
elm.triggerHandler('click');
expect($scope.changedState).toHaveBeenCalled();
});
});
describe('when a key is pressed', function() {
// Change state to true
beforeEach(function() {
$scope.$apply(function() {
$scope.switchState = false;
});
$scope.changedState = function(){};
spyOn($scope, 'changedState');
});
it('and it is "space" change handler gets called', function() {
var elm = compileDirective(changeTemplate, $scope);
var event = {
type: 'keypress',
charCode: 32
};
elm.triggerHandler(event);
expect($scope.changedState).toHaveBeenCalled();
});
it('and it is "space" but modifier is set do not call change handler', function() {
var elm = compileDirective(changeTemplate, $scope);
var event = {
type: 'keypress',
charCode: 32,
ctrlKey: true
};
elm.triggerHandler(event);
expect($scope.changedState).not.toHaveBeenCalled();
});
it('and it is not "space" do not call change handler', function() {
var elm = compileDirective(changeTemplate, $scope);
var event = {
type: 'keypress',
charCode: 12
};
elm.triggerHandler(event);
expect($scope.changedState).not.toHaveBeenCalled();
});
});
describe('when there is a custom `on-label`', function () {
// @TODO: figure out how to deal with html in Angular 1.2
//describe('is html', function() {
// it('sets the on label', function() {
// var elm = compileDirective(htmlLabelsTemplate, $scope);
// expect(elm.html()).toContain('<i class="icon-ok icon-white"></i>');
// });
//});
describe('is string', function() {
it('sets the on label', function() {
var elm = compileDirective(onLabelTemplate, $scope);
expect(elm.text()).toContain('CUSTOM-ON');
});
});
});
describe('when there is a custom `off-label`', function () {
// @TODO: figure out how to deal with html in Angular 1.2
//describe('is html', function() {
// it('sets the on label', function() {
// var elm = compileDirective(htmlLabelsTemplate, $scope);
// expect(elm.html()).toContain('<i class="icon-remove"></i>');
// });
//});
describe('is string', function() {
it('sets the on label', function() {
var elm = compileDirective(offLabelTemplate, $scope);
expect(elm.text()).toContain('CUSTOM-OFF');
});
});
});
describe('when there is a custom `knob-label`', function () {
it('sets the on label', function() {
var elm = compileDirective(knobLabelTemplate, $scope);
expect(elm.text()).toContain('CUSTOM');
});
});
describe('when toggle is disabled', function() {
it('ngModel does not change on click', function() {
$scope.switchState = true;
$scope.isDisabled = true;
var elm = compileDirective(disabledTemplate, $scope);
elm.triggerHandler('click');
expect($scope.switchState).toEqual(true);
});
});
describe('tabindex is set', function() {
it('to default value', function() {
var elm = compileDirective(baseTemplate, $scope);
expect(elm.attr('tabindex')).toEqual('0');
});
it('to custom value', function() {
var elm = compileDirective(tabindexTemplate, $scope);
expect(elm.attr('tabindex')).toEqual('2');
});
});
});