angular.js/test/directivesSpec.js

180 lines
5.7 KiB
JavaScript
Raw Normal View History

2010-03-22 20:58:04 +00:00
describe("directives", function(){
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-04-05 21:09:25 +00:00
if (model && model.$element) model.$element.remove();
expect(size(jqCache)).toEqual(0);
});
2010-03-22 20:58:04 +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
});
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
});
it('should ng-bind', function() {
var scope = compile('<div ng-bind="a"></div>');
expect(element.text()).toEqual('');
2010-03-26 05:03:11 +00:00
scope.a = 'misko';
scope.$eval();
2010-03-22 20:58:04 +00:00
expect(element.text()).toEqual('misko');
});
2010-03-23 21:57:11 +00:00
it('should ng-bind-template', function() {
var scope = compile('<div ng-bind-template="Hello {{name}}!"></div>');
2010-03-26 05:03:11 +00:00
scope.$set('name', 'Misko');
scope.$eval();
2010-03-23 21:57:11 +00:00
expect(element.text()).toEqual('Hello Misko!');
});
2010-03-22 20:58:04 +00:00
it('should ng-bind-attr', function(){
var scope = compile('<img ng-bind-attr="{src:\'mysrc\', alt:\'myalt\'}"/>');
expect(element.attr('src')).toEqual('mysrc');
expect(element.attr('alt')).toEqual('myalt');
});
it('should ng-non-bindable', function(){
var scope = compile('<div ng-non-bindable><span ng-bind="name"></span></div>');
2010-03-26 05:03:11 +00:00
scope.$set('name', 'misko');
scope.$eval();
2010-03-22 20:58:04 +00:00
expect(element.text()).toEqual('');
});
it('should ng-repeat over array', function(){
var scope = compile('<ul><li ng-repeat="item in items" ng-init="suffix = \';\'" ng-bind="item + suffix"></li></ul>');
2010-03-26 05:03:11 +00:00
scope.$set('items', ['misko', 'shyam']);
scope.$eval();
2010-03-22 20:58:04 +00:00
expect(element.text()).toEqual('misko;shyam;');
2010-03-26 05:03:11 +00:00
scope.$set('items', ['adam', 'kai', 'brad']);
scope.$eval();
2010-03-22 20:58:04 +00:00
expect(element.text()).toEqual('adam;kai;brad;');
2010-03-26 05:03:11 +00:00
scope.$set('items', ['brad']);
scope.$eval();
2010-03-22 20:58:04 +00:00
expect(element.text()).toEqual('brad;');
});
2010-03-22 22:46:34 +00:00
it('should ng-repeat over object', function(){
var scope = compile('<ul><li ng-repeat="(key, value) in items" ng-bind="key + \':\' + value + \';\' "></li></ul>');
2010-03-26 05:03:11 +00:00
scope.$set('items', {misko:'swe', shyam:'set'});
scope.$eval();
2010-03-22 22:46:34 +00:00
expect(element.text()).toEqual('misko:swe;shyam:set;');
});
2010-03-30 21:55:04 +00:00
it('should set ng-repeat to [] if undefinde', function(){
var scope = compile('<ul><li ng-repeat="item in items"></li></ul>');
expect(scope.items).toEqual([]);
});
2010-03-22 22:46:34 +00:00
it('should error on wrong parsing of ng-repeat', function(){
var scope = compile('<ul><li ng-repeat="i dont parse"></li></ul>');
var log = "";
log += element.attr('ng-exception') + ';';
2010-03-26 05:03:11 +00:00
log += element.hasClass('ng-exception') + ';';
2010-03-22 22:46:34 +00:00
expect(log).toEqual("\"Expected ng-repeat in form of 'item in collection' but got 'i dont parse'.\";true;");
});
2010-03-22 23:07:42 +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();
expect(scope.$get('count')).toEqual(0);
2010-03-22 23:07:42 +00:00
2010-03-26 05:03:11 +00:00
scope.$set('i', 0);
scope.$eval();
scope.$eval();
expect(scope.$get('count')).toEqual(1);
2010-03-22 23:07:42 +00:00
});
2010-03-31 20:57:25 +00:00
it('should ng-click', function(){
var scope = compile('<div ng-click="clicked = true"></div>');
2010-03-26 05:03:11 +00:00
scope.$eval();
expect(scope.$get('clicked')).toBeFalsy();
element.click();
2010-03-26 05:03:11 +00:00
expect(scope.$get('clicked')).toEqual(true);
});
2010-03-23 04:29:57 +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();
});
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();
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
});
it('should ng-style', function(){
var scope = compile('<div ng-style="{color:\'red\'}"></div>');
2010-03-26 05:03:11 +00:00
scope.$eval();
2010-03-23 04:29:57 +00:00
expect(element.css('color')).toEqual('red');
});
it('should ng-show', function(){
var scope = compile('<div ng-hide="hide"></div>');
2010-04-08 20:43:40 +00:00
jqLite(document.body).append(scope.$element);
2010-03-26 05:03:11 +00:00
scope.$eval();
2010-04-08 20:43:40 +00:00
expect(isVisible(scope.$element)).toEqual(true);
2010-03-26 05:03:11 +00:00
scope.$set('hide', true);
scope.$eval();
2010-04-08 20:43:40 +00:00
expect(isVisible(scope.$element)).toEqual(false);
2010-03-23 04:29:57 +00:00
});
it('should ng-hide', function(){
var scope = compile('<div ng-show="show"></div>');
2010-04-08 20:43:40 +00:00
jqLite(document.body).append(scope.$element);
2010-03-26 05:03:11 +00:00
scope.$eval();
2010-04-08 20:43:40 +00:00
expect(isVisible(scope.$element)).toEqual(false);
2010-03-26 05:03:11 +00:00
scope.$set('show', true);
scope.$eval();
2010-04-08 20:43:40 +00:00
expect(isVisible(scope.$element)).toEqual(true);
2010-03-23 04:29:57 +00:00
});
2010-04-06 04:26:52 +00:00
it('should ng-controller', function(){
window.Greeter = function(){
this.greeting = 'hello';
};
window.Greeter.prototype = {
2010-04-07 17:17:15 +00:00
init: function(){
this.suffix = '!';
},
2010-04-06 04:26:52 +00:00
greet: function(name) {
2010-04-07 17:17:15 +00:00
return this.greeting + ' ' + name + this.suffix;
2010-04-06 04:26:52 +00:00
}
};
var scope = compile('<div ng-controller="Greeter"></div>');
expect(scope.greeting).toEqual('hello');
2010-04-07 17:17:15 +00:00
expect(scope.greet('misko')).toEqual('hello misko!');
2010-04-06 04:26:52 +00:00
delete window.Greeter;
});
2010-03-22 20:58:04 +00:00
});