forked from mgcrea/angular-strap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuttonSpec.js
More file actions
202 lines (173 loc) · 6.81 KB
/
buttonSpec.js
File metadata and controls
202 lines (173 loc) · 6.81 KB
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
'use strict';
describe('button', function () {
var scope, $sandbox, $compile, $timeout, $q;
beforeEach(module('$strap.directives'));
beforeEach(function() {
this.addMatchers({
toEquals: function(expected) {
return angular.equals(this.actual, expected);
}
});
});
beforeEach(inject(function ($injector, $rootScope, _$compile_, _$timeout_, _$q_) {
scope = $rootScope;
$compile = _$compile_;
$timeout = _$timeout_;
$q = _$q_;
$sandbox = $('<div id="sandbox"></div>').appendTo($('body'));
// Fix events not firing
$(document)
.off('click.button.data-api', '[data-toggle^=button]')
.on('click.button.data-api', '[data-toggle^=button]', function (e) {
var $btn = $(e.target);
if (!$btn.hasClass('btn')) $btn = $btn.closest('.btn');
$btn.button('toggle');
});
}));
afterEach(function() {
$sandbox.remove();
scope.$destroy();
});
var templates = {
'default': {
scope: {buttonValue: undefined},
element: '<div class="btn" ng-model="buttonValue" data-toggle="button" bs-button>'
},
'promise': {
scope: {},
element: '<div class="btn" ng-model="buttonValue" data-toggle="button" bs-button>'
},
'checkbox': {
scope: {checkbox: {left: false, middle: true, right: false}},
element: '<div class="btn-group" bs-buttons-checkbox>'
+ '<button type="button" class="btn" ng-model="checkbox.left">Left</button>'
+ '<button type="button" class="btn" ng-model="checkbox.middle">Middle</button>'
+ '<button type="button" class="btn" ng-model="checkbox.right">Right</button>'
+ '</div>'
},
'radio': {
scope: {radio: {left: false, middle: true, right: false}},
element: '<div class="btn-group" bs-buttons-radio>'
+ '<button type="button" class="btn" ng-model="radio.left">Left</button>'
+ '<button type="button" class="btn" ng-model="radio.middle">Middle</button>'
+ '<button type="button" class="btn" ng-model="radio.right">Right</button>'
+ '</div>'
},
'radio-single-model': {
scope: {radio: 'middle'},
element: '<div class="btn-group" ng-model="radio" bs-buttons-radio>'
+ '<button type="button" class="btn" value="left">Left</button>'
+ '<button type="button" class="btn" value="middle">Middle</button>'
+ '<button type="button" class="btn" value="right">Right</button>'
+ '</div>'
}
};
function compileDirective(template) {
template = template ? templates[template] : templates['default'];
angular.extend(scope, template.scope);
var $element = $(template.element).appendTo($sandbox);
$element = $compile($element)(scope);
scope.$digest();
if(template === 'radio' || template === 'radio-single-model') {
$timeout.flush();
}
return $element;
}
// Tests
it('should add "data-toggle" attr for you', function () {
var elm = compileDirective();
expect(elm.attr('data-toggle')).toBe('button');
});
describe('model change to view', function () {
it('should add class', function () {
var elm = compileDirective();
scope.$apply(function () {
scope.buttonValue = true;
});
expect(elm.hasClass('active')).toBe(true);
});
it('should remove class', function () {
var elm = compileDirective();
scope.$apply(function () {
scope.buttonValue = false;
});
expect(elm.hasClass('active')).toBe(false);
});
it('should handle $q model', function () {
var deferred = $q.defer();
scope.buttonValue = deferred.promise;
deferred.resolve(true);
var elm = compileDirective('promise');
expect(elm.hasClass('active')).toBe(true);
});
});
describe('view change to model', function () {
it('should set model true on click', function () {
var elm = compileDirective();
expect(scope.buttonValue).toBeUndefined();
elm.trigger('click');
expect(scope.buttonValue).toBe(true);
});
it('should set model false on double-click', function () {
var elm = compileDirective();
expect(scope.buttonValue).toBeUndefined();
elm.trigger('click');
elm.trigger('click');
expect(scope.buttonValue).toBe(false);
});
});
describe('buttons checkbox', function () {
it('should handle view to model changes', function () {
var elm = compileDirective('checkbox');
elm.find('[ng-model="checkbox.left"]').trigger('click');
expect(scope.checkbox).toEquals({left: true, middle: true, right: false});
elm.find('[ng-model="checkbox.left"]').trigger('click');
expect(scope.checkbox).toEquals({left: false, middle: true, right: false});
});
it('should handle model to view changes', function () {
var elm = compileDirective('checkbox');
scope.$apply(function () {
scope.checkbox = {left: true, middle: false, right: true};
});
expect(elm.find('[ng-model="checkbox.left"]').hasClass('active')).toBe(true);
expect(elm.find('[ng-model="checkbox.middle"]').hasClass('active')).toBe(false);
expect(elm.find('[ng-model="checkbox.right"]').hasClass('active')).toBe(true);
});
});
describe('buttons radio', function () {
it('should handle view to model changes', function () {
var elm = compileDirective('radio');
elm.find('[ng-model="radio.left"]').trigger('click');
expect(scope.radio).toEquals({left: true, middle: false, right: false});
elm.find('[ng-model="radio.left"]').trigger('click');
expect(scope.radio).toEquals({left: true, middle: false, right: false});
});
it('should handle model to view changes', function () {
var elm = compileDirective('radio');
scope.$apply(function () {
scope.radio = {left: false, middle: false, right: true};
});
expect(elm.find('[ng-model="radio.left"]').hasClass('active')).toBe(false);
expect(elm.find('[ng-model="radio.middle"]').hasClass('active')).toBe(false);
expect(elm.find('[ng-model="radio.right"]').hasClass('active')).toBe(true);
});
});
describe('buttons radio single model', function () {
it('should handle view to model changes', function () {
var elm = compileDirective('radio-single-model');
elm.find('[value="left"]').trigger('click');
expect(scope.radio).toEquals('left');
elm.find('[value="left"]').trigger('click');
expect(scope.radio).toEquals('left');
});
it('should handle model to view changes', function () {
var elm = compileDirective('radio-single-model');
scope.$apply(function () {
scope.radio = 'right';
});
expect(elm.find('[value="left"]').hasClass('active')).toBe(false);
expect(elm.find('[value="middle"]').hasClass('active')).toBe(false);
expect(elm.find('[value="right"]').hasClass('active')).toBe(true);
});
});
});