forked from angular/angular.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbooleanAttrsSpec.js
180 lines (140 loc) · 5.55 KB
/
booleanAttrsSpec.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
'use strict';
describe('boolean attr directives', function() {
var element;
afterEach(function() {
dealoc(element);
});
it('should properly evaluate 0 as false', inject(function($rootScope, $compile) {
// jQuery does not treat 0 as false, when setting attr()
element = $compile('<button ng-disabled="isDisabled">Button</button>')($rootScope)
$rootScope.isDisabled = 0;
$rootScope.$digest();
expect(element.attr('disabled')).toBeFalsy();
$rootScope.isDisabled = 1;
$rootScope.$digest();
expect(element.attr('disabled')).toBeTruthy();
}));
it('should bind disabled', inject(function($rootScope, $compile) {
element = $compile('<button ng-disabled="isDisabled">Button</button>')($rootScope)
$rootScope.isDisabled = false;
$rootScope.$digest();
expect(element.attr('disabled')).toBeFalsy();
$rootScope.isDisabled = true;
$rootScope.$digest();
expect(element.attr('disabled')).toBeTruthy();
}));
it('should bind checked', inject(function($rootScope, $compile) {
element = $compile('<input type="checkbox" ng-checked="isChecked" />')($rootScope)
$rootScope.isChecked = false;
$rootScope.$digest();
expect(element.attr('checked')).toBeFalsy();
$rootScope.isChecked=true;
$rootScope.$digest();
expect(element.attr('checked')).toBeTruthy();
}));
it('should bind selected', inject(function($rootScope, $compile) {
element = $compile('<select><option value=""></option><option ng-selected="isSelected">Greetings!</option></select>')($rootScope)
jqLite(document.body).append(element)
$rootScope.isSelected=false;
$rootScope.$digest();
expect(element.children()[1].selected).toBeFalsy();
$rootScope.isSelected=true;
$rootScope.$digest();
expect(element.children()[1].selected).toBeTruthy();
}));
it('should bind readonly', inject(function($rootScope, $compile) {
element = $compile('<input type="text" ng-readonly="isReadonly" />')($rootScope)
$rootScope.isReadonly=false;
$rootScope.$digest();
expect(element.attr('readOnly')).toBeFalsy();
$rootScope.isReadonly=true;
$rootScope.$digest();
expect(element.attr('readOnly')).toBeTruthy();
}));
it('should bind multiple', inject(function($rootScope, $compile) {
element = $compile('<select ng-multiple="isMultiple"></select>')($rootScope)
$rootScope.isMultiple=false;
$rootScope.$digest();
expect(element.attr('multiple')).toBeFalsy();
$rootScope.isMultiple='multiple';
$rootScope.$digest();
expect(element.attr('multiple')).toBeTruthy();
}));
it('should bind open', inject(function($rootScope, $compile) {
element = $compile('<details ng-open="isOpen"></details>')($rootScope)
$rootScope.isOpen=false;
$rootScope.$digest();
expect(element.attr('open')).toBeFalsy();
$rootScope.isOpen=true;
$rootScope.$digest();
expect(element.attr('open')).toBeTruthy();
}));
});
describe('ngSrc', function() {
it('should interpolate the expression and bind to src', inject(function($compile, $rootScope) {
var element = $compile('<div ng-src="some/{{id}}"></div>')($rootScope);
$rootScope.$digest();
expect(element.attr('src')).toEqual('some/');
$rootScope.$apply(function() {
$rootScope.id = 1;
});
expect(element.attr('src')).toEqual('some/1');
dealoc(element);
}));
if (msie) {
it('should update the element property as well as the attribute', inject(
function($compile, $rootScope) {
// on IE, if "ng:src" directive declaration is used and "src" attribute doesn't exist
// then calling element.setAttribute('src', 'foo') doesn't do anything, so we need
// to set the property as well to achieve the desired effect
var element = $compile('<div ng-src="some/{{id}}"></div>')($rootScope);
$rootScope.$digest();
expect(element.prop('src')).toEqual('some/');
$rootScope.$apply(function() {
$rootScope.id = 1;
});
expect(element.prop('src')).toEqual('some/1');
dealoc(element);
}));
}
});
describe('ngSrcset', function() {
it('should interpolate the expression and bind to srcset', inject(function($compile, $rootScope) {
var element = $compile('<div ng-srcset="some/{{id}} 2x"></div>')($rootScope);
$rootScope.$digest();
expect(element.attr('srcset')).toEqual('some/ 2x');
$rootScope.$apply(function() {
$rootScope.id = 1;
});
expect(element.attr('srcset')).toEqual('some/1 2x');
dealoc(element);
}));
});
describe('ngHref', function() {
var element;
afterEach(function() {
dealoc(element);
});
it('should interpolate the expression and bind to href', inject(function($compile, $rootScope) {
element = $compile('<div ng-href="some/{{id}}"></div>')($rootScope)
$rootScope.$digest();
expect(element.attr('href')).toEqual('some/');
$rootScope.$apply(function() {
$rootScope.id = 1;
});
expect(element.attr('href')).toEqual('some/1');
}));
it('should bind href and merge with other attrs', inject(function($rootScope, $compile) {
element = $compile('<a ng-href="{{url}}" rel="{{rel}}"></a>')($rootScope);
$rootScope.url = 'http://server';
$rootScope.rel = 'REL';
$rootScope.$digest();
expect(element.attr('href')).toEqual('http://server');
expect(element.attr('rel')).toEqual('REL');
}));
it('should bind href even if no interpolation', inject(function($rootScope, $compile) {
element = $compile('<a ng-href="http://server"></a>')($rootScope)
$rootScope.$digest();
expect(element.attr('href')).toEqual('http://server');
}));
});