diff --git a/packages/forms/src/directives/shared.ts b/packages/forms/src/directives/shared.ts index 38a3779b7f..282be9901a 100644 --- a/packages/forms/src/directives/shared.ts +++ b/packages/forms/src/directives/shared.ts @@ -187,9 +187,13 @@ export function selectValueAccessor( dir: NgControl, valueAccessors: ControlValueAccessor[]): ControlValueAccessor|null { if (!valueAccessors) return null; + if (!Array.isArray(valueAccessors)) + _throwError(dir, 'Value accessor was not provided as an array for form control with'); + let defaultAccessor: ControlValueAccessor|undefined = undefined; let builtinAccessor: ControlValueAccessor|undefined = undefined; let customAccessor: ControlValueAccessor|undefined = undefined; + valueAccessors.forEach((v: ControlValueAccessor) => { if (v.constructor === DefaultValueAccessor) { defaultAccessor = v; diff --git a/packages/forms/test/directives_spec.ts b/packages/forms/test/directives_spec.ts index ef1fd2711e..5964b53e0f 100644 --- a/packages/forms/test/directives_spec.ts +++ b/packages/forms/test/directives_spec.ts @@ -55,6 +55,12 @@ function asyncValidator(expected: any, timeout = 0) { it('should throw when given an empty array', () => { expect(() => selectValueAccessor(dir, [])).toThrowError(); }); + it('should throw when accessor is not provided as array', () => { + expect(() => selectValueAccessor(dir, {} as any[])) + .toThrowError( + `Value accessor was not provided as an array for form control with unspecified name attribute`); + }); + it('should return the default value accessor when no other provided', () => { expect(selectValueAccessor(dir, [defaultAccessor])).toEqual(defaultAccessor); }); diff --git a/packages/forms/test/spies.ts b/packages/forms/test/spies.ts index e956a4821c..1f6b3940c7 100644 --- a/packages/forms/test/spies.ts +++ b/packages/forms/test/spies.ts @@ -16,6 +16,6 @@ export class SpyChangeDetectorRef extends SpyObject { } } -export class SpyNgControl extends SpyObject {} +export class SpyNgControl extends SpyObject { path = []; } export class SpyValueAccessor extends SpyObject { writeValue: any; }