diff options
Diffstat (limited to 'test/directivesSpec.js')
| -rw-r--r-- | test/directivesSpec.js | 491 |
1 files changed, 238 insertions, 253 deletions
diff --git a/test/directivesSpec.js b/test/directivesSpec.js index e92cb719..eb74d227 100644 --- a/test/directivesSpec.js +++ b/test/directivesSpec.js @@ -2,329 +2,316 @@ describe("directive", function() { - var compile, model, element; - - beforeEach(function() { - compile = function(html) { - element = jqLite(html); - return model = angular.compile(element)(); - }; - }); - - afterEach(function() { - dealoc(model); - }); - - it("should ng:init", function() { - var scope = compile('<div ng:init="a=123"></div>'); - expect(scope.a).toEqual(123); - }); + it("should ng:init", inject(function($rootScope) { + var element = angular.compile('<div ng:init="a=123"></div>')($rootScope); + expect($rootScope.a).toEqual(123); + })); describe('ng:bind', function() { - it('should set text', function() { - var scope = compile('<div ng:bind="a"></div>'); + it('should set text', inject(function($rootScope) { + var element = angular.compile('<div ng:bind="a"></div>')($rootScope); expect(element.text()).toEqual(''); - scope.a = 'misko'; - scope.$digest(); + $rootScope.a = 'misko'; + $rootScope.$digest(); expect(element.hasClass('ng-binding')).toEqual(true); expect(element.text()).toEqual('misko'); - }); + })); - it('should set text to blank if undefined', function() { - var scope = compile('<div ng:bind="a"></div>'); - scope.a = 'misko'; - scope.$digest(); + it('should set text to blank if undefined', inject(function($rootScope) { + var element = angular.compile('<div ng:bind="a"></div>')($rootScope); + $rootScope.a = 'misko'; + $rootScope.$digest(); expect(element.text()).toEqual('misko'); - scope.a = undefined; - scope.$digest(); + $rootScope.a = undefined; + $rootScope.$digest(); expect(element.text()).toEqual(''); - }); + })); - it('should set html', function() { - var scope = compile('<div ng:bind="html|html"></div>'); - scope.html = '<div unknown>hello</div>'; - scope.$digest(); + it('should set html', inject(function($rootScope) { + var element = angular.compile('<div ng:bind="html|html"></div>')($rootScope); + $rootScope.html = '<div unknown>hello</div>'; + $rootScope.$digest(); expect(lowercase(element.html())).toEqual('<div>hello</div>'); - }); + })); - it('should set unsafe html', function() { - var scope = compile('<div ng:bind="html|html:\'unsafe\'"></div>'); - scope.html = '<div onclick="">hello</div>'; - scope.$digest(); + it('should set unsafe html', inject(function($rootScope) { + var element = angular.compile('<div ng:bind="html|html:\'unsafe\'"></div>')($rootScope); + $rootScope.html = '<div onclick="">hello</div>'; + $rootScope.$digest(); expect(lowercase(element.html())).toEqual('<div onclick="">hello</div>'); - }); + })); - it('should set element element', function() { + it('should set element element', inject(function($rootScope) { angularFilter.myElement = function() { return jqLite('<a>hello</a>'); }; - var scope = compile('<div ng:bind="0|myElement"></div>'); - scope.$digest(); + var element = angular.compile('<div ng:bind="0|myElement"></div>')($rootScope); + $rootScope.$digest(); expect(lowercase(element.html())).toEqual('<a>hello</a>'); - }); + })); - 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('<div>before<div ng:bind="0|myFilter"></div>after</div>'); - scope.$digest(); - expect(sortedHtml(scope.$element)).toEqual('<div>before<div class="filter" ng:bind="0|myFilter">HELLO</div>after</div>'); - }); - - - it('should suppress rendering of falsy values', function() { - var scope = compile('<div>{{ null }}{{ undefined }}{{ "" }}-{{ 0 }}{{ false }}</div>'); - scope.$digest(); - expect(scope.$element.text()).toEqual('-0false'); - }); - - it('should render object as JSON ignore $$', function() { - var scope = compile('<div>{{ {key:"value", $$key:"hide"} }}</div>'); - scope.$digest(); - expect(fromJson(scope.$element.text())).toEqual({key:'value'}); - }); + var element = angular.compile('<div>before<div ng:bind="0|myFilter"></div>after</div>')($rootScope); + $rootScope.$digest(); + expect(sortedHtml(element)).toEqual('<div>before<div class="filter" ng:bind="0|myFilter">HELLO</div>after</div>'); + })); + + + it('should suppress rendering of falsy values', inject(function($rootScope) { + var element = angular.compile('<div>{{ null }}{{ undefined }}{{ "" }}-{{ 0 }}{{ false }}</div>')($rootScope); + $rootScope.$digest(); + expect(element.text()).toEqual('-0false'); + })); + + it('should render object as JSON ignore $$', inject(function($rootScope) { + var element = angular.compile('<div>{{ {key:"value", $$key:"hide"} }}</div>')($rootScope); + $rootScope.$digest(); + expect(fromJson(element.text())).toEqual({key:'value'}); + })); }); describe('ng:bind-template', function() { - it('should ng:bind-template', function() { - var scope = compile('<div ng:bind-template="Hello {{name}}!"></div>'); - scope.name = 'Misko'; - scope.$digest(); + it('should ng:bind-template', inject(function($rootScope) { + var element = angular.compile('<div ng:bind-template="Hello {{name}}!"></div>')($rootScope); + $rootScope.name = 'Misko'; + $rootScope.$digest(); expect(element.hasClass('ng-binding')).toEqual(true); expect(element.text()).toEqual('Hello Misko!'); - }); + })); - it('should have $element set to current bind element', function() { + it('should have $element set to current bind element', inject(function($rootScope) { var innerText; angularFilter.myFilter = function(text) { innerText = innerText || this.$element.text(); return text; }; - var scope = compile('<div>before<span ng:bind-template="{{\'HELLO\'|myFilter}}">INNER</span>after</div>'); - scope.$digest(); - expect(scope.$element.text()).toEqual("beforeHELLOafter"); + var element = angular.compile('<div>before<span ng:bind-template="{{\'HELLO\'|myFilter}}">INNER</span>after</div>')($rootScope); + $rootScope.$digest(); + expect(element.text()).toEqual("beforeHELLOafter"); expect(innerText).toEqual('INNER'); - }); + })); - it('should render object as JSON ignore $$', function() { - var scope = compile('<pre>{{ {key:"value", $$key:"hide"} }}</pre>'); - scope.$digest(); - expect(fromJson(scope.$element.text())).toEqual({key:'value'}); - }); + it('should render object as JSON ignore $$', inject(function($rootScope) { + var element = angular.compile('<pre>{{ {key:"value", $$key:"hide"} }}</pre>')($rootScope); + $rootScope.$digest(); + expect(fromJson(element.text())).toEqual({key:'value'}); + })); }); describe('ng:bind-attr', function() { - it('should bind attributes', function() { - var scope = compile('<div ng:bind-attr="{src:\'http://localhost/mysrc\', alt:\'myalt\'}"/>'); - scope.$digest(); + it('should bind attributes', inject(function($rootScope) { + var element = angular.compile('<div ng:bind-attr="{src:\'http://localhost/mysrc\', alt:\'myalt\'}"/>')($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('<img alt="{{ {a:1} }}"/>'); - scope.$digest(); + it('should not pretty print JSON in attributes', inject(function($rootScope) { + var element = angular.compile('<img alt="{{ {a:1} }}"/>')($rootScope); + $rootScope.$digest(); expect(element.attr('alt')).toEqual('{"a":1}'); - }); + })); }); - it('should remove special attributes on false', function() { - var scope = compile('<input ng:bind-attr="{disabled:\'{{disabled}}\', readonly:\'{{readonly}}\', checked:\'{{checked}}\'}"/>'); - var input = scope.$element[0]; + it('should remove special attributes on false', inject(function($rootScope) { + var element = angular.compile('<input ng:bind-attr="{disabled:\'{{disabled}}\', readonly:\'{{readonly}}\', checked:\'{{checked}}\'}"/>')($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('<div ng:click="clicked = true"></div>'); - scope.$digest(); - expect(scope.clicked).toBeFalsy(); + it('should get called on a click', inject(function($rootScope) { + var element = angular.compile('<div ng:click="clicked = true"></div>')($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('<div ng:click="outer = true"><div ng:click="inner = true"></div></div>'); - scope.$digest(); - expect(scope.outer).not.toBeDefined(); - expect(scope.inner).not.toBeDefined(); + it('should stop event propagation', inject(function($rootScope) { + var element = angular.compile('<div ng:click="outer = true"><div ng:click="inner = true"></div></div>')($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('<form action="" ng:submit="submitted = true">' + - '<input type="submit"/>' + - '</form>'); - scope.$digest(); - expect(scope.submitted).not.toBeDefined(); + it('should get called on form submit', inject(function($rootScope) { + var element = angular.compile('<form action="" ng:submit="submitted = true">' + + '<input type="submit"/>' + + '</form>')($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('<div class="existing" ng:class="dynClass"></div>'); - scope.dynClass = 'A'; - scope.$digest(); + it('should add new and remove old classes dynamically', inject(function($rootScope) { + var element = angular.compile('<div class="existing" ng:class="dynClass"></div>')($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('<div class="existing" ng:class="[\'A\', \'B\']"></div>'); - scope.$digest(); + it('should support adding multiple classes via an array', inject(function($rootScope) { + var element = angular.compile('<div class="existing" ng:class="[\'A\', \'B\']"></div>')($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('<div class="existing" ng:class="\'A B\'"></div>'); - scope.$digest(); + it('should support adding multiple classes via a space delimited string', inject(function($rootScope) { + var element = angular.compile('<div class="existing" ng:class="\'A B\'"></div>')($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('<div class="existing" ng:class="dynClass"></div>'); - scope.dynClass = 'A'; - scope.$digest(); + it('should preserve class added post compilation with pre-existing classes', inject(function($rootScope) { + var element = angular.compile('<div class="existing" ng:class="dynClass"></div>')($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('<div ng:class="dynClass"></div>'); - scope.dynClass = 'A'; - scope.$digest(); + it('should preserve class added post compilation without pre-existing classes"', inject(function($rootScope) { + var element = angular.compile('<div ng:class="dynClass"></div>')($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('<div class="ui-panel ui-selected" ng:class="dynCls"></div>'); - 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('<div class="ui-panel ui-selected" ng:class="dynCls"></div>')($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('<div class="panel bar" ng:class="dynCls"></div>'); - scope.dynCls = 'panel'; - scope.$digest(); + it('should not add duplicate classes', inject(function($rootScope) { + var element = angular.compile('<div class="panel bar" ng:class="dynCls"></div>')($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('<div class="panel bar" ng:class="dynCls"></div>'); - 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('<div class="panel bar" ng:class="dynCls"></div>')($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('<div ng:class="dynCls"></div>'); - scope.dynCls = 'foo'; - scope.$digest(); + it('should remove classes even if they were added by another code', inject(function($rootScope) { + var element = angular.compile('<div ng:class="dynCls"></div>')($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('<div ng:class="dynCls"></div>'); - scope.dynCls = [undefined, null]; - scope.$digest(); + it('should convert undefined and null values to an empty string', inject(function($rootScope) { + var element = angular.compile('<div ng:class="dynCls"></div>')($rootScope); + $rootScope.dynCls = [undefined, null]; + $rootScope.$digest(); expect(element[0].className).toBe('ng-directive'); - }); + })); }); - 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>'); - scope.$digest(); + it('should ng:class odd/even', inject(function($rootScope) { + var element = angular.compile('<ul><li ng:repeat="i in [0,1]" class="existing" ng:class-odd="\'odd\'" ng:class-even="\'even\'"></li><ul>')($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('<ul>' + - '<li ng:repeat="i in [0,1]" ng:class="\'plainClass\'" ' + - 'ng:class-odd="\'odd\'" ng:class-even="\'even\'"></li>' + - '<ul>'); - scope.$apply(); + it('should allow both ng:class and ng:class-odd/even on the same element', inject(function($rootScope) { + var element = angular.compile('<ul>' + + '<li ng:repeat="i in [0,1]" ng:class="\'plainClass\'" ' + + 'ng:class-odd="\'odd\'" ng:class-even="\'even\'"></li>' + + '<ul>')($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('<ul>' + - '<li ng:repeat="i in [0,1]" ng:class="[\'A\', \'B\']" ' + - 'ng:class-odd="[\'C\', \'D\']" ng:class-even="[\'E\', \'F\']"></li>' + - '<ul>'); - scope.$apply(); + it('should allow both ng:class and ng:class-odd/even with multiple classes', inject(function($rootScope) { + var element = angular.compile('<ul>' + + '<li ng:repeat="i in [0,1]" ng:class="[\'A\', \'B\']" ' + + 'ng:class-odd="[\'C\', \'D\']" ng:class-even="[\'E\', \'F\']"></li>' + + '<ul>')($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('<div ng:style="{height: \'40px\'}"></div>'); - scope.$digest(); + it('should set', inject(function($rootScope) { + var element = angular.compile('<div ng:style="{height: \'40px\'}"></div>')($rootScope); + $rootScope.$digest(); expect(element.css('height')).toEqual('40px'); - }); + })); - it('should silently ignore undefined style', function() { - var scope = compile('<div ng:style="myStyle"></div>'); - scope.$digest(); + it('should silently ignore undefined style', inject(function($rootScope) { + var element = angular.compile('<div ng:style="myStyle"></div>')($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('<div ng:style="styleObj"></div>'); 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('<div ng:show="exp"></div>'), - scope = compile(element); - - scope.$digest(); + it('should show and hide an element', inject(function($rootScope) { + var element = jqLite('<div ng:show="exp"></div>'); + 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('<div style="display: none" ng:show="exp"></div>'), - scope = compile(element); + it('should make hidden element visible', inject(function($rootScope) { + var element = jqLite('<div style="display: none" ng:show="exp"></div>'); + 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('<div ng:hide="exp"></div>'), - scope = compile(element); - + it('should hide an element', inject(function($rootScope) { + var element = jqLite('<div ng:hide="exp"></div>'); + 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('<div ng:controller="temp.Greeter"></div>'); - expect(scope.greeter.greeting).toEqual('hello'); - expect(scope.greeter.greet('misko')).toEqual('hello misko!'); - }); + it('should bind', inject(function($rootScope) { + var element = angular.compile('<div ng:controller="temp.Greeter"></div>')($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('<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'); - scope.$digest(); - expect(scope.$element.text()).toEqual('hey dude!'); - }); - - it('should infer injection arguments', function() { + var element = angular.compile('<div ng:controller="temp.Greeter"><div ng:controller="temp.ChildGreeter">{{greet("misko")}}</div></div>')($rootScope); + expect($rootScope.greeting).not.toBeDefined(); + expect($rootScope.greeter.greeting).toEqual('hello'); + expect($rootScope.greeter.greet('misko')).toEqual('hello misko!'); + expect($rootScope.greeter.greeting).toEqual('hello'); + expect($rootScope.childGreeter.greeting).toEqual('hey'); + expect($rootScope.childGreeter.$parent.greeting).toEqual('hello'); + $rootScope.$digest(); + expect(element.text()).toEqual('hey dude!'); + })); + + it('should infer injection arguments', inject(function($rootScope) { temp.MyController = function($xhr){ this.$root.someService = $xhr; }; - var scope = compile('<div ng:controller="temp.MyController"></div>'); - expect(scope.someService).toBe(scope.$service('$xhr')); - }); + var element = angular.compile('<div ng:controller="temp.MyController"></div>')($rootScope); + expect($rootScope.someService).toBe($rootScope.$service('$xhr')); + })); }); describe('ng:cloak', function() { - it('should get removed when an element is compiled', function() { + it('should get removed when an element is compiled', inject(function($rootScope) { var element = jqLite('<div ng:cloak></div>'); expect(element.attr('ng:cloak')).toBe(''); angular.compile(element); expect(element.attr('ng:cloak')).toBeUndefined(); - }); + })); - it('should remove ng-cloak class from a compiled element', function() { + it('should remove ng-cloak class from a compiled element', inject(function($rootScope) { var element = jqLite('<div ng:cloak class="foo ng-cloak bar"></div>'); expect(element.hasClass('foo')).toBe(true); @@ -558,6 +543,6 @@ describe("directive", function() { expect(element.hasClass('foo')).toBe(true); expect(element.hasClass('ng-cloak')).toBe(false); expect(element.hasClass('bar')).toBe(true); - }); + })); }); }); |
