diff options
| author | Misko Hevery | 2011-10-17 16:56:56 -0700 |
|---|---|---|
| committer | Misko Hevery | 2011-11-14 16:39:31 -0800 |
| commit | 48697a2b86dbb12ea8de64cc5fece7caf68b321e (patch) | |
| tree | 1fa50659f0bb5de2640dea2a2e5bb5628f2bb14a /test/widget | |
| parent | 93b777c916ccff243c5a6080bf5f39860ac7bf39 (diff) | |
| download | angular.js-48697a2b86dbb12ea8de64cc5fece7caf68b321e.tar.bz2 | |
refactor(injector): turn scope into a service
- turn scope into a $rootScope service.
- injector is now a starting point for creating angular application.
- added inject() method which wraps jasmine its/beforeEach/afterEach,
and which allows configuration and injection of services.
- refactor tests to use inject() where possible
BREAK:
- removed angular.scope() method
Diffstat (limited to 'test/widget')
| -rw-r--r-- | test/widget/formSpec.js | 58 | ||||
| -rw-r--r-- | test/widget/inputSpec.js | 55 | ||||
| -rw-r--r-- | test/widget/selectSpec.js | 23 |
3 files changed, 66 insertions, 70 deletions
diff --git a/test/widget/formSpec.js b/test/widget/formSpec.js index 4f5630ba..a30f21eb 100644 --- a/test/widget/formSpec.js +++ b/test/widget/formSpec.js @@ -8,17 +8,17 @@ describe('form', function() { }); - it('should attach form to DOM', function() { + it('should attach form to DOM', inject(function($rootScope) { doc = angular.element('<form>'); - var scope = angular.compile(doc)(); + angular.compile(doc)($rootScope); expect(doc.data('$form')).toBeTruthy(); - }); + })); - it('should prevent form submission', function() { + it('should prevent form submission', inject(function($rootScope) { var startingUrl = '' + window.location; doc = angular.element('<form name="myForm"><input type=submit val=submit>'); - var scope = angular.compile(doc)(); + angular.compile(doc)($rootScope); browserTrigger(doc.find('input')); waitsFor( function() { return true; }, @@ -26,44 +26,44 @@ describe('form', function() { runs(function() { expect('' + window.location).toEqual(startingUrl); }); - }); + })); - it('should publish form to scope', function() { + it('should publish form to scope', inject(function($rootScope) { doc = angular.element('<form name="myForm"></form>'); - var scope = angular.compile(doc)(); - expect(scope.myForm).toBeTruthy(); + angular.compile(doc)($rootScope); + expect($rootScope.myForm).toBeTruthy(); expect(doc.data('$form')).toBeTruthy(); - expect(doc.data('$form')).toEqual(scope.myForm); - }); + expect(doc.data('$form')).toEqual($rootScope.myForm); + })); - it('should have ng-valide/ng-invalid style', function() { + it('should have ng-valide/ng-invalid style', inject(function($rootScope) { doc = angular.element('<form name="myForm"><input type=text ng:model=text required>'); - var scope = angular.compile(doc)(); - scope.text = 'misko'; - scope.$digest(); + angular.compile(doc)($rootScope); + $rootScope.text = 'misko'; + $rootScope.$digest(); expect(doc.hasClass('ng-valid')).toBe(true); expect(doc.hasClass('ng-invalid')).toBe(false); - scope.text = ''; - scope.$digest(); + $rootScope.text = ''; + $rootScope.$digest(); expect(doc.hasClass('ng-valid')).toBe(false); expect(doc.hasClass('ng-invalid')).toBe(true); - }); + })); - it('should chain nested forms', function() { + it('should chain nested forms', inject(function($rootScope) { doc = angular.element( '<ng:form name=parent>' + '<ng:form name=child>' + '<input type=text ng:model=text name=text>' + '</ng:form>' + '</ng:form>'); - var scope = angular.compile(doc)(); - var parent = scope.parent; - var child = scope.child; + angular.compile(doc)($rootScope); + var parent = $rootScope.parent; + var child = $rootScope.child; var input = child.text; input.$emit('$invalid', 'MyError'); @@ -73,21 +73,21 @@ describe('form', function() { input.$emit('$valid', 'MyError'); expect(parent.$error.MyError).toBeUndefined(); expect(child.$error.MyError).toBeUndefined(); - }); + })); - it('should chain nested forms in repeater', function() { + it('should chain nested forms in repeater', inject(function($rootScope) { doc = angular.element( '<ng:form name=parent>' + '<ng:form ng:repeat="f in forms" name=child>' + '<input type=text ng:model=text name=text>' + '</ng:form>' + '</ng:form>'); - var scope = angular.compile(doc)(); - scope.forms = [1]; - scope.$digest(); + angular.compile(doc)($rootScope); + $rootScope.forms = [1]; + $rootScope.$digest(); - var parent = scope.parent; + var parent = $rootScope.parent; var child = doc.find('input').scope().child; var input = child.text; expect(parent).toBeDefined(); @@ -102,5 +102,5 @@ describe('form', function() { input.$emit('$valid', 'myRule'); expect(parent.$error.myRule).toBeUndefined(); expect(child.$error.myRule).toBeUndefined(); - }); + })); }); diff --git a/test/widget/inputSpec.js b/test/widget/inputSpec.js index 179f8156..a889212f 100644 --- a/test/widget/inputSpec.js +++ b/test/widget/inputSpec.js @@ -4,8 +4,8 @@ describe('widget: input', function() { var compile = null, element = null, scope = null, defer = null; var doc = null; - beforeEach(function() { - scope = null; + beforeEach(inject(function($rootScope) { + scope = $rootScope; element = null; compile = function(html, parent) { if (parent) { @@ -14,12 +14,12 @@ describe('widget: input', function() { } else { element = jqLite(html); } - scope = angular.compile(element)(); + angular.compile(element)(scope); scope.$apply(); defer = scope.$service('$browser').defer; return scope; }; - }); + })); afterEach(function() { dealoc(element); @@ -28,8 +28,7 @@ describe('widget: input', function() { describe('text', function() { - var scope = null, - form = null, + var form = null, formElement = null, inputElement = null; @@ -41,7 +40,7 @@ describe('widget: input', function() { formElement = doc = angular.element('<form name="form"><input ' + prefix + 'type="text" ng:model="name" name="name" ng:change="change()"></form>'); inputElement = formElement.find('input'); - scope = angular.compile(doc)(); + angular.compile(doc)(scope); form = formElement.inheritedData('$form'); }; @@ -91,18 +90,18 @@ describe('widget: input', function() { }); - it('should change non-html5 types to text', function() { + it('should change non-html5 types to text', inject(function($rootScope) { doc = angular.element('<form name="form"><input type="abc" ng:model="name"></form>'); - scope = angular.compile(doc)(); + angular.compile(doc)($rootScope); expect(doc.find('input').attr('type')).toEqual('text'); - }); + })); - it('should not change html5 types to text', function() { + it('should not change html5 types to text', inject(function($rootScope) { doc = angular.element('<form name="form"><input type="number" ng:model="name"></form>'); - scope = angular.compile(doc)(); + angular.compile(doc)($rootScope); expect(doc.find('input')[0].getAttribute('type')).toEqual('number'); - }); + })); }); @@ -455,35 +454,33 @@ describe('widget: input', function() { describe('scope declaration', function() { - it('should read the declaration from scope', function() { + it('should read the declaration from scope', inject(function($rootScope) { var input, $formFactory; element = angular.element('<input type="@MyType" ng:model="abc">'); - scope = angular.scope(); - scope.MyType = function($f, i) { + $rootScope.MyType = function($f, i) { input = i; $formFactory = $f; }; - scope.MyType.$inject = ['$formFactory']; + $rootScope.MyType.$inject = ['$formFactory']; - angular.compile(element)(scope); + angular.compile(element)($rootScope); - expect($formFactory).toBe(scope.$service('$formFactory')); + expect($formFactory).toBe($rootScope.$service('$formFactory')); expect(input[0]).toBe(element[0]); - }); + })); - it('should throw an error of Cntoroller not declared in scope', function() { + it('should throw an error of Controller not declared in scope', inject(function($rootScope) { var input, $formFactory; element = angular.element('<input type="@DontExist" ng:model="abc">'); var error; try { - scope = angular.scope(); - angular.compile(element)(scope); + angular.compile(element)($rootScope); error = 'no error thrown'; } catch (e) { error = e; } expect(error.message).toEqual("Argument 'DontExist' is not a function, got undefined"); - }); + })); }); @@ -580,16 +577,16 @@ describe('widget: input', function() { {'ng:maxlength': 3}); - it('should throw an error when scope pattern can\'t be found', function() { - var el = jqLite('<input ng:model="foo" ng:pattern="fooRegexp">'), - scope = angular.compile(el)(); + it('should throw an error when scope pattern can\'t be found', inject(function($rootScope) { + var el = jqLite('<input ng:model="foo" ng:pattern="fooRegexp">'); + angular.compile(el)($rootScope); el.val('xx'); browserTrigger(el, 'keydown'); - expect(function() { scope.$service('$browser').defer.flush(); }). + expect(function() { $rootScope.$service('$browser').defer.flush(); }). toThrow('Expected fooRegexp to be a RegExp but was undefined'); dealoc(el); - }); + })); }); }); diff --git a/test/widget/selectSpec.js b/test/widget/selectSpec.js index c3fdc2e6..31e5223d 100644 --- a/test/widget/selectSpec.js +++ b/test/widget/selectSpec.js @@ -1,10 +1,10 @@ 'use strict'; describe('select', function() { - var compile = null, element = null, scope = null, $formFactory = null; + var compile = null, element = null, scope = null; - beforeEach(function() { - scope = null; + beforeEach(inject(function($rootScope) { + scope = $rootScope; element = null; compile = function(html, parent) { if (parent) { @@ -13,12 +13,11 @@ describe('select', function() { } else { element = jqLite(html); } - scope = angular.compile(element)(); + angular.compile(element)($rootScope); scope.$apply(); - $formFactory = scope.$service('$formFactory'); return scope; }; - }); + })); afterEach(function() { dealoc(element); @@ -41,7 +40,7 @@ describe('select', function() { expect(scope.$element.text()).toBe('foobarC'); }); - it('should require', function() { + it('should require', inject(function($formFactory) { compile('<select name="select" ng:model="selection" required ng:change="log=log+\'change;\'">' + '<option value=""></option>' + '<option value="c">C</option>' + @@ -65,7 +64,7 @@ describe('select', function() { expect(element).toBeValid(); expect(element).toBeDirty(); expect(scope.log).toEqual('change;'); - }); + })); it('should not be invalid if no require', function() { compile('<select name="select" ng:model="selection">' + @@ -91,7 +90,7 @@ describe('select', function() { expect(element[0].childNodes[0].selected).toEqual(true); }); - it('should require', function() { + it('should require', inject(function($formFactory) { compile('<select name="select" ng:model="selection" multiple required>' + '<option>A</option>' + '<option>B</option>' + @@ -112,7 +111,7 @@ describe('select', function() { browserTrigger(element, 'change'); expect(element).toBeValid(); expect(element).toBeDirty(); - }); + })); }); @@ -157,12 +156,12 @@ describe('select', function() { dealoc(scope); }); - it('should throw when not formated "? for ? in ?"', function() { + it('should throw when not formated "? for ? in ?"', inject(function($rootScope, $exceptionHandler) { expect(function() { compile('<select ng:model="selected" ng:options="i dont parse"></select>'); }).toThrow("Expected ng:options in form of '_select_ (as _label_)? for (_key_,)?_value_ in" + " _collection_' but got 'i dont parse'."); - }); + })); it('should render a list', function() { createSingleSelect(); |
