forked from angular/angular.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdirectivesSpec.js
310 lines (263 loc) · 10.1 KB
/
directivesSpec.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
describe("directive", function(){
var compile, model, element;
beforeEach(function() {
compile = function(html) {
element = jqLite(html);
return model = angular.compile(element)();
};
});
afterEach(function() {
dealoc(model);
});
it("should ng:init", function() {
var scope = compile('<div ng:init="a=123"></div>');
expect(scope.a).toEqual(123);
});
it("should ng:eval", function() {
var scope = compile('<div ng:init="a=0" ng:eval="a = a + 1"></div>');
expect(scope.a).toEqual(1);
scope.$eval();
expect(scope.a).toEqual(2);
});
describe('ng:bind', function(){
it('should set text', function() {
var scope = compile('<div ng:bind="a"></div>');
expect(element.text()).toEqual('');
scope.a = 'misko';
scope.$eval();
expect(element.hasClass('ng-binding')).toEqual(true);
expect(element.text()).toEqual('misko');
});
it('should set text to blank if undefined', function() {
var scope = compile('<div ng:bind="a"></div>');
scope.a = 'misko';
scope.$eval();
expect(element.text()).toEqual('misko');
scope.a = undefined;
scope.$eval();
expect(element.text()).toEqual('');
});
it('should set html', function() {
var scope = compile('<div ng:bind="html|html"></div>');
scope.html = '<div unknown>hello</div>';
scope.$eval();
expect(lowercase(element.html())).toEqual('<div>hello</div>');
});
it('should set unsafe html', function() {
var scope = compile('<div ng:bind="html|html:\'unsafe\'"></div>');
scope.html = '<div onclick="">hello</div>';
scope.$eval();
expect(lowercase(element.html())).toEqual('<div onclick="">hello</div>');
});
it('should set element element', function() {
angularFilter.myElement = function() {
return jqLite('<a>hello</a>');
};
var scope = compile('<div ng:bind="0|myElement"></div>');
scope.$eval();
expect(lowercase(element.html())).toEqual('<a>hello</a>');
});
it('should have $element set to current bind element', function(){
angularFilter.myFilter = function(){
this.$element.addClass("filter");
return 'HELLO';
};
var scope = compile('<div>before<div ng:bind="0|myFilter"></div>after</div>');
expect(sortedHtml(scope.$element)).toEqual('<div>before<div class="filter" ng:bind="0|myFilter">HELLO</div>after</div>');
});
it('should suppress rendering of falsy values', function(){
var scope = compile('<div>{{ null }}{{ undefined }}{{ "" }}-{{ 0 }}{{ false }}</div>');
expect(scope.$element.text()).toEqual('-0false');
});
});
describe('ng:bind-template', function(){
it('should ng:bind-template', function() {
var scope = compile('<div ng:bind-template="Hello {{name}}!"></div>');
scope.$set('name', 'Misko');
scope.$eval();
expect(element.hasClass('ng-binding')).toEqual(true);
expect(element.text()).toEqual('Hello Misko!');
});
it('should have $element set to current bind element', function(){
var innerText = 'blank';
angularFilter.myFilter = function(text){
innerText = this.$element.text();
return text;
};
var scope = compile('<div>before<span ng:bind-template="{{\'HELLO\'|myFilter}}">INNER</span>after</div>');
expect(scope.$element.text()).toEqual("beforeHELLOafter");
expect(innerText).toEqual('INNER');
});
});
describe('ng:bind-attr', function(){
it('should bind attributes', function(){
var scope = compile('<img ng:bind-attr="{src:\'http://localhost/mysrc\', alt:\'myalt\'}"/>');
expect(element.attr('src')).toEqual('http://localhost/mysrc');
expect(element.attr('alt')).toEqual('myalt');
});
it('should not pretty print JSON in attributes', function(){
var scope = compile('<img alt="{{ {a:1} }}"/>');
expect(element.attr('alt')).toEqual('{"a":1}');
});
});
it('should remove special attributes on false', function(){
var scope = compile('<input ng:bind-attr="{disabled:\'{{disabled}}\', readonly:\'{{readonly}}\', checked:\'{{checked}}\'}"/>');
var input = scope.$element[0];
expect(input.disabled).toEqual(false);
expect(input.readOnly).toEqual(false);
expect(input.checked).toEqual(false);
scope.disabled = true;
scope.readonly = true;
scope.checked = true;
scope.$eval();
expect(input.disabled).toEqual(true);
expect(input.readOnly).toEqual(true);
expect(input.checked).toEqual(true);
});
describe('ng:click', function(){
it('should get called on a click', function(){
var scope = compile('<div ng:click="clicked = true"></div>');
scope.$eval();
expect(scope.$get('clicked')).toBeFalsy();
browserTrigger(element, 'click');
expect(scope.$get('clicked')).toEqual(true);
});
it('should stop event propagation', function() {
var scope = compile('<div ng:click="outer = true"><div ng:click="inner = true"></div></div>');
scope.$eval();
expect(scope.outer).not.toBeDefined();
expect(scope.inner).not.toBeDefined();
var innerDiv = element.children()[0];
browserTrigger(innerDiv, 'click');
expect(scope.outer).not.toBeDefined();
expect(scope.inner).toEqual(true);
});
});
describe('ng:submit', function() {
it('should get called on form submit', function() {
var scope = compile('<form action="" ng:submit="submitted = true">' +
'<input type="submit"/>' +
'</form>');
scope.$eval();
expect(scope.submitted).not.toBeDefined();
browserTrigger(element.children()[0]);
expect(scope.submitted).toEqual(true);
});
});
it('should ng:class', function(){
var scope = compile('<div class="existing" ng:class="[\'A\', \'B\']"></div>');
scope.$eval();
expect(element.hasClass('existing')).toBeTruthy();
expect(element.hasClass('A')).toBeTruthy();
expect(element.hasClass('B')).toBeTruthy();
});
it('should ng:class odd/even', function(){
var scope = compile('<ul><li ng:repeat="i in [0,1]" class="existing" ng:class-odd="\'odd\'" ng:class-even="\'even\'"></li><ul>');
scope.$eval();
var e1 = jqLite(element[0].childNodes[1]);
var e2 = jqLite(element[0].childNodes[2]);
expect(e1.hasClass('existing')).toBeTruthy();
expect(e1.hasClass('odd')).toBeTruthy();
expect(e2.hasClass('existing')).toBeTruthy();
expect(e2.hasClass('even')).toBeTruthy();
});
describe('ng:style', function(){
it('should set', function(){
var scope = compile('<div ng:style="{height: \'40px\'}"></div>');
scope.$eval();
expect(element.css('height')).toEqual('40px');
});
it('should silently ignore undefined style', function() {
var scope = compile('<div ng:style="myStyle"></div>');
scope.$eval();
expect(element.hasClass('ng-exception')).toBeFalsy();
});
it('should preserve and remove previous style', function(){
var scope = compile('<div style="height: 10px;" ng:style="myStyle"></div>');
scope.$eval();
expect(getStyle(element)).toEqual({height: '10px'});
scope.myStyle = {height: '20px', width: '10px'};
scope.$eval();
expect(getStyle(element)).toEqual({height: '20px', width: '10px'});
scope.myStyle = {};
scope.$eval();
expect(getStyle(element)).toEqual({height: '10px'});
});
});
it('should silently ignore undefined ng:style', function() {
var scope = compile('<div ng:style="myStyle"></div>');
scope.$eval();
expect(element.hasClass('ng-exception')).toBeFalsy();
});
it('should ng:show', function(){
var scope = compile('<div ng:hide="hide"></div>');
scope.$eval();
expect(isCssVisible(scope.$element)).toEqual(true);
scope.$set('hide', true);
scope.$eval();
expect(isCssVisible(scope.$element)).toEqual(false);
});
it('should ng:hide', function(){
var scope = compile('<div ng:show="show"></div>');
scope.$eval();
expect(isCssVisible(scope.$element)).toEqual(false);
scope.$set('show', true);
scope.$eval();
expect(isCssVisible(scope.$element)).toEqual(true);
});
describe('ng:controller', function(){
var temp;
beforeEach(function(){
temp = window.temp = {};
temp.Greeter = function(){
this.$root.greeter = this;
this.greeting = 'hello';
this.suffix = '!';
};
temp.Greeter.prototype = {
greet: function(name) {
return this.greeting + ' ' + name + this.suffix;
}
};
});
afterEach(function(){
window.temp = undefined;
});
it('should bind', function(){
var scope = compile('<div ng:controller="temp.Greeter"></div>');
expect(scope.greeter.greeting).toEqual('hello');
expect(scope.greeter.greet('misko')).toEqual('hello misko!');
});
it('should support nested controllers', function(){
temp.ChildGreeter = function(){
this.greeting = 'hey';
this.$root.childGreeter = this;
};
temp.ChildGreeter.prototype = {
greet: function() {
return this.greeting + ' dude' + this.suffix;
}
};
var scope = compile('<div ng:controller="temp.Greeter"><div ng:controller="temp.ChildGreeter">{{greet("misko")}}</div></div>');
expect(scope.greeting).not.toBeDefined();
expect(scope.greeter.greeting).toEqual('hello');
expect(scope.greeter.greet('misko')).toEqual('hello misko!');
expect(scope.greeter.greeting).toEqual('hello');
expect(scope.childGreeter.greeting).toEqual('hey');
expect(scope.childGreeter.$parent.greeting).toEqual('hello');
expect(scope.$element.text()).toEqual('hey dude!');
});
});
it('should eval things according to ng:eval-order', function(){
var scope = compile(
'<div ng:init="log=\'\'">' +
'{{log = log + \'e\'}}' +
'<span ng:eval-order="first" ng:eval="log = log + \'a\'">' +
'{{log = log + \'b\'}}' +
'<span src="{{log = log + \'c\'}}"></span>' +
'<span bind-template="{{log = log + \'d\'}}"></span>' +
'</span>' +
'</div>');
expect(scope.log).toEqual('abcde');
});
});