While running test cases for the EditFlag component in Angular, I encountered an error stating TypeError: Cannot read property 'client' of undefined. Additionally, I am looking to add a test case for a switch case function. Can someone assist me with this particular test case?
editflag.component.ts
constructor(private flagService: FlagService,
private editFlagDialog: MatDialogRef<EditFlagComponent>),
@Inject(MAT_DIALOG_DATA) public data: any,
private dialog: MatDialog){}
ngOnInit: void {
switch(this.data.action) {
case 'create':
this.flagClone = this.initializeFlagClone();
break;
case 'edit':
this.flagEdit();
break;
default:
console.log('No action');
}
}
public flagEdit(){
const res = {...this.data};
this.flagVar = { ...res.flag};
if(this.data.flag.client == null ) {
this.isSearch = false;
this.initializeClient();
}
}
editflag.spec.ts
const testData = {
"flag":{
id: null, //string
description: null, //string
url: null, //string
client: null
},
"action": "edit"
};
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule
],
declarations: [
EditFlagComponent
],
providers: [
HttpClient,
HttpHandler,
{provide: MatDialog, useValue: {} },
{provide: MatDialogRef, useValue: {} },
{provide: MAT_DIALOG_DATA, useValue: {testData} },
]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(EditFlagComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should call flagEdit',() => {
component.flag.client = testData.flag.client;
component.flagEdit();
fixture.detectChanges();
expect(component.isSearch).tobeFalsy();
});
I also hope that the transition block mentioned below will be covered in the summary:
switch(this.data.action) {
case 'create':
this.flagClone = this.initializeFlagClone();
break;
case 'edit':
this.flagEdit();
break;
default:
console.log('No action');
}