-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessagePage.test.js
39 lines (35 loc) · 1.35 KB
/
MessagePage.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import * as api from "../../../api/adminSideApi";
import { MessagesPage } from "../MessagesPage";
import { AdminContext } from "../AdminPage";
// // we want to run a function or some other code repeatedly “before each” test that code can be put in the beforeEach function.
beforeEach(() => {
//jest.spyOn: Spy or mock a function
jest.spyOn(api, "getMessages").mockResolvedValue([]);
});
describe("render list of messages", () => {
it("load all the message data", async () => {
api.getMessages.mockResolvedValue([
{
email: "[email protected]",
id: 38,
message: "Message Mock Testing",
messaged_at: "2022-11-20T04:35:20.232Z",
name: "John",
phone_number: "",
read: false,
},
]);
// Need to add the admin context in order to provide the token value.
render(
<AdminContext.Provider value="token">
<MessagesPage />
</AdminContext.Provider>
);
await waitFor(() => expect(api.getMessages).toHaveBeenCalledTimes(1));
expect(await screen.findByText("[email protected]")).toBeInTheDocument();
expect(await screen.findByText("John")).toBeInTheDocument();
expect(await screen.findByText("Message Mock Testing")).toBeInTheDocument();
});
});