Unit testing angular async promise

Viewed 34

I have some hard times trying to resolve some unit testing. I succeed in testing the function if params.callType === 'inbound' but don't know how to test if params.calltype === "something else". Here is the code I have to test:

searchObject(params: ISearchObjectParams): Promise<IScreenPopSearchObjectResult> {
return new Promise((resolve, reject) => {
  let searchParams;
  if (params.searchValuesString) {
    searchParams = params.searchValuesString;
  } else {
    const quotedSearchValues = params && params.searchValues && params.searchValues.map(x => '"' + x + '"');
    searchParams = quotedSearchValues && quotedSearchValues.join(' OR ');
  }
  if (params.callType === 'inbound') {
    sforce.opencti.searchAndScreenPop({
      searchParams: searchParams,
      queryParams: params.queryParams,
      callType: params.callType,
      deferred: true,
      callback: response => {
        if (response.success) {
          resolve(this.createSearchObjectResult(response));
        } else {
          reject(this.parseLightningError(response, LocaleKeys.searchObject));
        }
      }
    });
  }
});

Here is the unit test I made that is working for the "if (params.callType === 'inbound').

it('should resolve with parsed search result', async () => {
      service.createSearchObjectResult = jasmine.createSpy().and.returnValue({});
      sforce.opencti.searchAndScreenPop = jasmine.createSpy().and.callFake((params: {
        callback: (response: { returnValue: { runApex: string }, success: boolean }) => void
      }) => {
        params.callback({ success: true } as any);
      });
      await expectAsync(service.searchObject({ searchValues: ['value1', 'value2'], callType: 'inbound' } as any)).toBeResolvedTo({} as any);
      testUtils.expectSingleCall(service.createSearchObjectResult, { success: true });
    });

I'm trying to make it for the else, but I can't succeed. This is what I tried for params.callType === "outbound"(that should be the else condition):

  it('should not resolve if is not inbound', async () => {
      sforce.opencti.searchAndScreenPop = jasmine.createSpy().and.callFake((params: {
        callback: (response: { returnValue: { runApex: string }, success: boolean }) => void
      }) => {
        params.callback({ success: false } as any);
      });
      await expectAsync(service.searchObject({ searchValuesString: 'value1&&value2', callType: 'inbound' } as any)).toBeRejected({} as any);
      testUtils.expectNotCalled(sforce.opencti.searchAndScreenPop);
      expect(sforce.opencti.searchAndScreenPop).not.toHaveBeenCalled();
    });

Suggestions please?

1 Answers

If the Promise-Rejection isn't cached, it will bubble up and throw the error. So maybe you should add an expectation for the error like

expect(service.searchObject).toThrow();
Related