Skip to content

Jest mock function not being invoked #1389

@johnnithin08

Description

@johnnithin08

I need to have a unit test which checks whether my component dropdown handler function has been invoked after pressing any elements in the dropdown.

The error that I am getting is that the handler function is not being invoked.

I've tried using the waitfor. Didnt make any difference.

Test:

it("render NewDatePicker and check on Press", async () => {
    const mockCurrentFn = jest.fn();
   render(<NewDatePicker placeholder="Test Placeholder" mode="time" 
   setValue={mockCurrentFn} testId="test" value={undefined} />)
    fireEvent.press(await screen.findByTestId("test"));
    fireEvent.press(await screen.findByText("Confirm Date"));
    await waitFor(() => {
      expect(mockCurrentFn).toHaveBeenCalled();
    })

  });

Component:

The relevant portion where I call the handle function.

const handleConfirmDate = () => {
    setValue(selectedDate);
    handleAnimationClose();
  };

<CustomButton
     buttonStyle={defaultButtonStyle}
     onPress={handleConfirmDate}
     text={buttonText || DATE_PICKER.BUTTON_CONFIRM}
     />

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions