1
1
import { fireEvent , render , screen } from "@testing-library/react" ;
2
2
import axios from "axios" ;
3
3
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" ;
5
6
import Navbar from "." ;
6
7
import { MemoryRouter } from "react-router-dom" ;
7
8
@@ -16,6 +17,24 @@ jest.mock("react-router-dom", () => ({
16
17
useNavigate : ( ) => mockUseNavigate ( ) ,
17
18
} ) ) ;
18
19
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
+
19
38
describe ( "Navigation routes" , ( ) => {
20
39
it ( "Question route is present" , ( ) => {
21
40
const username = faker . internet . userName ( ) ;
@@ -41,7 +60,7 @@ describe("Navigation routes", () => {
41
60
} ,
42
61
} ,
43
62
} ) ;
44
- jest . spyOn ( hooks , "useAuth" ) . mockImplementation ( ( ) => ( {
63
+ jest . spyOn ( authHooks , "useAuth" ) . mockImplementation ( ( ) => ( {
45
64
signup : jest . fn ( ) ,
46
65
login : jest . fn ( ) ,
47
66
logout : jest . fn ( ) ,
@@ -71,7 +90,7 @@ describe("Navigation routes", () => {
71
90
describe ( "Unauthenticated user" , ( ) => {
72
91
it ( "Sign up button is rendered" , ( ) => {
73
92
mockedAxios . get . mockResolvedValue ( { data : { data : null } } ) ;
74
- jest . spyOn ( hooks , "useAuth" ) . mockImplementation ( ( ) => ( {
93
+ jest . spyOn ( authHooks , "useAuth" ) . mockImplementation ( ( ) => ( {
75
94
signup : jest . fn ( ) ,
76
95
login : jest . fn ( ) ,
77
96
logout : jest . fn ( ) ,
@@ -89,7 +108,7 @@ describe("Unauthenticated user", () => {
89
108
90
109
it ( "Login button is rendered" , ( ) => {
91
110
mockedAxios . get . mockResolvedValue ( { data : { data : null } } ) ;
92
- jest . spyOn ( hooks , "useAuth" ) . mockImplementation ( ( ) => ( {
111
+ jest . spyOn ( authHooks , "useAuth" ) . mockImplementation ( ( ) => ( {
93
112
signup : jest . fn ( ) ,
94
113
login : jest . fn ( ) ,
95
114
logout : jest . fn ( ) ,
@@ -131,7 +150,7 @@ describe("Authenticated user", () => {
131
150
} ,
132
151
} ,
133
152
} ) ;
134
- jest . spyOn ( hooks , "useAuth" ) . mockImplementation ( ( ) => ( {
153
+ jest . spyOn ( authHooks , "useAuth" ) . mockImplementation ( ( ) => ( {
135
154
signup : jest . fn ( ) ,
136
155
login : jest . fn ( ) ,
137
156
logout : jest . fn ( ) ,
@@ -181,7 +200,7 @@ describe("Authenticated user", () => {
181
200
} ,
182
201
} ,
183
202
} ) ;
184
- jest . spyOn ( hooks , "useAuth" ) . mockImplementation ( ( ) => ( {
203
+ jest . spyOn ( authHooks , "useAuth" ) . mockImplementation ( ( ) => ( {
185
204
signup : jest . fn ( ) ,
186
205
login : jest . fn ( ) ,
187
206
logout : jest . fn ( ) ,
@@ -235,7 +254,7 @@ describe("Authenticated user", () => {
235
254
} ,
236
255
} ,
237
256
} ) ;
238
- jest . spyOn ( hooks , "useAuth" ) . mockImplementation ( ( ) => ( {
257
+ jest . spyOn ( authHooks , "useAuth" ) . mockImplementation ( ( ) => ( {
239
258
signup : jest . fn ( ) ,
240
259
login : jest . fn ( ) ,
241
260
logout : jest . fn ( ) ,
@@ -287,7 +306,7 @@ describe("Authenticated user", () => {
287
306
} ,
288
307
} ,
289
308
} ) ;
290
- jest . spyOn ( hooks , "useAuth" ) . mockImplementation ( ( ) => ( {
309
+ jest . spyOn ( authHooks , "useAuth" ) . mockImplementation ( ( ) => ( {
291
310
signup : jest . fn ( ) ,
292
311
login : jest . fn ( ) ,
293
312
logout : jest . fn ( ) ,
@@ -316,4 +335,60 @@ describe("Authenticated user", () => {
316
335
screen . getByRole ( "menuitem" , { name : "Logout" } )
317
336
) . toBeInTheDocument ( ) ;
318
337
} ) ;
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
+ } ) ;
319
394
} ) ;
0 commit comments