1

I'm trying to write a test. That test looks like this:

it("throws expected error when invoke action promise is rejected", async () => {
    const datadogWarnSpy = jest.spyOn(datadogLogs.logger, "warn");
    const invokeActionSpy = jest
      .spyOn(flex.Actions, "invokeAction")
      .mockRejectedValue("no active call");

    const SUT = withConference((props: WithConferenceProps) => (
      <button onClick={() => props.outboundConference.controls.endCall()}>
        End Call
      </button>
    ));

    task.conference.liveWorkerCount = 1;
    render(
      <SUT flex={flex} task={task} conferenceService={conferenceService} />,
      { store }
    );

    userEvent.click(screen.getByText(/end call/i));

    await waitFor(() => {
      expect(invokeActionSpy).toThrow("no active call");
    });

    await waitFor(() => {
      expect(datadogWarnSpy).toHaveBeenCalled();
    });
});

In my test, I click a button that triggers an asynchronous piece of code to run. That code looks like this.

private endCall = async (other?: {
      taskSid: string;
      reservationSid: string;
}): Promise<void> => {
  try {
    const { task, flex } = this.props;
    
    if (
      this.getParticipants(other?.reservationSid).length ||
      task.conference?.liveWorkerCount
    ) {
      const payload = other?.reservationSid
        ? { sid: other.reservationSid }
        : { task };
        await flex.Actions.invokeAction("HangupCall", payload);
      }
    } catch (e: any) {
      if (e.messsage === NO_ACTIVE_CALL_MSG) {
        datadogLogs.logger.warn(`endCall: ${e.message}`);
      } else {
        datadogLogs.logger.error(`endCall: ${e.message}`);
      }
    } finally {
        this.endRecording(other?.taskSid);
    }
};

I want to verify that, when this button is clicked, and the exception message equals a specific string (ie NO_ACTIVE_CALL_MSG), that the correct datadog logger method (in this case, "warn") is called. The problem I'm dealing with is that the caught exception object (e) is always coming in as undefined in my test. Thus the wrong logger method is called and my test fails. Does anyone have any ideas a) why the exception is always undefined and b) what I should do to fix this problem in my test? The code under test works as expected when it is run.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Wynn
  • 173
  • 1
  • 14

0 Answers0