describe("directives", function(){
var compile, model, element;
beforeEach(function() {
var compiler = new Compiler(angularTextMarkup, angularAttrMarkup, angularDirective, angularWidget);
compile = function(html) {
element = jqLite(html);
model = compiler.compile(element)(element);
model.$init();
return model;
};
});
afterEach(function() {
if (model && model.$element) model.$element.remove();
expect(size(jqCache)).toEqual(0);
});
it("should ng-init", function() {
var scope = compile('
');
expect(scope.a).toEqual(123);
});
it("should ng-eval", function() {
var scope = compile('');
expect(scope.a).toEqual(1);
scope.$eval();
expect(scope.a).toEqual(2);
});
it('should ng-bind', function() {
var scope = compile('');
expect(element.text()).toEqual('');
scope.a = 'misko';
scope.$eval();
expect(element.text()).toEqual('misko');
});
it('should ng-bind html', function() {
var scope = compile('');
scope.html = 'hello
';
scope.$eval();
expect(element.html()).toEqual('hello
');
});
it('should ng-bind-template', function() {
var scope = compile('');
scope.$set('name', 'Misko');
scope.$eval();
expect(element.text()).toEqual('Hello Misko!');
});
it('should ng-bind-attr', function(){
var scope = compile('
');
expect(element.attr('src')).toEqual('mysrc');
expect(element.attr('alt')).toEqual('myalt');
});
it('should remove special attributes on false', function(){
var scope = compile('');
expect(scope.$element.attr('disabled')).toEqual(null);
expect(scope.$element.attr('readonly')).toEqual(null);
expect(scope.$element.attr('checked')).toEqual(null);
scope.disabled = true;
scope.readonly = true;
scope.checked = true;
scope.$eval();
expect(scope.$element.attr('disabled')).not.toEqual(null);
expect(scope.$element.attr('readonly')).not.toEqual(null);
expect(scope.$element.attr('checked')).not.toEqual(null);
});
it('should ng-non-bindable', function(){
var scope = compile('
');
scope.$set('name', 'misko');
scope.$eval();
expect(element.text()).toEqual('');
});
it('should ng-repeat over array', function(){
var scope = compile('');
scope.$set('items', ['misko', 'shyam']);
scope.$eval();
expect(element.text()).toEqual('misko;shyam;');
scope.$set('items', ['adam', 'kai', 'brad']);
scope.$eval();
expect(element.text()).toEqual('adam;kai;brad;');
scope.$set('items', ['brad']);
scope.$eval();
expect(element.text()).toEqual('brad;');
});
it('should ng-repeat over object', function(){
var scope = compile('');
scope.$set('items', {misko:'swe', shyam:'set'});
scope.$eval();
expect(element.text()).toEqual('misko:swe;shyam:set;');
});
it('should set ng-repeat to [] if undefinde', function(){
var scope = compile('');
expect(scope.items).toEqual([]);
});
it('should error on wrong parsing of ng-repeat', function(){
var scope = compile('');
var log = "";
log += element.attr('ng-exception') + ';';
log += element.hasClass('ng-exception') + ';';
expect(log).toEqual("\"Expected ng-repeat in form of 'item in collection' but got 'i dont parse'.\";true;");
});
it('should ng-watch', function(){
var scope = compile('');
scope.$eval();
scope.$eval();
expect(scope.$get('count')).toEqual(0);
scope.$set('i', 0);
scope.$eval();
scope.$eval();
expect(scope.$get('count')).toEqual(1);
});
it('should ng-click', function(){
var scope = compile('
');
scope.$eval();
expect(scope.$get('clicked')).toBeFalsy();
element.click();
expect(scope.$get('clicked')).toEqual(true);
});
it('should ng-class', function(){
var scope = compile('
');
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('