After making a minor change to an existing function, it has been flagged as new code during our quality checks. This means I need to create a unit test specifically for the new 4 lines of code. The challenge is that there was never a unit test in place for this function which happens to be quite large!
I've attempted various methods such as mocking services, variables, and spying but I keep running into errors. Being relatively new to jasmine, I am finding it challenging. All I really want to do is create some kind of check to validate the truthiness or falsiness of those added lines.
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 attempted to set isBrilliant()
with a mock value of true
, expecting the value to be truthy
expect(component.isBrilliant()).toBeTruthy();
In the spec file, I tried:
const isBrilliant = true;
component.isBrilliant = isBrilliant;
However, this resulted in an error stating
Type 'true' is not assignable to type '() => boolean'.
If any experienced jasmine developer could provide me with a simple solution to achieve some coverage for this straightforward statement, I would greatly appreciate it. Thank you
UPDATE:
I have managed to set isBrilliant()
to be true or false. Now, I need to verify whether the correct string has been pushed into the array r? Any suggestions?