')($rootScope);
$rootScope.$digest();
expect(fromJson(element.text())).toEqual({key:'value'});
}));
});
describe('ng:bind-attr', function() {
it('should bind attributes', inject(function($rootScope, $compile) {
element = $compile('')($rootScope);
$rootScope.$digest();
expect(element.attr('src')).toEqual('http://localhost/mysrc');
expect(element.attr('alt')).toEqual('myalt');
}));
it('should not pretty print JSON in attributes', inject(function($rootScope, $compile) {
element = $compile('')($rootScope);
$rootScope.$digest();
expect(element.attr('alt')).toEqual('{"a":1}');
}));
it('should remove special attributes on false', inject(function($rootScope, $compile) {
element = $compile('')($rootScope);
var input = element[0];
expect(input.disabled).toEqual(false);
expect(input.readOnly).toEqual(false);
expect(input.checked).toEqual(false);
$rootScope.disabled = true;
$rootScope.readonly = true;
$rootScope.checked = true;
$rootScope.$digest();
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', inject(function($rootScope, $compile) {
element = $compile('')($rootScope);
$rootScope.$digest();
expect($rootScope.clicked).toBeFalsy();
browserTrigger(element, 'click');
expect($rootScope.clicked).toEqual(true);
}));
it('should stop event propagation', inject(function($rootScope, $compile) {
element = $compile('
')($rootScope);
$rootScope.$digest();
expect($rootScope.outer).not.toBeDefined();
expect($rootScope.inner).not.toBeDefined();
browserTrigger(element.find('div'), 'click');
expect($rootScope.outer).not.toBeDefined();
expect($rootScope.inner).toEqual(true);
}));
});
describe('ng:submit', function() {
it('should get called on form submit', inject(function($rootScope, $compile) {
element = $compile('')($rootScope);
$rootScope.$digest();
expect($rootScope.submitted).not.toBeDefined();
browserTrigger(element.children()[0]);
expect($rootScope.submitted).toEqual(true);
}));
});
describe('ng:class', function() {
it('should add new and remove old classes dynamically', inject(function($rootScope, $compile) {
element = $compile('')($rootScope);
$rootScope.dynClass = 'A';
$rootScope.$digest();
expect(element.hasClass('existing')).toBe(true);
expect(element.hasClass('A')).toBe(true);
$rootScope.dynClass = 'B';
$rootScope.$digest();
expect(element.hasClass('existing')).toBe(true);
expect(element.hasClass('A')).toBe(false);
expect(element.hasClass('B')).toBe(true);
delete $rootScope.dynClass;
$rootScope.$digest();
expect(element.hasClass('existing')).toBe(true);
expect(element.hasClass('A')).toBe(false);
expect(element.hasClass('B')).toBe(false);
}));
it('should support adding multiple classes via an array', inject(function($rootScope, $compile) {
element = $compile('')($rootScope);
$rootScope.$digest();
expect(element.hasClass('existing')).toBeTruthy();
expect(element.hasClass('A')).toBeTruthy();
expect(element.hasClass('B')).toBeTruthy();
}));
it('should support adding multiple classes conditionally via a map of class names to boolean' +
'expressions', inject(function($rootScope, $compile) {
var element = $compile(
'
' +
'
')($rootScope);
$rootScope.conditionA = true;
$rootScope.$digest();
expect(element.hasClass('existing')).toBeTruthy();
expect(element.hasClass('A')).toBeTruthy();
expect(element.hasClass('B')).toBeFalsy();
expect(element.hasClass('AnotB')).toBeTruthy();
$rootScope.conditionB = function() { return true };
$rootScope.$digest();
expect(element.hasClass('existing')).toBeTruthy();
expect(element.hasClass('A')).toBeTruthy();
expect(element.hasClass('B')).toBeTruthy();
expect(element.hasClass('AnotB')).toBeFalsy();
}));
it('should support adding multiple classes via a space delimited string', inject(function($rootScope, $compile) {
element = $compile('')($rootScope);
$rootScope.$digest();
expect(element.hasClass('existing')).toBeTruthy();
expect(element.hasClass('A')).toBeTruthy();
expect(element.hasClass('B')).toBeTruthy();
}));
it('should preserve class added post compilation with pre-existing classes', inject(function($rootScope, $compile) {
element = $compile('')($rootScope);
$rootScope.dynClass = 'A';
$rootScope.$digest();
expect(element.hasClass('existing')).toBe(true);
// add extra class, change model and eval
element.addClass('newClass');
$rootScope.dynClass = 'B';
$rootScope.$digest();
expect(element.hasClass('existing')).toBe(true);
expect(element.hasClass('B')).toBe(true);
expect(element.hasClass('newClass')).toBe(true);
}));
it('should preserve class added post compilation without pre-existing classes"', inject(function($rootScope, $compile) {
element = $compile('')($rootScope);
$rootScope.dynClass = 'A';
$rootScope.$digest();
expect(element.hasClass('A')).toBe(true);
// add extra class, change model and eval
element.addClass('newClass');
$rootScope.dynClass = 'B';
$rootScope.$digest();
expect(element.hasClass('B')).toBe(true);
expect(element.hasClass('newClass')).toBe(true);
}));
it('should preserve other classes with similar name"', inject(function($rootScope, $compile) {
element = $compile('')($rootScope);
$rootScope.dynCls = 'panel';
$rootScope.$digest();
$rootScope.dynCls = 'foo';
$rootScope.$digest();
expect(element[0].className).toBe('ui-panel ui-selected foo');
}));
it('should not add duplicate classes', inject(function($rootScope, $compile) {
element = $compile('')($rootScope);
$rootScope.dynCls = 'panel';
$rootScope.$digest();
expect(element[0].className).toBe('panel bar');
}));
it('should remove classes even if it was specified via class attribute', inject(function($rootScope, $compile) {
element = $compile('')($rootScope);
$rootScope.dynCls = 'panel';
$rootScope.$digest();
$rootScope.dynCls = 'window';
$rootScope.$digest();
expect(element[0].className).toBe('bar window');
}));
it('should remove classes even if they were added by another code', inject(function($rootScope, $compile) {
element = $compile('')($rootScope);
$rootScope.dynCls = 'foo';
$rootScope.$digest();
element.addClass('foo');
$rootScope.dynCls = '';
$rootScope.$digest();
}));
it('should convert undefined and null values to an empty string', inject(function($rootScope, $compile) {
element = $compile('')($rootScope);
$rootScope.dynCls = [undefined, null];
$rootScope.$digest();
}));
it('should ng:class odd/even', inject(function($rootScope, $compile) {
element = $compile('
')($rootScope);
$rootScope.$digest();
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();
}));
it('should allow both ng:class and ng:class-odd/even on the same element', inject(function($rootScope, $compile) {
element = $compile('
' +
'' +
'
')($rootScope);
$rootScope.$apply();
var e1 = jqLite(element[0].childNodes[1]);
var e2 = jqLite(element[0].childNodes[2]);
expect(e1.hasClass('plainClass')).toBeTruthy();
expect(e1.hasClass('odd')).toBeTruthy();
expect(e1.hasClass('even')).toBeFalsy();
expect(e2.hasClass('plainClass')).toBeTruthy();
expect(e2.hasClass('even')).toBeTruthy();
expect(e2.hasClass('odd')).toBeFalsy();
}));
it('should allow both ng:class and ng:class-odd/even with multiple classes', inject(function($rootScope, $compile) {
element = $compile('
' +
'' +
'
')($rootScope);
$rootScope.$apply();
var e1 = jqLite(element[0].childNodes[1]);
var e2 = jqLite(element[0].childNodes[2]);
expect(e1.hasClass('A')).toBeTruthy();
expect(e1.hasClass('B')).toBeTruthy();
expect(e1.hasClass('C')).toBeTruthy();
expect(e1.hasClass('D')).toBeTruthy();
expect(e1.hasClass('E')).toBeFalsy();
expect(e1.hasClass('F')).toBeFalsy();
expect(e2.hasClass('A')).toBeTruthy();
expect(e2.hasClass('B')).toBeTruthy();
expect(e2.hasClass('E')).toBeTruthy();
expect(e2.hasClass('F')).toBeTruthy();
expect(e2.hasClass('C')).toBeFalsy();
expect(e2.hasClass('D')).toBeFalsy();
}));
});
describe('ng:style', function() {
it('should set', inject(function($rootScope, $compile) {
element = $compile('')($rootScope);
$rootScope.$digest();
expect(element.css('height')).toEqual('40px');
}));
it('should silently ignore undefined style', inject(function($rootScope, $compile) {
element = $compile('')($rootScope);
$rootScope.$digest();
expect(element.hasClass('ng-exception')).toBeFalsy();
}));
describe('preserving styles set before and after compilation', function() {
var scope, preCompStyle, preCompVal, postCompStyle, postCompVal, element;
beforeEach(inject(function($rootScope, $compile) {
preCompStyle = 'width';
preCompVal = '300px';
postCompStyle = 'height';
postCompVal = '100px';
element = jqLite('');
element.css(preCompStyle, preCompVal);
jqLite(document.body).append(element);
$compile(element)($rootScope);
scope = $rootScope;
scope.styleObj = {'margin-top': '44px'};
scope.$apply();
element.css(postCompStyle, postCompVal);
}));
afterEach(function() {
element.remove();
});
it('should not mess up stuff after compilation', function() {
element.css('margin', '44px');
expect(element.css(preCompStyle)).toBe(preCompVal);
expect(element.css('margin-top')).toBe('44px');
expect(element.css(postCompStyle)).toBe(postCompVal);
});
it('should not mess up stuff after $apply with no model changes', function() {
element.css('padding-top', '33px');
scope.$apply();
expect(element.css(preCompStyle)).toBe(preCompVal);
expect(element.css('margin-top')).toBe('44px');
expect(element.css(postCompStyle)).toBe(postCompVal);
expect(element.css('padding-top')).toBe('33px');
});
it('should not mess up stuff after $apply with non-colliding model changes', function() {
scope.styleObj = {'padding-top': '99px'};
scope.$apply();
expect(element.css(preCompStyle)).toBe(preCompVal);
expect(element.css('margin-top')).not.toBe('44px');
expect(element.css('padding-top')).toBe('99px');
expect(element.css(postCompStyle)).toBe(postCompVal);
});
it('should overwrite original styles after a colliding model change', function() {
scope.styleObj = {'height': '99px', 'width': '88px'};
scope.$apply();
expect(element.css(preCompStyle)).toBe('88px');
expect(element.css(postCompStyle)).toBe('99px');
scope.styleObj = {};
scope.$apply();
expect(element.css(preCompStyle)).not.toBe('88px');
expect(element.css(postCompStyle)).not.toBe('99px');
});
});
});
describe('ng:show', function() {
it('should show and hide an element', inject(function($rootScope, $compile) {
element = jqLite('');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(isCssVisible(element)).toEqual(false);
$rootScope.exp = true;
$rootScope.$digest();
expect(isCssVisible(element)).toEqual(true);
}));
it('should make hidden element visible', inject(function($rootScope, $compile) {
element = jqLite('');
element = $compile(element)($rootScope);
expect(isCssVisible(element)).toBe(false);
$rootScope.exp = true;
$rootScope.$digest();
expect(isCssVisible(element)).toBe(true);
}));
});
describe('ng:hide', function() {
it('should hide an element', inject(function($rootScope, $compile) {
element = jqLite('');
element = $compile(element)($rootScope);
expect(isCssVisible(element)).toBe(true);
$rootScope.exp = true;
$rootScope.$digest();
expect(isCssVisible(element)).toBe(false);
}));
});
describe('ng:controller', function() {
var element;
beforeEach(inject(function($window) {
$window.Greeter = function($scope) {
// private stuff (not exported to scope)
this.prefix = 'Hello ';
// public stuff (exported to scope)
var ctrl = this;
$scope.name = 'Misko';
$scope.greet = function(name) {
return ctrl.prefix + name + ctrl.suffix;
};
$scope.protoGreet = bind(this, this.protoGreet);
};
$window.Greeter.prototype = {
suffix: '!',
protoGreet: function(name) {
return this.prefix + name + this.suffix;
}
};
$window.Child = function($scope) {
$scope.name = 'Adam';
};
}));
afterEach(function() {
dealoc(element);
});
it('should instantiate controller and bind methods', inject(function($compile, $rootScope) {
element = $compile('
')($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('Hello Adam!');
dealoc(element);
element = $compile('
{{protoGreet(name)}}
')($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('Hello Adam!');
}));
it('should instantiate controller defined on scope', inject(function($compile, $rootScope) {
$rootScope.Greeter = function($scope) {
$scope.name = 'Vojta';
};
element = $compile('
{{name}}
')($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('Vojta');
}));
});
describe('ng:cloak', function() {
it('should get removed when an element is compiled', inject(function($rootScope, $compile) {
element = jqLite('');
expect(element.attr('ng:cloak')).toBe('');
$compile(element);
expect(element.attr('ng:cloak')).toBeUndefined();
}));
it('should remove ng-cloak class from a compiled element', inject(function($rootScope, $compile) {
element = jqLite('');
expect(element.hasClass('foo')).toBe(true);
expect(element.hasClass('ng-cloak')).toBe(true);
expect(element.hasClass('bar')).toBe(true);
$compile(element);
expect(element.hasClass('foo')).toBe(true);
expect(element.hasClass('ng-cloak')).toBe(false);
expect(element.hasClass('bar')).toBe(true);
}));
});
});