I'm new to angular unit testing and I don't know how to test this switch:
displayBadge(text: any) {
switch (text) {
case 'blabla':
return 'badge badge-pill badge-success';
case 'lalala':
return 'badge badge-pill badge-secondary';
case 'uuuu':
return 'badge badge-pill badge-warning';
case 'ooooo':
return 'badge badge-pill badge-warning';
case 'eeee':
return 'badge badge-pill badge-warning';
case 'zzzzz':
return 'badge badge-pill badge-dark';
case 'aaaaa':
return 'badge badge-pill badge-dark';
case 'qqqqq':
return 'badge badge-pill badge-success';
case 'ccccc':
return 'badge badge-pill badge-warning';
case 'rrrrr':
return 'badge badge-pill badge-success';
case 'ttttt':
return 'badge badge-pill badge-warning';
case 'fffff':
return 'badge badge-pill badge-success';
default:
return '';
I'm using Simontest plugin on VSCODE and the code coverage always tells me I'm missing like 80% of the coverage, which is this switch.
I've tried this way on my spec.ts file:
it('text aaaaa', () => {
component.text ='aaaaa'
component.displayBadge(component.text);
expect('badge badge-pill badge-secondary');
});
but it doesn't work.
text is defined like this in my ts:
@Input()
text!: any;
I've seen there are others posts about testing a switch, I've tried everything I've seen here on stack and in general on Google, but SimonTest always tells me this function is not tested.
CodePudding user response:
Since displayBadge() is returning the value you are testing for, something like this should work:
it('text aaaaa', () => {
expect(component.displayBadge('aaaaa')).toEqual('badge badge-pill
badge-secondary');
});