Skip to content

Commit e9a4364

Browse files
committed
Update test cases for useAuth and useMatch
1 parent 2eac2d9 commit e9a4364

File tree

4 files changed

+121
-18
lines changed

4 files changed

+121
-18
lines changed

frontend/src/components/Navbar/Navbar.test.tsx

Lines changed: 83 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { fireEvent, render, screen } from "@testing-library/react";
22
import axios from "axios";
33
import { faker } from "@faker-js/faker";
4-
import * as hooks from "../../contexts/AuthContext";
4+
import * as authHooks from "../../contexts/AuthContext";
5+
import * as matchHooks from "../../contexts/MatchContext";
56
import Navbar from ".";
67
import { MemoryRouter } from "react-router-dom";
78

@@ -16,6 +17,24 @@ jest.mock("react-router-dom", () => ({
1617
useNavigate: () => mockUseNavigate(),
1718
}));
1819

20+
beforeEach(() => {
21+
jest.spyOn(matchHooks, "useMatch").mockImplementation(() => ({
22+
findMatch: jest.fn(),
23+
stopMatch: () => mockUseNavigate("/home"),
24+
acceptMatch: jest.fn(),
25+
rematch: jest.fn(),
26+
retryMatch: jest.fn(),
27+
matchingTimeout: jest.fn(),
28+
matchOfferTimeout: jest.fn(),
29+
verifyMatchStatus: jest.fn(),
30+
matchUser: null,
31+
matchCriteria: null,
32+
partner: null,
33+
matchPending: false,
34+
loading: false,
35+
}));
36+
});
37+
1938
describe("Navigation routes", () => {
2039
it("Question route is present", () => {
2140
const username = faker.internet.userName();
@@ -41,7 +60,7 @@ describe("Navigation routes", () => {
4160
},
4261
},
4362
});
44-
jest.spyOn(hooks, "useAuth").mockImplementation(() => ({
63+
jest.spyOn(authHooks, "useAuth").mockImplementation(() => ({
4564
signup: jest.fn(),
4665
login: jest.fn(),
4766
logout: jest.fn(),
@@ -71,7 +90,7 @@ describe("Navigation routes", () => {
7190
describe("Unauthenticated user", () => {
7291
it("Sign up button is rendered", () => {
7392
mockedAxios.get.mockResolvedValue({ data: { data: null } });
74-
jest.spyOn(hooks, "useAuth").mockImplementation(() => ({
93+
jest.spyOn(authHooks, "useAuth").mockImplementation(() => ({
7594
signup: jest.fn(),
7695
login: jest.fn(),
7796
logout: jest.fn(),
@@ -89,7 +108,7 @@ describe("Unauthenticated user", () => {
89108

90109
it("Login button is rendered", () => {
91110
mockedAxios.get.mockResolvedValue({ data: { data: null } });
92-
jest.spyOn(hooks, "useAuth").mockImplementation(() => ({
111+
jest.spyOn(authHooks, "useAuth").mockImplementation(() => ({
93112
signup: jest.fn(),
94113
login: jest.fn(),
95114
logout: jest.fn(),
@@ -131,7 +150,7 @@ describe("Authenticated user", () => {
131150
},
132151
},
133152
});
134-
jest.spyOn(hooks, "useAuth").mockImplementation(() => ({
153+
jest.spyOn(authHooks, "useAuth").mockImplementation(() => ({
135154
signup: jest.fn(),
136155
login: jest.fn(),
137156
logout: jest.fn(),
@@ -181,7 +200,7 @@ describe("Authenticated user", () => {
181200
},
182201
},
183202
});
184-
jest.spyOn(hooks, "useAuth").mockImplementation(() => ({
203+
jest.spyOn(authHooks, "useAuth").mockImplementation(() => ({
185204
signup: jest.fn(),
186205
login: jest.fn(),
187206
logout: jest.fn(),
@@ -235,7 +254,7 @@ describe("Authenticated user", () => {
235254
},
236255
},
237256
});
238-
jest.spyOn(hooks, "useAuth").mockImplementation(() => ({
257+
jest.spyOn(authHooks, "useAuth").mockImplementation(() => ({
239258
signup: jest.fn(),
240259
login: jest.fn(),
241260
logout: jest.fn(),
@@ -287,7 +306,7 @@ describe("Authenticated user", () => {
287306
},
288307
},
289308
});
290-
jest.spyOn(hooks, "useAuth").mockImplementation(() => ({
309+
jest.spyOn(authHooks, "useAuth").mockImplementation(() => ({
291310
signup: jest.fn(),
292311
login: jest.fn(),
293312
logout: jest.fn(),
@@ -316,4 +335,60 @@ describe("Authenticated user", () => {
316335
screen.getByRole("menuitem", { name: "Logout" })
317336
).toBeInTheDocument();
318337
});
338+
339+
it("Stop matching button is rendered", () => {
340+
const username = faker.internet.userName();
341+
const firstName = faker.person.firstName();
342+
const lastName = faker.person.lastName();
343+
const email = faker.internet.email();
344+
const biography = faker.person.bio();
345+
const profilePictureUrl = "";
346+
const createdAt = "";
347+
const isAdmin = false;
348+
mockedAxios.get.mockResolvedValue({
349+
data: {
350+
data: {
351+
id: "1",
352+
username,
353+
firstName,
354+
lastName,
355+
email,
356+
biography,
357+
profilePictureUrl,
358+
createdAt,
359+
isAdmin,
360+
},
361+
},
362+
});
363+
jest.spyOn(authHooks, "useAuth").mockImplementation(() => ({
364+
signup: jest.fn(),
365+
login: jest.fn(),
366+
logout: jest.fn(),
367+
loading: false,
368+
setUser: jest.fn(),
369+
user: {
370+
id: "1",
371+
username,
372+
firstName,
373+
lastName,
374+
email,
375+
profilePictureUrl,
376+
biography,
377+
createdAt,
378+
isAdmin,
379+
},
380+
}));
381+
render(
382+
<MemoryRouter initialEntries={["/matching"]}>
383+
<Navbar />
384+
</MemoryRouter>
385+
);
386+
const stopMatchingButton = screen.getByRole("button", {
387+
name: "Stop matching",
388+
});
389+
expect(stopMatchingButton).toBeInTheDocument();
390+
391+
fireEvent.click(stopMatchingButton);
392+
expect(mockUseNavigate).toHaveBeenCalledWith("/home");
393+
});
319394
});

frontend/src/pages/LogIn/LogIn.test.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ describe("Log In Components", () => {
2727
signup: jest.fn(),
2828
login: jest.fn(),
2929
logout: jest.fn(),
30+
setUser: jest.fn(),
31+
loading: false,
3032
user: null,
3133
}));
3234
});
@@ -76,6 +78,8 @@ describe("Log In Events", () => {
7678
});
7779
},
7880
logout: jest.fn(),
81+
setUser: jest.fn(),
82+
loading: false,
7983
user: null,
8084
}));
8185
});

frontend/src/pages/QuestionList/QuestionList.test.tsx

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ jest.mock("../../utils/api", () => ({
1717
get: jest.fn(),
1818
},
1919
}));
20-
const mockedGet = questionClient.get as jest.MockedFunction<typeof questionClient.get>;
20+
const mockedGet = questionClient.get as jest.MockedFunction<
21+
typeof questionClient.get
22+
>;
2123
const questions = [
2224
{
2325
id: "1",
@@ -32,7 +34,7 @@ const questions = [
3234
description: "Description of Question 2",
3335
complexity: "Medium",
3436
categories: ["Algorithms", "Data Structures"],
35-
}
37+
},
3638
];
3739
mockedGet.mockImplementation((url: string) => {
3840
switch (url) {
@@ -61,6 +63,8 @@ describe("Question List Components For All Users", () => {
6163
signup: jest.fn(),
6264
login: jest.fn(),
6365
logout: jest.fn(),
66+
setUser: jest.fn(),
67+
loading: false,
6468
user: null,
6569
}));
6670
});
@@ -87,9 +91,15 @@ describe("Question List Components For All Users", () => {
8791

8892
it("Question table headers are rendered", async () => {
8993
render(<QuestionList />);
90-
expect(await screen.findByRole("columnheader", { name: "Title" })).toBeInTheDocument();
91-
expect(await screen.findByRole("columnheader", { name: "Complexity" })).toBeInTheDocument();
92-
expect(await screen.findByRole("columnheader", { name: "Categories" })).toBeInTheDocument();
94+
expect(
95+
await screen.findByRole("columnheader", { name: "Title" })
96+
).toBeInTheDocument();
97+
expect(
98+
await screen.findByRole("columnheader", { name: "Complexity" })
99+
).toBeInTheDocument();
100+
expect(
101+
await screen.findByRole("columnheader", { name: "Categories" })
102+
).toBeInTheDocument();
93103
});
94104

95105
it("Question table rows are rendered", async () => {
@@ -105,14 +115,20 @@ describe("Question List Components For All Users", () => {
105115

106116
it("Question table pagination is rendered", async () => {
107117
render(<QuestionList />);
108-
expect(await screen.findByRole("button", { name: /previous page/i })).toBeInTheDocument();
109-
expect(await screen.findByRole("button", { name: /next page/i })).toBeInTheDocument();
118+
expect(
119+
await screen.findByRole("button", { name: /previous page/i })
120+
).toBeInTheDocument();
121+
expect(
122+
await screen.findByRole("button", { name: /next page/i })
123+
).toBeInTheDocument();
110124
});
111125

112126
it("Create button is not rendered", async () => {
113127
render(<QuestionList />);
114128
await waitFor(() => {
115-
expect(screen.queryByRole("button", { name: "Create" })).not.toBeInTheDocument();
129+
expect(
130+
screen.queryByRole("button", { name: "Create" })
131+
).not.toBeInTheDocument();
116132
});
117133
});
118134

@@ -131,6 +147,8 @@ describe("Question List Components for Admin", () => {
131147
signup: jest.fn(),
132148
login: jest.fn(),
133149
logout: jest.fn(),
150+
setUser: jest.fn(),
151+
loading: false,
134152
user: {
135153
id: "1",
136154
firstName: "Admin",
@@ -161,10 +179,12 @@ describe("Question List Components for Admin", () => {
161179
editDeleteMenu.forEach((button) => {
162180
expect(button).toBeInTheDocument();
163181
});
164-
182+
165183
fireEvent.click(editDeleteMenu[0]);
166184
expect(screen.getByRole("menuitem", { name: "Edit" })).toBeInTheDocument();
167-
expect(screen.getByRole("menuitem", { name: "Delete" })).toBeInTheDocument();
185+
expect(
186+
screen.getByRole("menuitem", { name: "Delete" })
187+
).toBeInTheDocument();
168188
});
169189

170190
it("Edit button redirects to edit question page", async () => {

frontend/src/pages/SignUp/SignUp.test.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ describe("Sign Up Components", () => {
2727
signup: jest.fn(),
2828
login: jest.fn(),
2929
logout: jest.fn(),
30+
setUser: jest.fn(),
31+
loading: false,
3032
user: null,
3133
}));
3234
});
@@ -97,6 +99,8 @@ describe("Sign Up Events", () => {
9799
},
98100
login: jest.fn(),
99101
logout: jest.fn(),
102+
setUser: jest.fn(),
103+
loading: false,
100104
user: null,
101105
}));
102106
});

0 commit comments

Comments
 (0)