new to Angular and testing.
In this method in the main code
private isReIndexActive(view: ViewShortDto): boolean {
return view.dataFlowTaskName && this.isViewCompleted(view);
}
I created a test case in the
it('should check if Re-Run Index is not active', () => {
const isActionEnabled = service.itemActions[4].isActive(SHORT_VIEWS[3]);
expect(isActionEnabled).toBeFalse();
});
and the mock data is
{
systemName: 'xxx-xxx-xxx',
name: 'Demo',
step: 6,
indexStatus: 'Completed',
dataFlowTaskName: null,
}
when I run the unit test, the the output result is
expected null to be false.
however the return type in the main code is boolean. why is this error being shown?
Changed the code to
tobeFalsy()
however, the code review told it is not best practise.
view.dataFlowTaskName && this.isViewCompleted(view);
will return null
because view.dataFlowTaskName
is null
-> null
is falsy -> the operator short-circuits and returns the first falsy value. Congratulations - you've found a bug in your application. Apparently it doesn't expect that the value could be null
since the method is typed as returning boolean
. Most likely you don't have strictNullChecks
enabled in the TS compiler options which would have notified you about this. return
help fix this issue? such as const variable: boolean = view.dataFlowTaskName; return variable && this.isViewCompleted(view);