I'm trying to simply pass the test (should create - component=> toBeTruthy) for the custom component (angular component class) implemented with ControlValueAccessor. I've injected NgControl in constructor and pass the this keyword.
constructor(@Optional() @Self() public controlDir?: NgControl) {
this.controlDir.valueAccessor = this;
}
And then on ngOnInit()
ngOnInit() {
const control = this.controlDir.control;
const validators = control.validator ? [control.validator] : [];
const asyncValidators = control.asyncValidator ? [control.asyncValidator] : [];
control.setValidators(validators);
control.setAsyncValidators(asyncValidators);
control.updateValueAndValidity();
}
In the test file
describe('TextInputComponent', () => {
let component: TextInputComponent;
let fixture: ComponentFixture<TextInputComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [TextInputComponent],
imports: [ReactiveFormsModule]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(TextInputComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
And the errors
TypeError: Cannot set property 'valueAccessor' of null
I'd be appreciated if anyone shares your ideas to solve this issue?
Thank you. Rabin