mercredi 16 décembre 2020

How to test react component with Jest

I am using Jest and Enzyme to test React components, Now I want to test the given Year element inside given function. I can test element which comes from API. But How can I test object which decleared inside test function.

let Years= [
  {
    SubjectId: 1,
    SubjectName: "1st Year"
  }, {
    SubjectId: 2,
    SubjectName: "2nd Year"
  }, {
    SubjectId: 3,
    SubjectName: "3rd Year"
  }, {
    SubjectId: 4,
    SubjectName: "4th Year"
  }, {
    SubjectId: 5,
    SubjectName: "5th Year"
  }
]
test("Officer filter lectures button works", async () => {
  const mockGetLectures = jest.spyOn(API, "getOfficerSchedule");
  mockGetLectures.mockReturnValue(new Promise((resolve) => resolve(Courses)));

  render(<ModifyLecture notLoggedUser={mockNotLoggedUser} />);

  await waitFor(() => expect(mockGetLectures).toHaveBeenCalledTimes(1));
  await waitFor(() => expect(mockNotLoggedUser).toHaveBeenCalledTimes(0));

  mockGetLectures.mockReturnValue(new Promise((resolve) => resolve(Year)));
  userEvent.click(screen.getAllByTestId("handlelecture-button")[1], leftClick);

});

Aucun commentaire:

Enregistrer un commentaire