import React from "react";
import { render, waitFor } from "@testing-library/react";
import axios from "axios";
import ax from "../conf/ax";
import conf from "../conf/main";
import AllCourse from "../pages/allCourse";
jest.mock("../conf/ax");
jest.mock("../conf/main");
describe("AllCourse component", () => {
test("fetches likeMost data and renders course components", async () => {
// Mocking the response for likeMost
axios.get.mockResolvedValueOnce({
data: {
data: [
{ id: 1, title: "Course 1" },
{ id: 2, title: "Course 2" },
],
},
});
// Rendering the component
const { getByText } = render(<AllCourse />);
await waitFor(() => {
// Expecting the course titles to be rendered
expect(getByText("Course 1")).toBeInTheDocument();
expect(getByText("Course 2")).toBeInTheDocument();
});
});
test("fetches newest data and renders newest course components", async () => {
// Mocking the response for newest
axios.get.mockResolvedValueOnce({
data: {
data: [
{ id: 3, title: "New Course 1" },
{ id: 4, title: "New Course 2" },
],
},
});
// Rendering the component
const { getByText } = render(<AllCourse />);
// Waiting for the component to render
await waitFor(() => {
// Expecting the newest course titles to be rendered
expect(getByText("New Course 1")).toBeInTheDocument();
expect(getByText("New Course 2")).toBeInTheDocument();
});
});
});
I want to test 2 case, One is can render to get like most course. Next one, it get newest data. I'm beginer to learn about this. So, I dont know how to write good test case code for react. I try to removed import axios but It's not work. it's failed next import (mockAdapter)