| Index: third_party/pkg/angular/test/directive/ng_form_spec.dart
|
| ===================================================================
|
| --- third_party/pkg/angular/test/directive/ng_form_spec.dart (revision 33054)
|
| +++ third_party/pkg/angular/test/directive/ng_form_spec.dart (working copy)
|
| @@ -4,41 +4,43 @@
|
|
|
| main() =>
|
| describe('form', () {
|
| - TestBed _;
|
| + TestBed _;
|
|
|
| - it('should set the name of the form and attach it to the scope', inject((Scope scope, TestBed _) {
|
| + beforeEach(inject((TestBed tb) => _ = tb));
|
| +
|
| + it('should set the name of the form and attach it to the scope', inject((Scope scope) {
|
| var element = $('<form name="myForm"></form>');
|
|
|
| - expect(scope.context['myForm']).toBeNull();
|
| + expect(scope['myForm']).toBeNull();
|
|
|
| _.compile(element);
|
| - scope.apply();
|
| + scope.$apply();
|
|
|
| - expect(scope.context['myForm']).toBeDefined();
|
| + expect(scope['myForm']).toBeDefined();
|
|
|
| - var form = scope.context['myForm'];
|
| + var form = scope['myForm'];
|
| expect(form.name).toEqual('myForm');
|
| }));
|
|
|
| describe('pristine / dirty', () {
|
| - it('should be set to pristine by default', inject((Scope scope, TestBed _) {
|
| + it('should be set to pristine by default', inject((Scope scope) {
|
| var element = $('<form name="myForm"></form>');
|
|
|
| _.compile(element);
|
| - scope.apply();
|
| + scope.$apply();
|
|
|
| - var form = scope.context['myForm'];
|
| + var form = scope['myForm'];
|
| expect(form.pristine).toEqual(true);
|
| expect(form.dirty).toEqual(false);
|
| }));
|
|
|
| - it('should add and remove the correct CSS classes when set to dirty and to pristine', inject((Scope scope, TestBed _) {
|
| + it('should add and remove the correct CSS classes when set to dirty and to pristine', inject((Scope scope) {
|
| var element = $('<form name="myForm"></form>');
|
|
|
| _.compile(element);
|
| - scope.apply();
|
| + scope.$apply();
|
|
|
| - var form = scope.context['myForm'];
|
| + var form = scope['myForm'];
|
|
|
| form.dirty = true;
|
| expect(form.pristine).toEqual(false);
|
| @@ -55,13 +57,13 @@
|
| });
|
|
|
| describe('valid / invalid', () {
|
| - it('should add and remove the correct flags when set to valid and to invalid', inject((Scope scope, TestBed _) {
|
| + it('should add and remove the correct flags when set to valid and to invalid', inject((Scope scope) {
|
| var element = $('<form name="myForm"></form>');
|
|
|
| _.compile(element);
|
| - scope.apply();
|
| + scope.$apply();
|
|
|
| - var form = scope.context['myForm'];
|
| + var form = scope['myForm'];
|
|
|
| form.invalid = true;
|
| expect(form.valid).toEqual(false);
|
| @@ -76,72 +78,72 @@
|
| expect(element.hasClass('ng-valid')).toBe(true);
|
| }));
|
|
|
| - it('should set the validity with respect to all existing validations when setValidity() is used', inject((Scope scope, TestBed _) {
|
| - var element = $('<form name="myForm">'
|
| + it('should set the validity with respect to all existing validations when setValidity() is used', inject((Scope scope) {
|
| + var element = $('<form name="myForm">' +
|
| ' <input type="text" ng-model="one" name="one" />' +
|
| ' <input type="text" ng-model="two" name="two" />' +
|
| ' <input type="text" ng-model="three" name="three" />' +
|
| '</form>');
|
|
|
| _.compile(element);
|
| - scope.apply();
|
| + scope.$apply();
|
|
|
| - var form = scope.context['myForm'];
|
| + var form = scope['myForm'];
|
| NgModel one = form['one'];
|
| NgModel two = form['two'];
|
| NgModel three = form['three'];
|
|
|
| - form.updateControlValidity(one, "some error", false);
|
| + form.setValidity(one, "some error", false);
|
| expect(form.valid).toBe(false);
|
| expect(form.invalid).toBe(true);
|
|
|
| - form.updateControlValidity(two, "some error", false);
|
| + form.setValidity(two, "some error", false);
|
| expect(form.valid).toBe(false);
|
| expect(form.invalid).toBe(true);
|
|
|
| - form.updateControlValidity(one, "some error", true);
|
| + form.setValidity(one, "some error", true);
|
| expect(form.valid).toBe(false);
|
| expect(form.invalid).toBe(true);
|
|
|
| - form.updateControlValidity(two, "some error", true);
|
| + form.setValidity(two, "some error", true);
|
| expect(form.valid).toBe(true);
|
| expect(form.invalid).toBe(false);
|
| }));
|
|
|
| - it('should not handle the control errorType pair more than once', inject((Scope scope, TestBed _) {
|
| - var element = $('<form name="myForm">'
|
| + it('should not handle the control + errorType pair more than once', inject((Scope scope) {
|
| + var element = $('<form name="myForm">' +
|
| ' <input type="text" ng-model="one" name="one" />' +
|
| '</form>');
|
|
|
| _.compile(element);
|
| - scope.apply();
|
| + scope.$apply();
|
|
|
| - var form = scope.context['myForm'];
|
| + var form = scope['myForm'];
|
| NgModel one = form['one'];
|
|
|
| - form.updateControlValidity(one, "validation error", false);
|
| + form.setValidity(one, "validation error", false);
|
| expect(form.valid).toBe(false);
|
| expect(form.invalid).toBe(true);
|
|
|
| - form.updateControlValidity(one, "validation error", false);
|
| + form.setValidity(one, "validation error", false);
|
| expect(form.valid).toBe(false);
|
| expect(form.invalid).toBe(true);
|
|
|
| - form.updateControlValidity(one, "validation error", true);
|
| + form.setValidity(one, "validation error", true);
|
| expect(form.valid).toBe(true);
|
| expect(form.invalid).toBe(false);
|
| }));
|
|
|
| - it('should update the validity of the parent form when the inner model changes', inject((Scope scope, TestBed _) {
|
| - var element = $('<form name="myForm">'
|
| + it('should update the validity of the parent form when the inner model changes', inject((Scope scope) {
|
| + var element = $('<form name="myForm">' +
|
| ' <input type="text" ng-model="one" name="one" />' +
|
| ' <input type="text" ng-model="two" name="two" />' +
|
| '</form>');
|
|
|
| _.compile(element);
|
| - scope.apply();
|
| + scope.$apply();
|
|
|
| - var form = scope.context['myForm'];
|
| + var form = scope['myForm'];
|
| NgModel one = form['one'];
|
| NgModel two = form['two'];
|
|
|
| @@ -162,19 +164,19 @@
|
| expect(form.invalid).toBe(false);
|
| }));
|
|
|
| - it('should set the validity for the parent form when fieldsets are used', inject((Scope scope, TestBed _) {
|
| - var element = $('<form name="myForm">'
|
| + it('should set the validity for the parent form when fieldsets are used', inject((Scope scope) {
|
| + var element = $('<form name="myForm">' +
|
| ' <fieldset probe="f">' +
|
| ' <input type="text" ng-model="one" name="one" probe="m" />' +
|
| ' </fieldset>' +
|
| '</form>');
|
|
|
| _.compile(element);
|
| - scope.apply();
|
| + scope.$apply();
|
|
|
| - var form = scope.context['myForm'];
|
| - var fieldset = _.rootScope.context['f'].directive(NgForm);
|
| - var model = _.rootScope.context['m'].directive(NgModel);
|
| + var form = scope['myForm'];
|
| + var fieldset = _.rootScope.f.directive(NgForm);
|
| + var model = _.rootScope.m.directive(NgModel);
|
|
|
| model.setValidity("error", false);
|
|
|
| @@ -188,12 +190,12 @@
|
| expect(fieldset.valid).toBe(true);
|
| expect(form.valid).toBe(true);
|
|
|
| - form.updateControlValidity(fieldset, "error", false);
|
| + form.setValidity(fieldset, "error", false);
|
| expect(model.valid).toBe(true);
|
| expect(fieldset.valid).toBe(true);
|
| expect(form.valid).toBe(false);
|
|
|
| - fieldset.updateControlValidity(model, "error", false);
|
| + fieldset.setValidity(model, "error", false);
|
| expect(model.valid).toBe(true);
|
| expect(fieldset.valid).toBe(false);
|
| expect(form.valid).toBe(false);
|
| @@ -201,41 +203,41 @@
|
| });
|
|
|
| describe('controls', () {
|
| - it('should add each contained ng-model as a control upon compile', inject((Scope scope, TestBed _) {
|
| - var element = $('<form name="myForm">'
|
| + it('should add each contained ng-model as a control upon compile', inject((Scope scope) {
|
| + var element = $('<form name="myForm">' +
|
| ' <input type="text" ng-model="mega_model" name="mega_name" />' +
|
| ' <select ng-model="fire_model" name="fire_name">' +
|
| - ' <option>value</option>'
|
| + ' <option>value</option>' +
|
| ' </select>' +
|
| '</form>');
|
|
|
| _.compile(element);
|
|
|
| - scope.context['mega_model'] = 'mega';
|
| - scope.context['fire_model'] = 'fire';
|
| - scope.apply();
|
| + scope.mega_model = 'mega';
|
| + scope.fire_model = 'fire';
|
| + scope.$apply();
|
|
|
| - var form = scope.context['myForm'];
|
| + var form = scope['myForm'];
|
| expect(form['mega_name'].modelValue).toBe('mega');
|
| expect(form['fire_name'].modelValue).toBe('fire');
|
| }));
|
|
|
| - it('should properly remove controls directly from the ngForm instance', inject((Scope scope, TestBed _) {
|
| - var element = $('<form name="myForm">'
|
| + it('should properly remove controls directly from the ngForm instance', inject((Scope scope) {
|
| + var element = $('<form name="myForm">' +
|
| ' <input type="text" ng-model="mega_model" name="mega_control" />' +
|
| '</form>');
|
|
|
| _.compile(element);
|
| - scope.apply();
|
| + scope.$apply();
|
|
|
| - var form = scope.context['myForm'];
|
| + var form = scope['myForm'];
|
| var control = form['mega_control'];
|
| form.removeControl(control);
|
| expect(form['mega_control']).toBeNull();
|
| }));
|
|
|
| - it('should remove all controls when the scope is destroyed', inject((Scope scope, TestBed _) {
|
| - Scope childScope = scope.createChild({});
|
| + it('should remove all controls when the scope is destroyed', inject((Scope scope) {
|
| + Scope childScope = scope.$new();
|
| var element = $('<form name="myForm">' +
|
| ' <input type="text" ng-model="one" name="one" />' +
|
| ' <input type="text" ng-model="two" name="two" />' +
|
| @@ -243,14 +245,14 @@
|
| '</form>');
|
|
|
| _.compile(element, scope: childScope);
|
| - childScope.apply();
|
| + childScope.$apply();
|
|
|
| - var form = childScope.context['myForm'];
|
| + var form = childScope['myForm'];
|
| expect(form['one']).toBeDefined();
|
| expect(form['two']).toBeDefined();
|
| expect(form['three']).toBeDefined();
|
|
|
| - childScope.destroy();
|
| + childScope.$destroy();
|
|
|
| expect(form['one']).toBeNull();
|
| expect(form['two']).toBeNull();
|
| @@ -259,11 +261,11 @@
|
| });
|
|
|
| describe('onSubmit', () {
|
| - it('should suppress the submission event if no action is provided within the form', inject((Scope scope, TestBed _) {
|
| + it('should suppress the submission event if no action is provided within the form', inject((Scope scope) {
|
| var element = $('<form name="myForm"></form>');
|
|
|
| _.compile(element);
|
| - scope.apply();
|
| + scope.$apply();
|
|
|
| Event submissionEvent = new Event.eventType('CustomEvent', 'submit');
|
|
|
| @@ -278,11 +280,11 @@
|
| expect(fakeEvent.defaultPrevented).toBe(false);
|
| }));
|
|
|
| - it('should not prevent the submission event if an action is defined', inject((Scope scope, TestBed _) {
|
| + it('should not prevent the submission event if an action is defined', inject((Scope scope) {
|
| var element = $('<form name="myForm" action="..."></form>');
|
|
|
| _.compile(element);
|
| - scope.apply();
|
| + scope.$apply();
|
|
|
| Event submissionEvent = new Event.eventType('CustomEvent', 'submit');
|
|
|
| @@ -291,139 +293,18 @@
|
| expect(submissionEvent.defaultPrevented).toBe(false);
|
| }));
|
|
|
| - it('should execute the ng-submit expression if provided upon form submission', inject((Scope scope, TestBed _) {
|
| + it('should execute the ng-submit expression if provided upon form submission', inject((Scope scope) {
|
| var element = $('<form name="myForm" ng-submit="submitted = true"></form>');
|
|
|
| _.compile(element);
|
| - scope.apply();
|
| + scope.$apply();
|
|
|
| - _.rootScope.context['submitted'] = false;
|
| + _.rootScope.submitted = false;
|
|
|
| Event submissionEvent = new Event.eventType('CustomEvent', 'submit');
|
| element[0].dispatchEvent(submissionEvent);
|
|
|
| - expect(_.rootScope.context['submitted']).toBe(true);
|
| + expect(_.rootScope.submitted).toBe(true);
|
| }));
|
| -
|
| - it('should apply the valid and invalid prefixed submit CSS classes to the element', inject((TestBed _) {
|
| - _.compile('<form name="superForm">' +
|
| - ' <input type="text" ng-model="myModel" probe="i" required />' +
|
| - '</form>');
|
| -
|
| - NgForm form = _.rootScope.context['superForm'];
|
| - Probe probe = _.rootScope.context['i'];
|
| - var model = probe.directive(NgModel);
|
| -
|
| - expect(form.submitted).toBe(false);
|
| - expect(form.valid_submit).toBe(false);
|
| - expect(form.invalid_submit).toBe(false);
|
| - expect(form.element.classes.contains('ng-submit-invalid')).toBe(false);
|
| - expect(form.element.classes.contains('ng-submit-valid')).toBe(false);
|
| -
|
| - Event submissionEvent = new Event.eventType('CustomEvent', 'submit');
|
| -
|
| - form.element.dispatchEvent(submissionEvent);
|
| - _.rootScope.apply();
|
| -
|
| - expect(form.submitted).toBe(true);
|
| - expect(form.valid_submit).toBe(false);
|
| - expect(form.invalid_submit).toBe(true);
|
| - expect(form.element.classes.contains('ng-submit-invalid')).toBe(true);
|
| - expect(form.element.classes.contains('ng-submit-valid')).toBe(false);
|
| -
|
| - _.rootScope.apply('myModel = "man"');
|
| - form.element.dispatchEvent(submissionEvent);
|
| -
|
| - expect(form.submitted).toBe(true);
|
| - expect(form.valid_submit).toBe(true);
|
| - expect(form.invalid_submit).toBe(false);
|
| - expect(form.element.classes.contains('ng-submit-invalid')).toBe(false);
|
| - expect(form.element.classes.contains('ng-submit-valid')).toBe(true);
|
| - }));
|
| });
|
| -
|
| - describe('reset()', () {
|
| - it('should reset the model value to its original state', inject((TestBed _) {
|
| - _.compile('<form name="superForm">' +
|
| - ' <input type="text" ng-model="myModel" probe="i" />' +
|
| - '</form>');
|
| - _.rootScope.apply('myModel = "animal"');
|
| -
|
| - NgForm form = _.rootScope.context['superForm'];
|
| -
|
| - Probe probe = _.rootScope.context['i'];
|
| - var model = probe.directive(NgModel);
|
| -
|
| - expect(_.rootScope.context['myModel']).toEqual('animal');
|
| - expect(model.modelValue).toEqual('animal');
|
| - expect(model.viewValue).toEqual('animal');
|
| -
|
| - _.rootScope.apply('myModel = "man"');
|
| -
|
| - expect(_.rootScope.context['myModel']).toEqual('man');
|
| - expect(model.modelValue).toEqual('man');
|
| - expect(model.viewValue).toEqual('man');
|
| -
|
| - form.reset();
|
| - _.rootScope.apply();
|
| -
|
| - expect(_.rootScope.context['myModel']).toEqual('animal');
|
| - expect(model.modelValue).toEqual('animal');
|
| - expect(model.viewValue).toEqual('animal');
|
| - }));
|
| -
|
| - it('should set the form control to be untouched when the model is reset or submitted', inject((TestBed _) {
|
| - var form = _.compile('<form name="duperForm">' +
|
| - ' <input type="text" ng-model="myModel" probe="i" />' +
|
| - '</form>');
|
| - var model = _.rootScope.context['i'].directive(NgModel);
|
| - var input = model.element;
|
| -
|
| - NgForm formModel = _.rootScope.context['duperForm'];
|
| -
|
| - expect(formModel.touched).toBe(false);
|
| - expect(formModel.untouched).toBe(true);
|
| - expect(form.classes.contains('ng-touched')).toBe(false);
|
| - expect(form.classes.contains('ng-untouched')).toBe(true);
|
| -
|
| - _.triggerEvent(input, 'blur');
|
| -
|
| - expect(formModel.touched).toBe(true);
|
| - expect(formModel.untouched).toBe(false);
|
| - expect(form.classes.contains('ng-touched')).toBe(true);
|
| - expect(form.classes.contains('ng-untouched')).toBe(false);
|
| -
|
| - formModel.reset();
|
| -
|
| - expect(formModel.touched).toBe(false);
|
| - expect(formModel.untouched).toBe(true);
|
| - expect(form.classes.contains('ng-touched')).toBe(false);
|
| - expect(form.classes.contains('ng-untouched')).toBe(true);
|
| -
|
| - _.triggerEvent(input, 'blur');
|
| -
|
| - expect(formModel.touched).toBe(true);
|
| -
|
| - _.triggerEvent(form, 'submit');
|
| -
|
| - expect(formModel.touched).toBe(false);
|
| - expect(formModel.untouched).toBe(true);
|
| - expect(form.classes.contains('ng-touched')).toBe(false);
|
| - expect(form.classes.contains('ng-untouched')).toBe(true);
|
| - }));
|
| - });
|
| -
|
| - describe('regression tests: form', () {
|
| - it('should be resolvable by injector if configured by user.', () {
|
| - module((Module module) {
|
| - module.type(NgForm);
|
| - });
|
| -
|
| - inject((Injector injector, Compiler compiler, DirectiveMap directives) {
|
| - var element = $('<form></form>');
|
| - compiler(element, directives)(injector, element);
|
| - // The only expectation is that this doesn't throw
|
| - });
|
| - });
|
| - });
|
| });
|
|
|