Unit test cases for nested api calls in Angular.

huangapple go评论61阅读模式
英文:

Unit test cases for nested api calls in Angular

问题

I'm trying to write unit test cases for nested API calls and don't know how to write unit test cases for the following scenario.

this.openService
      .createDocument(PIReq, this.id)
      .subscribe({
        next: (res1) => {
          const applicationReq = AppUtil.getApplicationRequest(
            this.openService.personDetails,
            res1.id
          );
          this.openService
            .createPersonAppliation(applicationReq, this.id)
            .subscribe({
              next: (res2) => {
                const personAppReqInfo: PersonApplicationIds = {
                  documentId: res1.id,
                  personApplicationId: res2.id,
                };
                
                if (this.openDataStoreService.details.length > 0) {
                  this.submitApplicationRequest();
                  this.invokeRequest();
                } else {
                  this.invokeRequest();
                }
              },
              error: () => {
                this.showSpinner = false;
              },
            });
        },
        error: () => {
          this.showSpinner = false;
        },
     });

Here is what I've tried

let sampleApplicationResponse!: ApplicationResponse;
jest.spyOn(openService, 'createPersonAppliation').mockReturnValueOnce(of(sampleApplicationResponse));
    openService.createPersonAppliation(applicationReq, component.id)
      .subscribe({
        next: applicationRes => {
          expect(applicationRes).toBe(sampleApplicationResponse);
        }
      });

This test case works, but the problem is that this test case doesn't contribute to anything in the coverage, any way to test all the lines in the nested API call? Jest/Jasmine anything is fine.

英文:

I'm trying to write unit test cases for nested API calls and don't know how to write unit test cases for the following scenario.

this.openService
      .createDocument(PIReq, this.id)
      .subscribe({
        next: (res1) => {
          const applicationReq = AppUtil.getApplicationRequest(
            this.openService.personDetails,
            res1.id
          );
          this.openService
            .createPersonAppliation(applicationReq, this.id)
            .subscribe({
              next: (res2) => {
                const personAppReqInfo: PersonApplicationIds = {
                  documentId: res1.id,
                  personApplicationId: res2.id,
                };
                
                if (this.openDataStoreService.details.length > 0) {
                  this.submitApplicationRequest();
                  this.invokeRequest();
                } else {
                  this.invokeRequest();
                }
              },
              error: () => {
                this.showSpinner = false;
              },
            });
        },
        error: () => {
          this.showSpinner = false;
        },
     });

Here is what I've tried

let sampleApplicationResponse!: ApplicationResponse;
jest.spyOn(openService, 'createPersonAppliation').mockReturnValueOnce(of(sampleApplicationResponse));
    openService.createPersonAppliation(applicationReq, component.id)
      .subscribe({
        next: applicationRes => {
          expect(applicationRes).toBe(sampleApplicationResponse);
        }
      });

This test case works, but the problem is that this test case doesn't contribute to anything in the coverage, any way to test all the lines in the nested api call? Jest/Jasmine anything is fine

答案1

得分: 0

After some research and help from my friends, I figured out a way. You can async if you want

it("nested api calls, async () => {
  const spy1 = jest.spyOn(openService, "createDocument").mockReturnValueOnce(of(sampleResponse));

  const spy2 = jest.spyOn(openService, "createPersonAppliation").mockReturnValueOnce(of(sampleResponse));

  component.submit();

  expect(spy1).toBeCalled();
  expect(spy2).toBeCalled();
});
英文:

After some research and help from my friends, I figured out a way. You can async if you want

it("nested api calls, async () => {
  const spy1 = jest.spyOn(openService, "createDocument").mockReturnValueOnce(of(sampleResponse));

  const spy2 = jest.spyOn(openService, "createPersonAppliation").mockReturnValueOnce(of(sampleResponse));

  component.submit();

  expect(spy1).toBeCalled();
  expect(spy2).toBeCalled();
});

huangapple
  • 本文由 发表于 2023年4月11日 02:13:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75979600.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定