Reputation: 141
I have this snippet of code:
exportData(type: 'config' | 'equipment'): Observable<any> {
url = '';
if (type === 'config') url = `${path}Exportconfig`;
else if (type === 'equipment') url = `${path}ExportEquipment`;
return httpClient(...);
}
I mocked the tests but still having this message in sonar branches should have sufficient coverage by tests
Here are my tests
it('export configuration', () => {
spyOn(service, 'exportData');
const type = 'config';
service.exportData(type)
expect(service.exportData).toHaveBeenCalledWith('config');
})
The same thing has been done for the other value of type. I think I'm missing something on the coverage for the conditions. Any thoughts?
Upvotes: 0
Views: 290
Reputation: 1374
You need to modify your test case like this:
it('export configuration', () => {
spyOn(service, 'exportData');
// This will serve the 'if' condition
const type = 'config';
service.exportData(type)
expect(service.exportData).toHaveBeenCalledWith('config');
// This will serve the 'else-if' condition
const type2 = 'equipment';
service.exportData(type2)
expect(service.exportData).toHaveBeenCalledWith('equipment');
// This will serve the 'else' condition
const type3 = 'not-equipment-not-config';
service.exportData(type3)
expect(service.exportData).toHaveBeenCalledWith('not-equipment-not-config');
})
Upvotes: 1
Reputation: 141
So apparently adding a final case with a closing *else with cover all branches. Final function should look like this:
exportData(type: 'config' | 'equipment'): Observable<any> {
url = '';
if (type === 'config') url = `${path}Exportconfig`;
else if (type === 'equipment') url = `${path}ExportEquipment`;
else return null;
return httpClient(...);
}
Upvotes: 0