This repository was archived by the owner on Dec 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathangular-toggle-switch.spec.js
164 lines (140 loc) · 5.27 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
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" ng-disabled="isDisabled">\n</toggle-switch>';
var changeTemplate = '<toggle-switch ng-model="switchState" ng-change="changedState()">\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 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('then re-enabled', function() {
it('ngModel changes on click', function() {
$scope.switchState = true;
$scope.isDisabled = true;
var elm = compileDirective(disabledTemplate, $scope);
$scope.$apply(function() {
$scope.isDisabled = false;
});
elm.triggerHandler('click');
expect($scope.switchState).toEqual(false);
});
});
});
});