diff options
| author | Misko Hevery | 2011-02-07 15:29:56 -0800 |
|---|---|---|
| committer | Misko Hevery | 2011-02-16 00:49:15 -0500 |
| commit | 23b255a8b7481ff5c06004b3558c07f981c42276 (patch) | |
| tree | 57e612f658c9f8903d93a28e9095590178e2ce3a /test | |
| parent | e2154cbc0b9265bea04ce328879d4e9bf1c67c51 (diff) | |
| download | angular.js-23b255a8b7481ff5c06004b3558c07f981c42276.tar.bz2 | |
remove $init on scope from applying compilation template
Closes #40
Diffstat (limited to 'test')
| -rw-r--r-- | test/BinderSpec.js | 31 | ||||
| -rw-r--r-- | test/CompilerSpec.js | 5 | ||||
| -rw-r--r-- | test/ScenarioSpec.js | 9 | ||||
| -rw-r--r-- | test/ValidatorsSpec.js | 5 | ||||
| -rw-r--r-- | test/directivesSpec.js | 3 | ||||
| -rw-r--r-- | test/markupSpec.js | 1 | ||||
| -rw-r--r-- | test/testabilityPatch.js | 1 | ||||
| -rw-r--r-- | test/widgetsSpec.js | 95 |
8 files changed, 69 insertions, 81 deletions
diff --git a/test/BinderSpec.js b/test/BinderSpec.js index 73650bd6..c3f90ad7 100644 --- a/test/BinderSpec.js +++ b/test/BinderSpec.js @@ -3,16 +3,18 @@ describe('Binder', function(){ beforeEach(function(){ var self = this; - this.compile = function(html, initialScope, parent) { + this.compile = function(html, parent) { var compiler = new Compiler(angularTextMarkup, angularAttrMarkup, angularDirective, angularWidget); if (self.element) dealoc(self.element); - var element = self.element = jqLite(html); + var element; + if (parent) { + parent.html(html); + element = parent.children(); + } else { + element = jqLite(html); + } + self.element = element; var scope = compiler.compile(element)(element); - - if (parent) parent.append(element); - - extend(scope, initialScope); - scope.$init(); return {node:element, scope:scope}; }; this.compileToHtml = function (content) { @@ -27,8 +29,8 @@ describe('Binder', function(){ }); - it('ChangingTextfieldUpdatesModel', function(){ - var state = this.compile('<input type="text" name="model.price" value="abc">', {model:{}}); + it('text-field should default to value attribute', function(){ + var state = this.compile('<input type="text" name="model.price" value="abc">'); state.scope.$eval(); assertEquals('abc', state.scope.model.price); }); @@ -443,8 +445,7 @@ describe('Binder', function(){ }); it('ActionOnAHrefThrowsError', function(){ - var model = {books:[]}; - var c = this.compile('<a ng:click="action()">Add Phone</a>', model); + var c = this.compile('<a ng:click="action()">Add Phone</a>'); c.scope.action = function(){ throw new Error('MyError'); }; @@ -517,9 +518,9 @@ describe('Binder', function(){ }); it('ValidateForm', function(){ - var c = this.compile('<div><input name="name" ng:required>' + - '<div ng:repeat="item in items"><input name="item.name" ng:required/></div></div>', - undefined, jqLite(document.body)); + var c = this.compile('<div id="test"><input name="name" ng:required>' + + '<input ng:repeat="item in items" name="item.name" ng:required/></div>', + jqLite(document.body)); var items = [{}, {}]; c.scope.$set("items", items); c.scope.$eval(); @@ -547,7 +548,7 @@ describe('Binder', function(){ }); it('ValidateOnlyVisibleItems', function(){ - var c = this.compile('<div><input name="name" ng:required><input ng:show="show" name="name" ng:required></div>', undefined, jqLite(document.body)); + var c = this.compile('<div><input name="name" ng:required><input ng:show="show" name="name" ng:required></div>', jqLite(document.body)); c.scope.$set("show", true); c.scope.$eval(); assertEquals(2, c.scope.$service('$invalidWidgets').length); diff --git a/test/CompilerSpec.js b/test/CompilerSpec.js index 291a5011..647cc366 100644 --- a/test/CompilerSpec.js +++ b/test/CompilerSpec.js @@ -28,7 +28,6 @@ describe('compiler', function(){ compile = function(html){ var e = jqLite("<div>" + html + "</div>"); var scope = compiler.compile(e)(e); - scope.$init(); return scope; }; }); @@ -48,10 +47,8 @@ describe('compiler', function(){ }; }; var template = compiler.compile(e); - scope = template(e); - var init = scope.$init; expect(log).toEqual("found"); - init(); + scope = template(e); expect(e.hasClass('ng-directive')).toEqual(true); expect(log).toEqual("found:init"); }); diff --git a/test/ScenarioSpec.js b/test/ScenarioSpec.js index 50b5e51c..cd1e3115 100644 --- a/test/ScenarioSpec.js +++ b/test/ScenarioSpec.js @@ -13,25 +13,24 @@ describe("ScenarioSpec: Compilation", function(){ it("should compile dom node and return scope", function(){ var node = jqLite('<div ng:init="a=1">{{b=a+1}}</div>')[0]; scope = compile(node); - scope.$init(); expect(scope.a).toEqual(1); expect(scope.b).toEqual(2); }); it("should compile jQuery node and return scope", function(){ - scope = compile(jqLite('<div>{{a=123}}</div>')).$init(); + scope = compile(jqLite('<div>{{a=123}}</div>')); expect(jqLite(scope.$element).text()).toEqual('123'); }); it("should compile text node and return scope", function(){ - scope = compile('<div>{{a=123}}</div>').$init(); + scope = compile('<div>{{a=123}}</div>'); expect(jqLite(scope.$element).text()).toEqual('123'); }); }); describe('scope', function(){ - it("should have set, get, eval, $init, updateView methods", function(){ - scope = compile('<div>{{a}}</div>').$init(); + it("should have $set, $get, $eval, $updateView methods", function(){ + scope = compile('<div>{{a}}</div>'); scope.$eval("$invalidWidgets.push({})"); expect(scope.$set("a", 2)).toEqual(2); expect(scope.$get("a")).toEqual(2); diff --git a/test/ValidatorsSpec.js b/test/ValidatorsSpec.js index 60d20418..65b93133 100644 --- a/test/ValidatorsSpec.js +++ b/test/ValidatorsSpec.js @@ -9,7 +9,7 @@ describe('ValidatorTest', function(){ }; var scope = compile('<input name="name" ng:validate="myValidator:\'hevery\'"/>'); scope.name = 'misko'; - scope.$init(); + scope.$eval(); assertEquals('misko', validator.first); assertEquals('hevery', validator.last); expect(validator._this.$id).toEqual(scope.$id); @@ -99,7 +99,6 @@ describe('ValidatorTest', function(){ jqLite(document.body).append(self.$element); self.$element.data('$validate', noop); self.$root = self; - self.$init(); }); afterEach(function(){ @@ -110,7 +109,6 @@ describe('ValidatorTest', function(){ var value, fn; var scope = compile('<input type="text" name="name" ng:validate="asynchronous:asyncFn"/>'); jqLite(document.body).append(scope.$element); - scope.$init(); var input = scope.$element; scope.asyncFn = function(v,f){ value=v; fn=f; @@ -155,7 +153,6 @@ describe('ValidatorTest', function(){ scope.asyncFn = jasmine.createSpy(); scope.updateFn = jasmine.createSpy(); scope.name = 'misko'; - scope.$init(); scope.$eval(); expect(scope.asyncFn).wasCalledWith('misko', scope.asyncFn.mostRecentCall.args[1]); assertTrue(scope.$element.hasClass('ng-input-indicator-wait')); diff --git a/test/directivesSpec.js b/test/directivesSpec.js index caf0bc15..8e5a10ee 100644 --- a/test/directivesSpec.js +++ b/test/directivesSpec.js @@ -7,7 +7,6 @@ describe("directive", function(){ compile = function(html) { element = jqLite(html); model = compiler.compile(element)(element); - model.$init(); return model; }; }); @@ -116,7 +115,7 @@ describe("directive", function(){ 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} }}"/>'); expect(element.attr('alt')).toEqual('{"a":1}'); diff --git a/test/markupSpec.js b/test/markupSpec.js index 16efba55..2462f19e 100644 --- a/test/markupSpec.js +++ b/test/markupSpec.js @@ -9,7 +9,6 @@ describe("markups", function(){ compile = function(html) { element = jqLite(html); scope = compiler.compile(element)(element); - scope.$init(); }; }); diff --git a/test/testabilityPatch.js b/test/testabilityPatch.js index 32272a4d..dc6acf8b 100644 --- a/test/testabilityPatch.js +++ b/test/testabilityPatch.js @@ -28,6 +28,7 @@ beforeEach(function(){ compileCache = {}; // reset to jQuery or default to us. bindJQuery(); + jqLite(document.body).html(''); this.addMatchers({ toBeInvalid: function(){ var element = jqLite(this.actual); diff --git a/test/widgetsSpec.js b/test/widgetsSpec.js index 9aa3b95d..606491a9 100644 --- a/test/widgetsSpec.js +++ b/test/widgetsSpec.js @@ -5,12 +5,14 @@ describe("widget", function(){ scope = null; element = null; var compiler = new Compiler(angularTextMarkup, angularAttrMarkup, angularDirective, angularWidget); - compile = function(html, before, parent) { - element = jqLite(html); + compile = function(html, parent) { + if (parent) { + parent.html(html); + element = parent.children(); + } else { + element = jqLite(html); + } scope = compiler.compile(element)(element); - (before||noop).apply(scope); - if (parent) parent.append(element); - scope.$init(); return scope; }; }); @@ -77,9 +79,7 @@ describe("widget", function(){ }); it("should come up blank if null", function(){ - compile('<input type="text" name="age" ng:format="number"/>', function(){ - scope.age = null; - }); + compile('<input type="text" name="age" ng:format="number" ng:init="age=null"/>'); expect(scope.age).toBeNull(); expect(scope.$element[0].value).toEqual(''); }); @@ -137,9 +137,7 @@ describe("widget", function(){ describe("checkbox", function(){ it("should format booleans", function(){ - compile('<input type="checkbox" name="name"/>', function(){ - scope.name = false; - }); + compile('<input type="checkbox" name="name" ng:init="name=false"/>'); expect(scope.name).toEqual(false); expect(scope.$element[0].checked).toEqual(false); }); @@ -184,7 +182,7 @@ describe("widget", function(){ describe("ng:validate", function(){ it("should process ng:validate", function(){ compile('<input type="text" name="price" value="abc" ng:validate="number"/>', - undefined, jqLite(document.body)); + jqLite(document.body)); expect(element.hasClass('ng-validation-error')).toBeTruthy(); expect(element.attr('ng-validation-error')).toEqual('Not a number'); @@ -238,7 +236,7 @@ describe("widget", function(){ }); it("should process ng:required", function(){ - compile('<input type="text" name="price" ng:required/>', undefined, jqLite(document.body)); + compile('<input type="text" name="price" ng:required/>', jqLite(document.body)); expect(element.hasClass('ng-validation-error')).toBeTruthy(); expect(element.attr('ng-validation-error')).toEqual('Required'); @@ -255,7 +253,7 @@ describe("widget", function(){ it('should allow conditions on ng:required', function() { compile('<input type="text" name="price" ng:required="ineedz"/>', - undefined, jqLite(document.body)); + jqLite(document.body)); scope.$set('ineedz', false); scope.$eval(); expect(element.hasClass('ng-validation-error')).toBeFalsy(); @@ -336,25 +334,21 @@ describe("widget", function(){ }); it('should honor model over html checked keyword after', function(){ - compile('<div>' + + compile('<div ng:init="choose=\'C\'">' + '<input type="radio" name="choose" value="A""/>' + '<input type="radio" name="choose" value="B" checked/>' + '<input type="radio" name="choose" value="C"/>' + - '</div>', function(){ - this.choose = 'C'; - }); + '</div>'); expect(scope.choose).toEqual('C'); }); it('should honor model over html checked keyword before', function(){ - compile('<div>' + + compile('<div ng:init="choose=\'A\'">' + '<input type="radio" name="choose" value="A""/>' + '<input type="radio" name="choose" value="B" checked/>' + '<input type="radio" name="choose" value="C"/>' + - '</div>', function(){ - this.choose = 'A'; - }); + '</div>'); expect(scope.choose).toEqual('A'); }); @@ -396,7 +390,7 @@ describe("widget", function(){ '<select name="selection" ng:required>' + '<option value="{{$index}}" ng:repeat="opt in options">{{opt}}</option>' + '</select>', - undefined, jqLite(document.body)); + jqLite(document.body)); scope.selection = 1; scope.options = ['one', 'two']; scope.$eval(); @@ -492,14 +486,13 @@ describe("widget", function(){ }); it('should allow binding to objects through index', function(){ - compile('<select name="selection" multiple ng:format="index:list">' + - '<option selected value="0">A</option>' + - '<option selected value="1">B</option>' + - '<option value="2">C</option>' + - '</select>', - function(){ - scope.list = [{name:'A'}, {name:'B'}, {name:'C'}]; - }); + compile('<div ng:init="list = [{name:\'A\'}, {name:\'B\'}, {name:\'C\'}]">' + + '<select name="selection" multiple ng:format="index:list">' + + '<option selected value="0">A</option>' + + '<option selected value="1">B</option>' + + '<option value="2">C</option>' + + '</select>' + + '</div>'); scope.$eval(); expect(scope.selection).toEqual([{name:'A'}, {name:'B'}]); }); @@ -517,14 +510,13 @@ describe("widget", function(){ }); it('should be contain the selected object', function(){ - compile('<select name="selection" multiple ng:format="index:list">' + - '<option value="0">A</option>' + - '<option value="1" selected>B</option>' + - '<option value="2">C</option>' + - '</select>', - function(){ - scope.list = [{name:'A'}, {name:'B'}, {name:'C'}]; - }); + compile('<div ng:init="list = [{name:\'A\'}, {name:\'B\'}, {name:\'C\'}]">' + + '<select name="selection" multiple ng:format="index:list">' + + '<option value="0">A</option>' + + '<option value="1" selected>B</option>' + + '<option value="2">C</option>' + + '</select>' + + '</div>'); scope.$eval(); expect(scope.selection).toEqual([{name:'B'}]); }); @@ -604,7 +596,7 @@ describe("widget", function(){ it('should call change on switch', function(){ var scope = angular.compile('<ng:switch on="url" change="name=\'works\'"><div ng:switch-when="a">{{name}}</div></ng:switch>'); scope.url = 'a'; - scope.$init(); + scope.$eval(); expect(scope.name).toEqual(undefined); expect(scope.$element.text()).toEqual('works'); dealoc(scope); @@ -619,7 +611,7 @@ describe("widget", function(){ scope.childScope.name = 'misko'; scope.url = 'myUrl'; scope.$service('$xhr.cache').data.myUrl = {value:'{{name}}'}; - scope.$init(); + scope.$eval(); scope.$service('$browser').defer.flush(); expect(element.text()).toEqual('misko'); dealoc(scope); @@ -632,7 +624,7 @@ describe("widget", function(){ scope.childScope.name = 'igor'; scope.url = 'myUrl'; scope.$service('$xhr.cache').data.myUrl = {value:'{{name}}'}; - scope.$init(); + scope.$eval(); scope.$service('$browser').defer.flush(); expect(element.text()).toEqual('igor'); @@ -649,7 +641,7 @@ describe("widget", function(){ var scope = angular.compile(element); scope.url = 'myUrl'; scope.$service('$xhr.cache').data.myUrl = {value:'{{c=c+1}}'}; - scope.$init(); + scope.$eval(); scope.$service('$browser').defer.flush(); // this one should really be just '1', but due to lack of real events things are not working @@ -666,7 +658,7 @@ describe("widget", function(){ scope.url = 'myUrl'; scope.$service('$xhr.cache').data.myUrl = {value:'my partial'}; - scope.$init(); + scope.$eval(); scope.$service('$browser').defer.flush(); expect(element.text()).toEqual('my partial'); expect(scope.loaded).toBe(true); @@ -800,7 +792,6 @@ describe("widget", function(){ beforeEach(function() { rootScope = angular.compile('<ng:view></ng:view>'); - rootScope.$init(); $route = rootScope.$service('$route'); $location = rootScope.$service('$location'); $browser = rootScope.$service('$browser'); @@ -867,15 +858,19 @@ describe("widget", function(){ }); it('should be possible to nest ng:view in ng:include', function() { - dealoc(rootScope); - rootScope = angular.compile('<div>include: <ng:include src="\'includePartial.html\'"></ng:include></div>'); - $browser = rootScope.$service('$browser'); + var myApp = angular.scope(); + var $browser = myApp.$service('$browser'); $browser.xhr.expectGET('includePartial.html').respond('view: <ng:view></ng:view>'); $browser.setUrl('http://server/#/foo'); - $route = rootScope.$service('$route'); + var $route = myApp.$service('$route'); $route.when('/foo', {controller: angular.noop, template: 'viewPartial.html'}); - rootScope.$init(); + + dealoc(rootScope); // we are about to override it. + rootScope = angular.compile( + '<div>' + + 'include: <ng:include src="\'includePartial.html\'">' + + '</ng:include></div>', myApp); $browser.xhr.expectGET('viewPartial.html').respond('content'); $browser.xhr.flush(); |
