Javascript testing: Mocking dispatch and get state

317 Views Asked by At

The test should be checking if inbox.get is getting called with current user id 12345 for currentuser filter criteria.

When I run the tests, it's not able to enter return(dispatch, getstate), out of the two console logs that i added here only Console.log(--------test 1-----------) is being printed. Second one is not. So im thinking that the problem is with how im mocking dispatch and getState.

Code:

function loadInboxSummary(paginationOffset) 
  Console.log(--------test 1-----------);
  {return (dispatch, getState) => {
    Console.log(--------testing inside 2 ---------);
    const state = getState();
    const userId = getCurrentUserId(state);
    const filterCriteria = getCurrentFilter(state);

    const adjustedInboxViewFilter = {
      ...filterCriteria,
      assignedUserIds: mapCurrentAssignee(userId),
    };

    return InboxItems.get(
      userId,
      adjustedInboxViewFilter,
      paginationOffset,
    ) ................

Testing when InboxItems.get(..) is called, it has correct parameters for adjustedInboxView.

Test:

describe("loadInboxSummary", () => {
  const dispatch = jest.fn();
  const getState = jest.fn().mockReturnValue("mock_state");

  beforeEach(() => {
    jest.spyOn(getCurrentUserDetails, "getCurrentUserId").mockReturnValue("12345");
    jest.spyOn(getCurrentFilter, "default").mockReturnValue({assignedUserIds: ["currentUser"]});
    jest.spyOn(InboxItems, "get").mockResolvedValue({});
  });

  it("calls InboxItems.get with current user id", () => {
    loadInboxSummary("mock_pagination")(dispatch, getState);
    const adjustedFilters = {
      assignedUserIds: ["12345"],
    };

    expect(InboxItems.get).toHaveBeenCalledWith(
      "12345",
      adjustedFilters,
      "mock_pagination",
    ); }); });
Error: expect(jest.fn()).toHaveBeenCalledWith(...expected)
Expected: "12345", {"assignedUserIds": ["12345"]}, "mock_pagination"
Number of calls: 0

This is my first time doing front end testing, I searched stack overflow for possible solutions but they looked quite different/had async functions so I couldn't understand them. Would really appreciate some help. Not sure what could be causing this error so feel free to ask questions.

0

There are 0 best solutions below