');
- scope = angular.compile(template)();
- scope.$digest();
+ it('should link to existing node and create scope', inject(function($rootScope) {
+ var template = angular.element('
{{greeting = "hello world"}}
');
+ angular.compile(template)($rootScope);
+ $rootScope.$digest();
expect(template.text()).toEqual('hello world');
- expect(scope.greeting).toEqual('hello world');
- });
+ expect($rootScope.greeting).toEqual('hello world');
+ }));
- it('should link to existing node and given scope', function() {
- scope = angular.scope();
- template = angular.element('
{{greeting = "hello world"}}
');
- angular.compile(template)(scope);
- scope.$digest();
+ it('should link to existing node and given scope', inject(function($rootScope) {
+ var template = angular.element('
{{greeting = "hello world"}}
');
+ angular.compile(template)($rootScope);
+ $rootScope.$digest();
expect(template.text()).toEqual('hello world');
- expect(scope).toEqual(scope);
- });
+ }));
- it('should link to new node and given scope', function() {
- scope = angular.scope();
- template = jqLite('
{{greeting = "hello world"}}
');
+ it('should link to new node and given scope', inject(function($rootScope) {
+ var template = jqLite('
{{greeting = "hello world"}}
');
var templateFn = angular.compile(template);
var templateClone = template.clone();
- templateFn(scope, function(clone){
+ var element = templateFn($rootScope, function(clone){
templateClone = clone;
});
- scope.$digest();
+ $rootScope.$digest();
expect(template.text()).toEqual('');
- expect(scope.$element.text()).toEqual('hello world');
- expect(scope.$element).toEqual(templateClone);
- expect(scope.greeting).toEqual('hello world');
- });
-
- it('should link to cloned node and create scope', function() {
- scope = angular.scope();
- template = jqLite('
{{greeting = "hello world"}}
');
- angular.compile(template)(scope, noop).$digest();
+ expect(element.text()).toEqual('hello world');
+ expect(element).toEqual(templateClone);
+ expect($rootScope.greeting).toEqual('hello world');
+ }));
+
+ it('should link to cloned node and create scope', inject(function($rootScope) {
+ var template = jqLite('
');
- });
+ }));
- it('should set element element', function() {
+ it('should set element element', inject(function($rootScope) {
angularFilter.myElement = function() {
return jqLite('hello');
};
- var scope = compile('');
- scope.$digest();
+ var element = angular.compile('')($rootScope);
+ $rootScope.$digest();
expect(lowercase(element.html())).toEqual('hello');
- });
+ }));
- it('should have $element set to current bind element', function() {
+ it('should have $element set to current bind element', inject(function($rootScope) {
angularFilter.myFilter = function() {
this.$element.addClass("filter");
return 'HELLO';
};
- var scope = compile('
');
- scope.$digest();
- expect(fromJson(scope.$element.text())).toEqual({key:'value'});
- });
+ it('should render object as JSON ignore $$', inject(function($rootScope) {
+ var element = angular.compile('
{{ {key:"value", $$key:"hide"} }}
')($rootScope);
+ $rootScope.$digest();
+ expect(fromJson(element.text())).toEqual({key:'value'});
+ }));
});
describe('ng:bind-attr', function() {
- it('should bind attributes', function() {
- var scope = compile('');
- scope.$digest();
+ it('should bind attributes', inject(function($rootScope) {
+ var element = angular.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', function() {
- var scope = compile('');
- scope.$digest();
+ it('should not pretty print JSON in attributes', inject(function($rootScope) {
+ var element = angular.compile('')($rootScope);
+ $rootScope.$digest();
expect(element.attr('alt')).toEqual('{"a":1}');
- });
+ }));
});
- it('should remove special attributes on false', function() {
- var scope = compile('');
- var input = scope.$element[0];
+ it('should remove special attributes on false', inject(function($rootScope) {
+ var element = angular.compile('')($rootScope);
+ var input = 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.$digest();
+ $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', function() {
- var scope = compile('');
- scope.$digest();
- expect(scope.clicked).toBeFalsy();
+ it('should get called on a click', inject(function($rootScope) {
+ var element = angular.compile('')($rootScope);
+ $rootScope.$digest();
+ expect($rootScope.clicked).toBeFalsy();
browserTrigger(element, 'click');
- expect(scope.clicked).toEqual(true);
- });
+ expect($rootScope.clicked).toEqual(true);
+ }));
- it('should stop event propagation', function() {
- var scope = compile('
');
- scope.$digest();
- expect(scope.outer).not.toBeDefined();
- expect(scope.inner).not.toBeDefined();
+ it('should stop event propagation', inject(function($rootScope) {
+ var element = angular.compile('
')($rootScope);
+ $rootScope.$digest();
+ expect($rootScope.outer).not.toBeDefined();
+ expect($rootScope.inner).not.toBeDefined();
var innerDiv = element.children()[0];
browserTrigger(innerDiv, 'click');
- expect(scope.outer).not.toBeDefined();
- expect(scope.inner).toEqual(true);
- });
+ expect($rootScope.outer).not.toBeDefined();
+ expect($rootScope.inner).toEqual(true);
+ }));
});
describe('ng:submit', function() {
- it('should get called on form submit', function() {
- var scope = compile('');
- scope.$digest();
- expect(scope.submitted).not.toBeDefined();
+ it('should get called on form submit', inject(function($rootScope) {
+ var element = angular.compile('')($rootScope);
+ $rootScope.$digest();
+ expect($rootScope.submitted).not.toBeDefined();
browserTrigger(element.children()[0]);
- expect(scope.submitted).toEqual(true);
- });
+ expect($rootScope.submitted).toEqual(true);
+ }));
});
describe('ng:class', function() {
- it('should add new and remove old classes dynamically', function() {
- var scope = compile('');
- scope.dynClass = 'A';
- scope.$digest();
+ it('should add new and remove old classes dynamically', inject(function($rootScope) {
+ var element = angular.compile('')($rootScope);
+ $rootScope.dynClass = 'A';
+ $rootScope.$digest();
expect(element.hasClass('existing')).toBe(true);
expect(element.hasClass('A')).toBe(true);
- scope.dynClass = 'B';
- scope.$digest();
+ $rootScope.dynClass = 'B';
+ $rootScope.$digest();
expect(element.hasClass('existing')).toBe(true);
expect(element.hasClass('A')).toBe(false);
expect(element.hasClass('B')).toBe(true);
- delete scope.dynClass;
- scope.$digest();
+ 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', function() {
- var scope = compile('');
- scope.$digest();
+ it('should support adding multiple classes via an array', inject(function($rootScope) {
+ var element = angular.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 via a space delimited string', function() {
- var scope = compile('');
- scope.$digest();
+ it('should support adding multiple classes via a space delimited string', inject(function($rootScope) {
+ var element = angular.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', function() {
- var scope = compile('');
- scope.dynClass = 'A';
- scope.$digest();
+ it('should preserve class added post compilation with pre-existing classes', inject(function($rootScope) {
+ var element = angular.compile('')($rootScope);
+ $rootScope.dynClass = 'A';
+ $rootScope.$digest();
expect(element.hasClass('existing')).toBe(true);
// add extra class, change model and eval
element.addClass('newClass');
- scope.dynClass = 'B';
- scope.$digest();
+ $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"', function() {
- var scope = compile('');
- scope.dynClass = 'A';
- scope.$digest();
+ it('should preserve class added post compilation without pre-existing classes"', inject(function($rootScope) {
+ var element = angular.compile('')($rootScope);
+ $rootScope.dynClass = 'A';
+ $rootScope.$digest();
expect(element.hasClass('A')).toBe(true);
// add extra class, change model and eval
element.addClass('newClass');
- scope.dynClass = 'B';
- scope.$digest();
+ $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"', function() {
- var scope = compile('');
- scope.dynCls = 'panel';
- scope.$digest();
- scope.dynCls = 'foo';
- scope.$digest();
+ it('should preserve other classes with similar name"', inject(function($rootScope) {
+ var element = angular.compile('')($rootScope);
+ $rootScope.dynCls = 'panel';
+ $rootScope.$digest();
+ $rootScope.dynCls = 'foo';
+ $rootScope.$digest();
expect(element[0].className).toBe('ui-panel ui-selected ng-directive foo');
- });
+ }));
- it('should not add duplicate classes', function() {
- var scope = compile('');
- scope.dynCls = 'panel';
- scope.$digest();
+ it('should not add duplicate classes', inject(function($rootScope) {
+ var element = angular.compile('')($rootScope);
+ $rootScope.dynCls = 'panel';
+ $rootScope.$digest();
expect(element[0].className).toBe('panel bar ng-directive');
- });
+ }));
- it('should remove classes even if it was specified via class attribute', function() {
- var scope = compile('');
- scope.dynCls = 'panel';
- scope.$digest();
- scope.dynCls = 'window';
- scope.$digest();
+ it('should remove classes even if it was specified via class attribute', inject(function($rootScope) {
+ var element = angular.compile('')($rootScope);
+ $rootScope.dynCls = 'panel';
+ $rootScope.$digest();
+ $rootScope.dynCls = 'window';
+ $rootScope.$digest();
expect(element[0].className).toBe('bar ng-directive window');
- });
+ }));
- it('should remove classes even if they were added by another code', function() {
- var scope = compile('');
- scope.dynCls = 'foo';
- scope.$digest();
+ it('should remove classes even if they were added by another code', inject(function($rootScope) {
+ var element = angular.compile('')($rootScope);
+ $rootScope.dynCls = 'foo';
+ $rootScope.$digest();
element.addClass('foo');
- scope.dynCls = '';
- scope.$digest();
+ $rootScope.dynCls = '';
+ $rootScope.$digest();
expect(element[0].className).toBe('ng-directive');
- });
+ }));
- it('should convert undefined and null values to an empty string', function() {
- var scope = compile('');
- scope.dynCls = [undefined, null];
- scope.$digest();
+ it('should convert undefined and null values to an empty string', inject(function($rootScope) {
+ var element = angular.compile('')($rootScope);
+ $rootScope.dynCls = [undefined, null];
+ $rootScope.$digest();
expect(element[0].className).toBe('ng-directive');
- });
+ }));
});
- it('should ng:class odd/even', function() {
- var scope = compile('
');
- scope.$digest();
+ it('should ng:class odd/even', inject(function($rootScope) {
+ var element = angular.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', function() {
- var scope = compile('
' +
- '' +
- '
');
- scope.$apply();
+ it('should allow both ng:class and ng:class-odd/even on the same element', inject(function($rootScope) {
+ var element = angular.compile('
' +
+ '' +
+ '
')($rootScope);
+ $rootScope.$apply();
var e1 = jqLite(element[0].childNodes[1]);
var e2 = jqLite(element[0].childNodes[2]);
@@ -334,15 +321,15 @@ describe("directive", function() {
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', function() {
- var scope = compile('
' +
- '' +
- '
');
- scope.$apply();
+ it('should allow both ng:class and ng:class-odd/even with multiple classes', inject(function($rootScope) {
+ var element = angular.compile('
' +
+ '' +
+ '
')($rootScope);
+ $rootScope.$apply();
var e1 = jqLite(element[0].childNodes[1]);
var e2 = jqLite(element[0].childNodes[2]);
@@ -359,29 +346,29 @@ describe("directive", function() {
expect(e2.hasClass('F')).toBeTruthy();
expect(e2.hasClass('C')).toBeFalsy();
expect(e2.hasClass('D')).toBeFalsy();
- });
+ }));
describe('ng:style', function() {
- it('should set', function() {
- var scope = compile('');
- scope.$digest();
+ it('should set', inject(function($rootScope) {
+ var element = angular.compile('')($rootScope);
+ $rootScope.$digest();
expect(element.css('height')).toEqual('40px');
- });
+ }));
- it('should silently ignore undefined style', function() {
- var scope = compile('');
- scope.$digest();
+ it('should silently ignore undefined style', inject(function($rootScope) {
+ var element = angular.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;
+ var scope, preCompStyle, preCompVal, postCompStyle, postCompVal, element;
- beforeEach(function() {
+ beforeEach(inject(function($rootScope) {
preCompStyle = 'width';
preCompVal = '300px';
postCompStyle = 'height';
@@ -389,11 +376,12 @@ describe("directive", function() {
element = jqLite('');
element.css(preCompStyle, preCompVal);
jqLite(document.body).append(element);
- scope = compile(element);
+ angular.compile(element)($rootScope);
+ scope = $rootScope;
scope.styleObj = {'margin-top': '44px'};
scope.$apply();
element.css(postCompStyle, postCompVal);
- });
+ }));
afterEach(function() {
element.remove();
@@ -443,39 +431,36 @@ describe("directive", function() {
describe('ng:show', function() {
- it('should show and hide an element', function() {
- var element = jqLite(''),
- scope = compile(element);
-
- scope.$digest();
+ it('should show and hide an element', inject(function($rootScope) {
+ var element = jqLite('');
+ var element = angular.compile(element)($rootScope);
+ $rootScope.$digest();
expect(isCssVisible(element)).toEqual(false);
- scope.exp = true;
- scope.$digest();
+ $rootScope.exp = true;
+ $rootScope.$digest();
expect(isCssVisible(element)).toEqual(true);
- });
-
+ }));
- it('should make hidden element visible', function() {
- var element = jqLite(''),
- scope = compile(element);
+ it('should make hidden element visible', inject(function($rootScope) {
+ var element = jqLite('');
+ var element = angular.compile(element)($rootScope);
expect(isCssVisible(element)).toBe(false);
- scope.exp = true;
- scope.$digest();
+ $rootScope.exp = true;
+ $rootScope.$digest();
expect(isCssVisible(element)).toBe(true);
- });
+ }));
});
describe('ng:hide', function() {
- it('should hide an element', function() {
- var element = jqLite(''),
- scope = compile(element);
-
+ it('should hide an element', inject(function($rootScope) {
+ var element = jqLite('');
+ var element = angular.compile(element)($rootScope);
expect(isCssVisible(element)).toBe(true);
- scope.exp = true;
- scope.$digest();
+ $rootScope.exp = true;
+ $rootScope.$digest();
expect(isCssVisible(element)).toBe(false);
- });
+ }));
});
describe('ng:controller', function() {
@@ -500,13 +485,13 @@ describe("directive", function() {
window.temp = undefined;
});
- it('should bind', function() {
- var scope = compile('');
- expect(scope.greeter.greeting).toEqual('hello');
- expect(scope.greeter.greet('misko')).toEqual('hello misko!');
- });
+ it('should bind', inject(function($rootScope) {
+ var element = angular.compile('')($rootScope);
+ expect($rootScope.greeter.greeting).toEqual('hello');
+ expect($rootScope.greeter.greet('misko')).toEqual('hello misko!');
+ }));
- it('should support nested controllers', function() {
+ it('should support nested controllers', inject(function($rootScope) {
temp.ChildGreeter = function() {
this.greeting = 'hey';
this.$root.childGreeter = this;
@@ -516,37 +501,37 @@ describe("directive", function() {
return this.greeting + ' dude' + this.suffix;
}
};
- var scope = compile('
')($rootScope);
a = {};
b = {};
c = {};
d = {};
- scope.items = [a, b, c];
- scope.$digest();
+ $rootScope.items = [a, b, c];
+ $rootScope.$digest();
lis = element.find('li');
- });
+ }));
- it('should preserve the order of elements', function() {
- scope.items = [a, c, d];
- scope.$digest();
+ it('should preserve the order of elements', inject(function($rootScope) {
+ $rootScope.items = [a, c, d];
+ $rootScope.$digest();
var newElements = element.find('li');
expect(newElements[0]).toEqual(lis[0]);
expect(newElements[1]).toEqual(lis[2]);
expect(newElements[2]).not.toEqual(lis[1]);
- });
+ }));
- it('should support duplicates', function() {
- scope.items = [a, a, b, c];
- scope.$digest();
+ it('should support duplicates', inject(function($rootScope) {
+ $rootScope.items = [a, a, b, c];
+ $rootScope.$digest();
var newElements = element.find('li');
expect(newElements[0]).toEqual(lis[0]);
expect(newElements[1]).not.toEqual(lis[0]);
@@ -371,170 +367,163 @@ describe("widget", function() {
expect(newElements[3]).toEqual(lis[2]);
lis = newElements;
- scope.$digest();
+ $rootScope.$digest();
newElements = element.find('li');
expect(newElements[0]).toEqual(lis[0]);
expect(newElements[1]).toEqual(lis[1]);
expect(newElements[2]).toEqual(lis[2]);
expect(newElements[3]).toEqual(lis[3]);
- scope.$digest();
+ $rootScope.$digest();
newElements = element.find('li');
expect(newElements[0]).toEqual(lis[0]);
expect(newElements[1]).toEqual(lis[1]);
expect(newElements[2]).toEqual(lis[2]);
expect(newElements[3]).toEqual(lis[3]);
- });
+ }));
- it('should remove last item when one duplicate instance is removed', function() {
- scope.items = [a, a, a];
- scope.$digest();
+ it('should remove last item when one duplicate instance is removed', inject(function($rootScope) {
+ $rootScope.items = [a, a, a];
+ $rootScope.$digest();
lis = element.find('li');
- scope.items = [a, a];
- scope.$digest();
+ $rootScope.items = [a, a];
+ $rootScope.$digest();
var newElements = element.find('li');
expect(newElements.length).toEqual(2);
expect(newElements[0]).toEqual(lis[0]);
expect(newElements[1]).toEqual(lis[1]);
- });
+ }));
- it('should reverse items when the collection is reversed', function() {
- scope.items = [a, b, c];
- scope.$digest();
+ it('should reverse items when the collection is reversed', inject(function($rootScope) {
+ $rootScope.items = [a, b, c];
+ $rootScope.$digest();
lis = element.find('li');
- scope.items = [c, b, a];
- scope.$digest();
+ $rootScope.items = [c, b, a];
+ $rootScope.$digest();
var newElements = element.find('li');
expect(newElements.length).toEqual(3);
expect(newElements[0]).toEqual(lis[2]);
expect(newElements[1]).toEqual(lis[1]);
expect(newElements[2]).toEqual(lis[0]);
- });
+ }));
});
- });
+ }));
describe('@ng:non-bindable', function() {
- it('should prevent compilation of the owning element and its children', function() {
- var scope = compile('
');
- scope.name = 'misko';
- scope.$digest();
+ it('should prevent compilation of the owning element and its children', inject(function($rootScope) {
+ var element = angular.compile('
')($rootScope);
+ $rootScope.name = 'misko';
+ $rootScope.$digest();
expect(element.text()).toEqual('');
- });
+ }));
});
describe('ng:view', function() {
- var rootScope, $route, $location, $browser;
-
- beforeEach(function() {
- rootScope = angular.compile('')();
- $route = rootScope.$service('$route');
- $location = rootScope.$service('$location');
- $browser = rootScope.$service('$browser');
- });
-
- afterEach(function() {
- dealoc(rootScope);
- });
+ var element;
+ beforeEach(inject(function($rootScope) {
+ element = angular.compile('')($rootScope);
+ }));
- it('should do nothing when no routes are defined', function() {
+ it('should do nothing when no routes are defined', inject(function($rootScope, $location) {
$location.path('/unknown');
- rootScope.$digest();
- expect(rootScope.$element.text()).toEqual('');
- });
+ $rootScope.$digest();
+ expect(element.text()).toEqual('');
+ }));
- it('should load content via xhr when route changes', function() {
+ it('should load content via xhr when route changes', inject(function($rootScope, $browser, $location, $route) {
$route.when('/foo', {template: 'myUrl1'});
$route.when('/bar', {template: 'myUrl2'});
- expect(rootScope.$element.text()).toEqual('');
+ expect(element.text()).toEqual('');
$location.path('/foo');
$browser.xhr.expectGET('myUrl1').respond('
{{1+3}}
');
- rootScope.$digest();
+ $rootScope.$digest();
$browser.xhr.flush();
- expect(rootScope.$element.text()).toEqual('4');
+ expect(element.text()).toEqual('4');
$location.path('/bar');
$browser.xhr.expectGET('myUrl2').respond('angular is da best');
- rootScope.$digest();
+ $rootScope.$digest();
$browser.xhr.flush();
- expect(rootScope.$element.text()).toEqual('angular is da best');
- });
+ expect(element.text()).toEqual('angular is da best');
+ }));
- it('should remove all content when location changes to an unknown route', function() {
+ it('should remove all content when location changes to an unknown route',
+ inject(function($rootScope, $location, $browser, $route) {
$route.when('/foo', {template: 'myUrl1'});
$location.path('/foo');
$browser.xhr.expectGET('myUrl1').respond('