Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(forms): emit statusChange when child controls have async validator #9652

Merged
merged 1 commit into from Jun 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 7 additions & 11 deletions modules/@angular/forms/src/model.ts
Expand Up @@ -213,15 +213,7 @@ export abstract class AbstractControl {
emitEvent = isPresent(emitEvent) ? emitEvent : true;

this._errors = errors;
this._status = this._calculateStatus();

if (emitEvent) {
ObservableWrapper.callEmit(this._statusChanges, this._status);
}

if (isPresent(this._parent)) {
this._parent._updateControlsErrors();
}
this._updateControlsErrors(emitEvent);
}

find(path: Array<string|number>|string): AbstractControl { return _find(this, path); }
Expand Down Expand Up @@ -250,11 +242,15 @@ export abstract class AbstractControl {
}

/** @internal */
_updateControlsErrors(): void {
_updateControlsErrors(emitEvent: boolean): void {
this._status = this._calculateStatus();

if (emitEvent) {
ObservableWrapper.callEmit(this._statusChanges, this._status);
}

if (isPresent(this._parent)) {
this._parent._updateControlsErrors();
this._parent._updateControlsErrors(emitEvent);
}
}

Expand Down
25 changes: 22 additions & 3 deletions modules/@angular/forms/test/model_spec.ts
Expand Up @@ -7,7 +7,7 @@
*/

import {fakeAsync, flushMicrotasks, tick} from '@angular/core/testing';
import {afterEach, beforeEach, ddescribe, describe, expect, iit, inject, it, xit} from '@angular/core/testing/testing_internal';
import {afterEach, beforeEach, ddescribe, describe, iit, inject, it, xit} from '@angular/core/testing/testing_internal';
import {AsyncTestCompleter} from '@angular/core/testing/testing_internal';
import {FormArray, FormControl, FormGroup, Validators} from '@angular/forms';

Expand All @@ -34,7 +34,7 @@ export function main() {
};
}

function asyncValidatorReturningObservable(c: any /** TODO #9100 */) {
function asyncValidatorReturningObservable(c: FormControl) {
var e = new EventEmitter();
PromiseWrapper.scheduleMicrotask(() => ObservableWrapper.callEmit(e, {'async': true}));
return e;
Expand Down Expand Up @@ -581,7 +581,7 @@ export function main() {
});

describe('valueChanges', () => {
var g: any /** TODO #9100 */, c1: any /** TODO #9100 */, c2: any /** TODO #9100 */;
var g: FormGroup, c1: FormControl, c2: FormControl;

beforeEach(() => {
c1 = new FormControl('old1');
Expand Down Expand Up @@ -662,6 +662,25 @@ export function main() {
}));
});

describe('statusChanges', () => {
const control = new FormControl('', asyncValidatorReturningObservable);
const group = new FormGroup({'one': control});

// TODO(kara): update these tests to use fake Async
it('should fire a statusChange if child has async validation change',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
const loggedValues: string[] = [];
ObservableWrapper.subscribe(group.statusChanges, (status: string) => {
loggedValues.push(status);
if (loggedValues.length === 2) {
expect(loggedValues).toEqual(['PENDING', 'INVALID']);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's be awesome to change all the tests in this file to use fakeAsync, so we don't have to use this roundabout way of verifying the emitted result. But you don't have to do it right now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, seems like I should do that all at once though. I'll put it in as a TODO.

}
async.done();
});
control.updateValue('');
}));
});

describe('getError', () => {
it('should return the error when it is present', () => {
var c = new FormControl('', Validators.required);
Expand Down