2010-11-18 06:32:35 +00:00
|
|
|
describe("directive", function(){
|
2010-03-22 20:58:04 +00:00
|
|
|
|
2010-03-26 05:03:11 +00:00
|
|
|
var compile, model, element;
|
2010-03-22 20:58:04 +00:00
|
|
|
|
|
|
|
|
beforeEach(function() {
|
2010-03-23 21:57:11 +00:00
|
|
|
var compiler = new Compiler(angularTextMarkup, angularAttrMarkup, angularDirective, angularWidget);
|
2010-03-22 20:58:04 +00:00
|
|
|
compile = function(html) {
|
|
|
|
|
element = jqLite(html);
|
2010-03-26 05:03:11 +00:00
|
|
|
model = compiler.compile(element)(element);
|
|
|
|
|
model.$init();
|
|
|
|
|
return model;
|
2010-03-22 20:58:04 +00:00
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
2010-03-25 20:01:08 +00:00
|
|
|
afterEach(function() {
|
2010-12-02 04:29:54 +00:00
|
|
|
dealoc(model);
|
2010-03-23 03:20:05 +00:00
|
|
|
});
|
|
|
|
|
|
2010-07-02 22:39:47 +00:00
|
|
|
it("should ng:init", function() {
|
|
|
|
|
var scope = compile('<div ng:init="a=123"></div>');
|
2010-03-26 05:03:11 +00:00
|
|
|
expect(scope.a).toEqual(123);
|
2010-03-22 20:58:04 +00:00
|
|
|
});
|
|
|
|
|
|
2010-07-02 22:39:47 +00:00
|
|
|
it("should ng:eval", function() {
|
|
|
|
|
var scope = compile('<div ng:init="a=0" ng:eval="a = a + 1"></div>');
|
2010-03-26 05:03:11 +00:00
|
|
|
expect(scope.a).toEqual(1);
|
|
|
|
|
scope.$eval();
|
|
|
|
|
expect(scope.a).toEqual(2);
|
2010-03-22 20:58:04 +00:00
|
|
|
});
|
|
|
|
|
|
2010-07-15 20:13:21 +00:00
|
|
|
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();
|
2010-10-27 05:02:24 +00:00
|
|
|
expect(element.hasClass('ng-binding')).toEqual(true);
|
2010-07-15 20:13:21 +00:00
|
|
|
expect(element.text()).toEqual('misko');
|
|
|
|
|
});
|
2010-03-22 20:58:04 +00:00
|
|
|
|
2010-10-13 17:51:16 +00:00
|
|
|
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('');
|
|
|
|
|
});
|
|
|
|
|
|
2010-07-15 20:13:21 +00:00
|
|
|
it('should set html', function() {
|
|
|
|
|
var scope = compile('<div ng:bind="html|html"></div>');
|
2010-10-19 04:20:47 +00:00
|
|
|
scope.html = '<div unknown>hello</div>';
|
2010-07-15 20:13:21 +00:00
|
|
|
scope.$eval();
|
|
|
|
|
expect(lowercase(element.html())).toEqual('<div>hello</div>');
|
|
|
|
|
});
|
|
|
|
|
|
2010-10-19 04:20:47 +00:00
|
|
|
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>');
|
|
|
|
|
});
|
|
|
|
|
|
2010-07-15 20:13:21 +00:00
|
|
|
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(){
|
2010-10-13 17:51:16 +00:00
|
|
|
this.$element.addClass("filter");
|
|
|
|
|
return 'HELLO';
|
2010-07-15 20:13:21 +00:00
|
|
|
};
|
|
|
|
|
var scope = compile('<div>before<div ng:bind="0|myFilter"></div>after</div>');
|
2010-10-13 17:51:16 +00:00
|
|
|
expect(sortedHtml(scope.$element)).toEqual('<div>before<div class="filter" ng:bind="0|myFilter">HELLO</div>after</div>');
|
2010-07-15 20:13:21 +00:00
|
|
|
});
|
2010-04-12 21:28:15 +00:00
|
|
|
|
2010-05-07 20:43:54 +00:00
|
|
|
});
|
|
|
|
|
|
2010-07-15 20:13:21 +00:00
|
|
|
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();
|
2010-10-27 05:02:24 +00:00
|
|
|
expect(element.hasClass('ng-binding')).toEqual(true);
|
2010-07-15 20:13:21 +00:00
|
|
|
expect(element.text()).toEqual('Hello Misko!');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should have $element set to current bind element', function(){
|
2010-07-15 21:16:04 +00:00
|
|
|
var innerText = 'blank';
|
2010-07-15 20:35:00 +00:00
|
|
|
angularFilter.myFilter = function(text){
|
|
|
|
|
innerText = this.$element.text();
|
|
|
|
|
return text;
|
2010-07-15 20:13:21 +00:00
|
|
|
};
|
2010-07-15 21:16:04 +00:00
|
|
|
var scope = compile('<div>before<span ng:bind-template="{{\'HELLO\'|myFilter}}">INNER</span>after</div>');
|
2010-07-15 20:13:21 +00:00
|
|
|
expect(scope.$element.text()).toEqual("beforeHELLOafter");
|
2010-07-15 20:35:00 +00:00
|
|
|
expect(innerText).toEqual('INNER');
|
2010-07-15 20:13:21 +00:00
|
|
|
});
|
|
|
|
|
|
2010-03-23 21:57:11 +00:00
|
|
|
});
|
|
|
|
|
|
2010-07-02 22:39:47 +00:00
|
|
|
it('should ng:bind-attr', function(){
|
|
|
|
|
var scope = compile('<img ng:bind-attr="{src:\'http://localhost/mysrc\', alt:\'myalt\'}"/>');
|
2010-04-21 21:29:05 +00:00
|
|
|
expect(element.attr('src')).toEqual('http://localhost/mysrc');
|
2010-03-22 20:58:04 +00:00
|
|
|
expect(element.attr('alt')).toEqual('myalt');
|
|
|
|
|
});
|
|
|
|
|
|
2010-04-16 21:01:29 +00:00
|
|
|
it('should remove special attributes on false', function(){
|
2010-07-02 22:39:47 +00:00
|
|
|
var scope = compile('<input ng:bind-attr="{disabled:\'{{disabled}}\', readonly:\'{{readonly}}\', checked:\'{{checked}}\'}"/>');
|
2010-04-23 05:09:17 +00:00
|
|
|
var input = scope.$element[0];
|
|
|
|
|
expect(input.disabled).toEqual(false);
|
|
|
|
|
expect(input.readOnly).toEqual(false);
|
|
|
|
|
expect(input.checked).toEqual(false);
|
2010-04-16 21:01:29 +00:00
|
|
|
|
|
|
|
|
scope.disabled = true;
|
|
|
|
|
scope.readonly = true;
|
|
|
|
|
scope.checked = true;
|
|
|
|
|
scope.$eval();
|
|
|
|
|
|
2010-04-23 05:09:17 +00:00
|
|
|
expect(input.disabled).toEqual(true);
|
|
|
|
|
expect(input.readOnly).toEqual(true);
|
|
|
|
|
expect(input.checked).toEqual(true);
|
2010-04-16 21:01:29 +00:00
|
|
|
});
|
|
|
|
|
|
2010-07-02 22:39:47 +00:00
|
|
|
it('should ng:watch', function(){
|
|
|
|
|
var scope = compile('<div ng:watch="i: count = count + 1" ng:init="count = 0">');
|
2010-03-26 05:03:11 +00:00
|
|
|
scope.$eval();
|
|
|
|
|
scope.$eval();
|
2010-11-11 00:08:54 +00:00
|
|
|
expect(scope.$get('count')).toEqual(1);
|
2010-03-22 23:07:42 +00:00
|
|
|
|
2010-03-26 05:03:11 +00:00
|
|
|
scope.$set('i', 0);
|
|
|
|
|
scope.$eval();
|
|
|
|
|
scope.$eval();
|
2010-11-11 00:08:54 +00:00
|
|
|
expect(scope.$get('count')).toEqual(2);
|
2010-03-22 23:07:42 +00:00
|
|
|
});
|
2010-03-23 01:20:49 +00:00
|
|
|
|
2010-07-27 22:54:50 +00:00
|
|
|
describe('ng:click', function(){
|
2010-10-22 23:45:56 +00:00
|
|
|
it('should get called on a click', function(){
|
2010-07-27 22:54:50 +00:00
|
|
|
var scope = compile('<div ng:click="clicked = true"></div>');
|
|
|
|
|
scope.$eval();
|
|
|
|
|
expect(scope.$get('clicked')).toBeFalsy();
|
2010-03-23 01:20:49 +00:00
|
|
|
|
2010-10-19 23:14:16 +00:00
|
|
|
browserTrigger(element, 'click');
|
2010-07-27 22:54:50 +00:00
|
|
|
expect(scope.$get('clicked')).toEqual(true);
|
|
|
|
|
});
|
2010-09-30 15:07:36 +00:00
|
|
|
|
|
|
|
|
it('should stop event propagation', function() {
|
|
|
|
|
var scope = compile('<div ng:click="outer = true"><div ng:click="inner = true"></div></div>');
|
|
|
|
|
scope.$eval();
|
2010-10-22 23:45:56 +00:00
|
|
|
expect(scope.outer).not.toBeDefined();
|
|
|
|
|
expect(scope.inner).not.toBeDefined();
|
2010-09-30 15:07:36 +00:00
|
|
|
|
2010-10-22 23:45:56 +00:00
|
|
|
var innerDiv = element.children()[0];
|
2010-09-30 15:07:36 +00:00
|
|
|
|
2010-10-19 23:14:16 +00:00
|
|
|
browserTrigger(innerDiv, 'click');
|
2010-10-22 23:45:56 +00:00
|
|
|
expect(scope.outer).not.toBeDefined();
|
|
|
|
|
expect(scope.inner).toEqual(true);
|
2010-10-04 16:00:09 +00:00
|
|
|
});
|
2010-03-23 01:20:49 +00:00
|
|
|
});
|
2010-03-23 04:29:57 +00:00
|
|
|
|
2010-10-22 23:44:14 +00:00
|
|
|
|
|
|
|
|
describe('ng:submit', function() {
|
|
|
|
|
it('should get called on form submit', function() {
|
|
|
|
|
var scope = compile('<form action="" ng:submit="submitted = true">' +
|
2010-10-26 22:35:58 +00:00
|
|
|
'<input type="submit"/>' +
|
2010-10-22 23:44:14 +00:00
|
|
|
'</form>');
|
|
|
|
|
scope.$eval();
|
|
|
|
|
expect(scope.submitted).not.toBeDefined();
|
|
|
|
|
|
|
|
|
|
browserTrigger(element.children()[0]);
|
|
|
|
|
expect(scope.submitted).toEqual(true);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2010-07-02 22:39:47 +00:00
|
|
|
it('should ng:class', function(){
|
|
|
|
|
var scope = compile('<div class="existing" ng:class="[\'A\', \'B\']"></div>');
|
2010-03-26 05:03:11 +00:00
|
|
|
scope.$eval();
|
2010-03-23 04:29:57 +00:00
|
|
|
expect(element.hasClass('existing')).toBeTruthy();
|
|
|
|
|
expect(element.hasClass('A')).toBeTruthy();
|
|
|
|
|
expect(element.hasClass('B')).toBeTruthy();
|
|
|
|
|
});
|
|
|
|
|
|
2010-07-02 22:39:47 +00:00
|
|
|
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>');
|
2010-03-26 05:03:11 +00:00
|
|
|
scope.$eval();
|
2010-03-30 03:25:42 +00:00
|
|
|
var e1 = jqLite(element[0].childNodes[1]);
|
|
|
|
|
var e2 = jqLite(element[0].childNodes[2]);
|
2010-03-23 04:29:57 +00:00
|
|
|
expect(e1.hasClass('existing')).toBeTruthy();
|
2010-03-31 20:57:25 +00:00
|
|
|
expect(e1.hasClass('odd')).toBeTruthy();
|
2010-03-23 04:29:57 +00:00
|
|
|
expect(e2.hasClass('existing')).toBeTruthy();
|
2010-03-31 20:57:25 +00:00
|
|
|
expect(e2.hasClass('even')).toBeTruthy();
|
2010-03-23 04:29:57 +00:00
|
|
|
});
|
|
|
|
|
|
2010-07-15 00:07:23 +00:00
|
|
|
describe('ng:style', function(){
|
|
|
|
|
it('should set', function(){
|
|
|
|
|
var scope = compile('<div ng:style="{color:\'red\'}"></div>');
|
|
|
|
|
scope.$eval();
|
|
|
|
|
expect(element.css('color')).toEqual('red');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should silently ignore undefined style', function() {
|
|
|
|
|
var scope = compile('<div ng:style="myStyle"></div>');
|
|
|
|
|
scope.$eval();
|
|
|
|
|
expect(element.hasClass('ng-exception')).toBeFalsy();
|
|
|
|
|
});
|
|
|
|
|
|
2010-07-15 21:16:04 +00:00
|
|
|
it('should preserve and remove previous style', function(){
|
2010-07-15 00:07:23 +00:00
|
|
|
var scope = compile('<div style="color:red;" ng:style="myStyle"></div>');
|
|
|
|
|
scope.$eval();
|
2010-07-15 00:48:09 +00:00
|
|
|
expect(getStyle(element)).toEqual({color:'red'});
|
2010-07-15 00:07:23 +00:00
|
|
|
scope.myStyle = {color:'blue', width:'10px'};
|
|
|
|
|
scope.$eval();
|
2010-07-15 00:48:09 +00:00
|
|
|
expect(getStyle(element)).toEqual({color:'blue', width:'10px'});
|
2010-07-15 00:07:23 +00:00
|
|
|
scope.myStyle = {};
|
|
|
|
|
scope.$eval();
|
2010-07-15 00:48:09 +00:00
|
|
|
expect(getStyle(element)).toEqual({color:'red'});
|
2010-07-15 00:07:23 +00:00
|
|
|
});
|
2010-07-14 19:08:55 +00:00
|
|
|
});
|
|
|
|
|
|
2010-07-14 19:08:55 +00:00
|
|
|
it('should silently ignore undefined ng:style', function() {
|
|
|
|
|
var scope = compile('<div ng:style="myStyle"></div>');
|
|
|
|
|
scope.$eval();
|
|
|
|
|
expect(element.hasClass('ng-exception')).toBeFalsy();
|
|
|
|
|
});
|
|
|
|
|
|
2010-07-02 22:39:47 +00:00
|
|
|
it('should ng:show', function(){
|
|
|
|
|
var scope = compile('<div ng:hide="hide"></div>');
|
2010-03-26 05:03:11 +00:00
|
|
|
scope.$eval();
|
2010-04-08 22:05:05 +00:00
|
|
|
expect(isCssVisible(scope.$element)).toEqual(true);
|
2010-03-26 05:03:11 +00:00
|
|
|
scope.$set('hide', true);
|
|
|
|
|
scope.$eval();
|
2010-04-08 22:05:05 +00:00
|
|
|
expect(isCssVisible(scope.$element)).toEqual(false);
|
2010-03-23 04:29:57 +00:00
|
|
|
});
|
|
|
|
|
|
2010-07-02 22:39:47 +00:00
|
|
|
it('should ng:hide', function(){
|
|
|
|
|
var scope = compile('<div ng:show="show"></div>');
|
2010-03-26 05:03:11 +00:00
|
|
|
scope.$eval();
|
2010-04-08 22:05:05 +00:00
|
|
|
expect(isCssVisible(scope.$element)).toEqual(false);
|
2010-03-26 05:03:11 +00:00
|
|
|
scope.$set('show', true);
|
|
|
|
|
scope.$eval();
|
2010-04-08 22:05:05 +00:00
|
|
|
expect(isCssVisible(scope.$element)).toEqual(true);
|
2010-03-23 04:29:57 +00:00
|
|
|
});
|
2010-04-06 04:26:52 +00:00
|
|
|
|
2010-07-02 22:39:47 +00:00
|
|
|
describe('ng:controller', function(){
|
2010-10-07 00:01:41 +00:00
|
|
|
|
|
|
|
|
var temp;
|
|
|
|
|
|
|
|
|
|
beforeEach(function(){
|
|
|
|
|
temp = window.temp = {};
|
|
|
|
|
temp.Greeter = function(){
|
|
|
|
|
this.$root.greeter = this;
|
2010-04-21 19:50:05 +00:00
|
|
|
this.greeting = 'hello';
|
2010-10-07 00:01:41 +00:00
|
|
|
this.suffix = '!';
|
2010-04-21 19:50:05 +00:00
|
|
|
};
|
2010-10-07 00:01:41 +00:00
|
|
|
temp.Greeter.prototype = {
|
2010-04-21 19:50:05 +00:00
|
|
|
greet: function(name) {
|
|
|
|
|
return this.greeting + ' ' + name + this.suffix;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
});
|
2010-10-07 00:01:41 +00:00
|
|
|
|
|
|
|
|
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(){
|
Introduced injector and $new to scope, and injection into link methods and controllers
- added angular.injector(scope, services, instanceCache) which returns inject
- inject method can return, instance, or call function which have $inject
property
- initialize services with $creation=[eager|eager-publish] this means that
only some of the services are now globally accessible
- upgraded $become on scope to use injector hence respect the $inject property
for injection
- $become should not be run multiple times and will most likely be removed
in future version
- added $new on scope to create a child scope
- $inject is respected on constructor function
- simplified scopes so that they no longer have separate __proto__ for
parent, api, behavior and instance this should speed up execution since
scope will now create one __proto__ chain per scope (not three).
BACKWARD COMPATIBILITY WARNING:
- services now need to have $inject instead of inject property for proper
injection this breaks backward compatibility
- not all services are now published into root scope
(only: $location, $cookie, $window)
- if you have widget/directive which uses services on scope
(such as this.$xhr), you will now have to inject that service in
(as it is not published on the root scope anymore)
2010-10-09 00:30:13 +00:00
|
|
|
temp.ChildGreeter = function(){
|
2010-10-07 00:01:41 +00:00
|
|
|
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!');
|
|
|
|
|
});
|
|
|
|
|
|
2010-04-06 04:26:52 +00:00
|
|
|
});
|
2010-04-12 23:24:28 +00:00
|
|
|
|
2010-07-02 22:39:47 +00:00
|
|
|
it('should eval things according to ng:eval-order', function(){
|
2010-04-12 23:24:28 +00:00
|
|
|
var scope = compile(
|
2010-07-02 22:39:47 +00:00
|
|
|
'<div ng:init="log=\'\'">' +
|
2010-04-12 23:24:28 +00:00
|
|
|
'{{log = log + \'e\'}}' +
|
2010-07-02 22:39:47 +00:00
|
|
|
'<span ng:eval-order="first" ng:eval="log = log + \'a\'">' +
|
2010-04-12 23:24:28 +00:00
|
|
|
'{{log = log + \'b\'}}' +
|
|
|
|
|
'<span src="{{log = log + \'c\'}}"></span>' +
|
|
|
|
|
'<span bind-template="{{log = log + \'d\'}}"></span>' +
|
|
|
|
|
'</span>' +
|
|
|
|
|
'</div>');
|
|
|
|
|
expect(scope.log).toEqual('abcde');
|
|
|
|
|
});
|
|
|
|
|
|
2010-03-22 20:58:04 +00:00
|
|
|
});
|