Below is the code snippet I am currently working with:
$scope.deleteJob = function(job) {
SandboxService.deleteJob(job.id).then(res => {
if (res.status == 200) {
ngToast.success();
$scope.refreshApps();
}
else {
ngToast.danger();
}
});
};
Accompanied by the unit test provided here:
it('should display success toast and refresh apps after deletion', () => {
spyOn(sandboxService, 'deleteJob').and.returnValue(Promise.resolve({status: 500}));
spyOn(ngToast, 'success');
spyOn(scope, 'refreshApps');
let mockJob = {
'id': 1
};
scope.deleteJob(mockJob);
sandboxService.deleteJob().then(() => {
expect(ngToast.success).toHaveBeenCalled();
expect(scope.refreshApps).toHaveBeenCalled();
});
});
The main functionality involves displaying a success toast and refreshing the page upon successful job deletion. In case of failure, a danger toast is shown.
Although the expected result is for the test to fail due to a status of 500 being returned, surprisingly it passes. This suggests that both ngToast.success()
and scope.refreshApps()
have been triggered.
Upon inserting some debug logs into the code, it confirms that indeed status: 500
is returned, leading to execution of the else
block.
What could possibly be overlooked in this scenario?