I have added a minor amendment within an existing function. Our quality checks have identified this as new code therefore I need to cover the new 4 lines with a unit test - within mind there was never a unit test there to begin with and the function I added to is pretty large!
I've tried a number of ways to try and mock services, variables, spying etc... but always get errors. I'm new to jasmine so struggling. All I need to do is get any sort of check to cover the lines truthy/falsy.
component.ts file
hasDescription() {
return this.isBrilliant() || this.isCool();
}
isBrilliant() {
return this.service.Type.description == 'Brilliant';
}
isCool() {
return this.service.Type.description == 'Cool';
}
onExportToExcel(): void {
var r = [];
// added the below if/else - any check on this will be suffice.
if (this.hasDescription()) {
if (!this.isBrilliant()) {
r.push('This is Cool');
} else {
r.push('This is Brilliant');
}
if (!this.isBrilliant()) {
r.push('More Cool content');
}
}
}
I tried to set isBrilliant() with a mock value of true, and expect that the value to be truthy expect(component.isBrilliant()).toBeTruthy();
I tried to set this in the spec file by:
const isBrilliant = true;
component.isBrilliant = isBrilliant;
however the error I got here was that Type 'true' is not assignable to type '() => boolean'.
If any experienced jasmine dev can show me a quick way to get some coverage for this simple statement that would be appreciated. Thanks
UPDATE:
I can get isBrilliant() to be true or false. I now need to check the array r to see if the correct string has been .push into it? any ideas?