forked from help-me-mom/ng-mocks
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(#246): auto spy covers control value accessor too
- Loading branch information
Showing
11 changed files
with
465 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// tslint:disable variable-name ban-ts-ignore | ||
|
||
import { AsyncValidator, ControlValueAccessor, ValidationErrors, Validator } from '@angular/forms'; | ||
|
||
import { AnyType } from '../common/core.types'; | ||
|
||
import { MockControlValueAccessor } from './mock-control-value-accessor'; | ||
|
||
const appyProxy = (proxy: any, method: string, value: any, storage?: string) => { | ||
if (proxy.instance && storage) { | ||
proxy.instance[storage] = value; | ||
} | ||
if (proxy.instance && proxy.instance[method]) { | ||
return proxy.instance[method](value); | ||
} | ||
}; | ||
|
||
export class MockControlValueAccessorProxy implements ControlValueAccessor { | ||
public instance?: Partial<MockControlValueAccessor & ControlValueAccessor>; | ||
|
||
public constructor(public readonly target?: AnyType<any>) {} | ||
|
||
public registerOnChange(fn: any): void { | ||
appyProxy(this, 'registerOnChange', fn, '__simulateChange'); | ||
} | ||
|
||
public registerOnTouched(fn: any): void { | ||
appyProxy(this, 'registerOnTouched', fn, '__simulateTouch'); | ||
} | ||
|
||
public setDisabledState(isDisabled: boolean): void { | ||
appyProxy(this, 'setDisabledState', isDisabled); | ||
} | ||
|
||
public writeValue(value: any): void { | ||
appyProxy(this, 'writeValue', value); | ||
} | ||
} | ||
|
||
export class MockValidatorProxy implements Validator { | ||
public instance?: Partial<MockControlValueAccessor & Validator>; | ||
|
||
public constructor(public readonly target?: AnyType<any>) {} | ||
|
||
public registerOnValidatorChange(fn: any): void { | ||
appyProxy(this, 'registerOnValidatorChange', fn, '__simulateValidatorChange'); | ||
} | ||
|
||
public validate(control: any): ValidationErrors | null { | ||
if (this.instance && this.instance.validate) { | ||
return this.instance.validate(control); | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
|
||
export class MockAsyncValidatorProxy implements AsyncValidator { | ||
public instance?: Partial<MockControlValueAccessor & AsyncValidator>; | ||
|
||
public constructor(public readonly target?: AnyType<any>) {} | ||
|
||
public registerOnValidatorChange(fn: any): void { | ||
appyProxy(this, 'registerOnValidatorChange', fn, '__simulateValidatorChange'); | ||
} | ||
|
||
public validate(control: any): any { | ||
if (this.instance && this.instance.validate) { | ||
const result: any = this.instance.validate(control); | ||
|
||
return result === undefined ? Promise.resolve(null) : result; | ||
} | ||
|
||
return Promise.resolve(null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,25 @@ | ||
// tslint:disable variable-name ban-ts-ignore | ||
|
||
import { AbstractControl, ControlValueAccessor, ValidationErrors, Validator } from '@angular/forms'; | ||
|
||
import { Mock } from './mock'; | ||
|
||
export class MockControlValueAccessor extends Mock implements ControlValueAccessor, Validator { | ||
public readonly __ngMocksMockControlValueAccessor: true = true; | ||
export class MockControlValueAccessor extends Mock { | ||
public get __ngMocksMockControlValueAccessor(): true { | ||
return true; | ||
} | ||
|
||
// istanbul ignore next | ||
// @ts-ignore | ||
public __simulateChange = (value: any) => {}; | ||
|
||
// istanbul ignore next | ||
public __simulateTouch = () => {}; | ||
|
||
// istanbul ignore next | ||
public __simulateValidatorChange = () => {}; | ||
|
||
public registerOnChange(fn: (value: any) => void): void { | ||
this.__simulateChange = fn; | ||
} | ||
|
||
public registerOnTouched(fn: () => void): void { | ||
this.__simulateTouch = fn; | ||
public __simulateChange(value: any) { | ||
// nothing to do. | ||
} | ||
|
||
public registerOnValidatorChange(fn: () => void): void { | ||
this.__simulateValidatorChange = fn; | ||
// istanbul ignore next | ||
public __simulateTouch() { | ||
// nothing to do. | ||
} | ||
|
||
// @ts-ignore | ||
public setDisabledState(isDisabled: boolean): void {} | ||
|
||
// @ts-ignore | ||
public validate(control: AbstractControl): ValidationErrors | null { | ||
return null; | ||
// istanbul ignore next | ||
public __simulateValidatorChange() { | ||
// nothing to do. | ||
} | ||
|
||
// @ts-ignore | ||
public writeValue(value: any) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.