I am new for unit test case in angular.I am trying to write unit test case for angular reactive fomr. But, It is not working properly.
Getting below error some times
Error: NG01052: formGroup expects a FormGroup instance. Please pass one in. Example: <div [formGroup]=”myGroup”> In your class: this.myGroup = new
FormGroup({ firstName: new FormControl() });
3 test case also failed. How to resolve these issue. If anyone knows, Please help to find the solution.
my-input.component.spec.ts:
import {
TestBed,
ComponentFixture
} from '@angular/core/testing';
import { provideMockStore } from '@ngrx/store/testing';
import { MyInputComponent } from './my-input.component';
import { FormBuilder, ReactiveFormsModule, Validators } from '@angular/forms';
describe('MyInputComponent', () => {
let component: MyInputComponent;
let fixture: ComponentFixture<MyInputComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
providers: [ReactiveFormsModule, provideMockStore()],
}).compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(MyInputComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should call ngOnInit', () => {
const expectedFormGroup = new FormBuilder().group({
fullname: ['', Validators.required],
username: ['', Validators.required],
email: ['', Validators.required]
});
component.form=expectedFormGroup;
expect(component.form).toEqual(expectedFormGroup);
});
it('should call to set form value', () => {
component.form.setValue({
fullname:'test' ,
username: 'testd' ,
email: '[email protected]'
});
expect(component.form.valid).toEqual(true);
});
});
Demo: https://stackblitz.com/edit/stackblitz-starters-jtk35tru?file=src%2Fmy-input-component%2Fmy-input.component.spec.ts