I have this function for which I'm writing the Unit Test.

    public showAdminAction(hraData: PhpGrid) {
        if (this.isInvalidMasterChildHealthPlanMapped(hraData.MasterChildHealthPlanId)) {
            this.showInvalidMasterChildHealthPlanPopUp(hraData);
        }
        else this.hraLockService.getHraLockDetail(hraData.HraId).then((res: any) => {
            if (res.LockedBy) {
                this.customDialogService.error(this.translateService.get("RecordLocked"), this.translateService.get("RecordLockedFromAdminAction").format(res.LockedBy));
            } else {
                this.customDialogService.createDialog(hraData, AdminActionComponent);
            }
        });
    }

   private isInvalidMasterChildHealthPlanMapped(masterChildHealthPlanId: number): boolean {
    if (this.siteService.getSiteHealthPlans() && masterChildHealthPlanId && !this.siteService.getSiteHealthPlans().find(x => x.Id == masterChildHealthPlanId)) {
        return true;
    }
    return false;
}

private showInvalidMasterChildHealthPlanPopUp(phpRowData: PhpGrid) {
    this.loggingService.log(this.translateService.get('IncorrectMasterChildHealthPlanMappedHraLog').replace('##hraId##', phpRowData.HraId.toString()).replace('##masterChildHealthPlanId##', phpRowData.MasterChildHealthPlanId.toString()).replace('##code##', MasterEventLookupTypeEnum.IncorrectMasterChildHealthPlanMappedHra.toString()), MasterEventTypeEnum.Error, MasterEventLookupTypeEnum.IncorrectMasterChildHealthPlanMappedHra, MasterLogLevelEnum.Error, MasterEventRecordTypeEnum.HRA, phpRowData.HraId);
    this.customDialogService.error(this.translateService.get("Error"), this.translateService.get("IncorrectMasterChildHealthPlanMappedHra").replace("##code##", MasterEventLookupTypeEnum.IncorrectMasterChildHealthPlanMappedHra.toString()));
}   

These are the UT's I've written

describe('showAdminAction', () => {
    it('shows error dialog when InvalidMasterChildHealthPlanMapped', () => {
        let dataHelper: PhpGridMockDataHelper = new PhpGridMockDataHelper();
        let data = dataHelper.getPhpGridDataRawFormat();
        let res: any = { LockedBy: 'TestUser' };
        let siteHealthPlan: SiteHealthPlan[] = [{ Id: 2, Name: "OTHER STANDARD | MEDICARE", HraTypeFlag: 31, DefaultHraTypeId: 1, MasterPlanTypeId: 1, MasterPlanTypeName: null, VisitTypes: null, SortOrder: 3 }];

        spyOn(customDialogService, 'error');
        spyOn(hraLockService, 'getHraLockDetail').and.returnValue(Promise.resolve(res));
        spyOn(siteService, 'getSiteHealthPlans').and.returnValue(siteHealthPlan);

        pwqGridComponent.showAdminAction(data.Rows[2]);

        expect(customDialogService.error).toHaveBeenCalled();
    });
});

describe('showAdminAction', () => {
    it('shows error dialog when validMasterChildHealthPlanMapped but locked by user', () => {
        let dataHelper: PhpGridMockDataHelper = new PhpGridMockDataHelper();
        let data = dataHelper.getPhpGridDataRawFormat();
        let res: any = { LockedBy: 'TestUser' };
        let siteHealthPlan: SiteHealthPlan[] = [{ Id: 1, Name: "OTHER STANDARD | MEDICARE", HraTypeFlag: 31, DefaultHraTypeId: 1, MasterPlanTypeId: 1, MasterPlanTypeName: null, VisitTypes: null, SortOrder: 3 }];

        spyOn(customDialogService, 'error');
        spyOn(hraLockService, 'getHraLockDetail').and.returnValue(Promise.resolve(res));
        spyOn(siteService, 'getSiteHealthPlans').and.returnValue(siteHealthPlan);

        pwqGridComponent.showAdminAction(data.Rows[2]);

        expect(customDialogService.error).toHaveBeenCalled();
    });
});

describe('showAdminAction', () => {
    it('shows custom dialog when validMasterChildHealthPlanMapped and not locked by user', () => {
        let dataHelper: PhpGridMockDataHelper = new PhpGridMockDataHelper();
        let data = dataHelper.getPhpGridDataRawFormat();
        let res: any = { LockedBy: null };
        let siteHealthPlan: SiteHealthPlan[] = [{ Id: 1, Name: "OTHER STANDARD | MEDICARE", HraTypeFlag: 31, DefaultHraTypeId: 1, MasterPlanTypeId: 1, MasterPlanTypeName: null, VisitTypes: null, SortOrder: 3 }];

        spyOn(customDialogService, 'createDialog').and.returnValue(Observable.of(true));
        spyOn(hraLockService, 'getHraLockDetail').and.returnValue(Promise.resolve(res));
        spyOn(siteService, 'getSiteHealthPlans').and.returnValue(siteHealthPlan);

        pwqGridComponent.showAdminAction(data.Rows[2]);

        expect(customDialogService.createDialog).toHaveBeenCalled();
    });
});

But I'm getting the below error for last two UT's:

FAILED PwqGrid Component showAdminAction shows error dialog when validMasterChildHealthPlanMapped but locked by user

zone.js:199 Uncaught Expected spy error to have been called. at UserContext. (http://localhost:9876/base/config/spec-bundle.js:128900:47)

FAILED PwqGrid Component showAdminAction shows custom dialog when validMasterChildHealthPlanMapped and not locked by user

zone.js:199 Uncaught Expected spy createDialog to have been called. at UserContext. (http://localhost:9876/base/config/spec-bundle.js:128913:54)

0 Answers