1

The below method calls CheckLastEmployee method which returns true/false.

async nextEmployee(empId:number) {
    let isLast;
    await this.CheckLastEmployee(empId).then(x=>{isLast=x});
    if (isLast) {
        Submit();
    }
}

More logic inside CheckLastEmployee....copied a part of the code..

async CheckLastEmployee(empId:number){
    let enrollment = new Map<string, boolean>();
    await this.GetEnrolledPrograms().then(res => enrollment = res);
    if(empId==10)
        return true;
    else
        return false;
}

Test case .spec.ts

 it('Is Last Employee-submit',()=>{
        spyOn(component,'CheckLastEmployee').and.returnValue(Promise.resolve(true));
        spyOn(component,'Submit');
        component.nextEmployee(10);
        expect(component.Submit).toHaveBeenCalled();
    }

the above test case fails.

PRI
  • 23
  • 5
  • If you're using Jest the try `spyOn(component,'CheckLastEmployee').mockResolvedValue(true)`. See https://jestjs.io/docs/mock-function-api#mockfnmockresolvedvaluevalue – Benny Halperin Jul 14 '22 at 06:25
  • i am using jasmine and karma...any solution with jasmine? – PRI Jul 14 '22 at 06:29
  • I can't see that you call `nextEmployee()` in your test – Benny Halperin Jul 14 '22 at 06:38
  • that was a typo...I have called. Edited the question now – PRI Jul 14 '22 at 06:58
  • `CheckLastEmployee` does not return a promise – Benny Halperin Jul 14 '22 at 08:27
  • yes. but have used this.CheckLastEmployee(empId).then(x=>{isLast=x}); So in .spec file, I get error Argument of type 'boolean' is not assignable to parameter of type 'Promise' if i try spyOn(component,'CheckLastEmployee').and.returnValue(true); – PRI Jul 14 '22 at 09:02

1 Answers1

0

I think your test is missing async/await. Furthermore, I think in order to be able to see if your submit method was called, you need to spy on that method as well. Please try the following implementation to see if it solves your issue:

it('Is Last Employee-submit', fakeAsync( () => {
    spyOn(component, 'CheckLastEmployee').and.resolveTo(true);
    let spy = spyOn(component, 'submit');  // Spy on submit method
    component.nextEmployee(10);
    tick(); // Try this
    expect(spy).toHaveBeenCalled(); // Now expect the spy to have been called if isLast is defined
}));

In case the above test didnt work, you can try tick(1000)(1000 milliseconds) or another value as well to just see if the test is failing due to a race condition or not.

Side note:

You need to import tick and fakeAsync:

import {fakeAsync, tick} from '@angular/core/testing';
ilpianoforte
  • 1,180
  • 1
  • 10
  • 28
  • tried async/await -no change.... resolveTo(null) ->Argument of type 'null' is not assignable to parameter of type 'boolean | undefined'. So I tried resolveTo(true)....still test case fails I have include spy for Submit method too....missed to include in the question before.. – PRI Jul 14 '22 at 07:54
  • @PriyaK Can you please include the error message when you use `resolveTo(true)`? – ilpianoforte Jul 14 '22 at 07:59
  • @PriyaK Also please share the `CheckLastEmployee` method in your question – ilpianoforte Jul 14 '22 at 08:00
  • Expected spy Submit to have been called. - error msg – PRI Jul 14 '22 at 08:11
  • @PriyaK I just found another flaw. It should be `await component.nextEmployee(10);` instead of `await nextEmployee(10);`. Could you please try this? – ilpianoforte Jul 14 '22 at 08:20
  • that another mistake on my cope-paste...its actually component.nextExmployee(10)... – PRI Jul 14 '22 at 08:48
  • @PriyaK I updated my answer one last time :)). I hope it finally works. – ilpianoforte Jul 14 '22 at 09:41