4
4
5
5
6
6
import marketplaceController from '../server/controllers/marketplaceController' ;
7
+ import sessionController from '../server/controllers/sessionController' ;
7
8
import app from '../server/server' ;
8
9
import mockData from '../mockData' ;
9
10
import { profileEnd } from 'console' ;
10
- import { Projects } from '../server/models/reactypeModels' ;
11
+ import { Projects , Sessions } from '../server/models/reactypeModels' ;
11
12
const request = require ( 'supertest' ) ;
12
13
const mongoose = require ( 'mongoose' ) ;
13
14
const mockNext = jest . fn ( ) ; // Mock nextFunction
@@ -288,8 +289,89 @@ describe('Server endpoint tests', () => {
288
289
} ) ;
289
290
} ) ;
290
291
292
+ xdescribe ( 'SessionController tests' , ( ) => {
293
+
291
294
295
+
296
+
297
+ afterEach ( ( ) => {
298
+ jest . resetAllMocks ( ) ;
299
+ } )
300
+
301
+ xdescribe ( 'isLoggedIn' , ( ) => {
302
+ // Mock Express request and response objects and next function
303
+ const mockReq : any = {
304
+ cookies : null , //trying to trigger if cookies was not assigned
305
+ body : {
306
+ userId : 'sampleUserId' , // Set up a sample userId in the request body
307
+ } ,
308
+ }
309
+
310
+ const mockRes : any = {
311
+ json : jest . fn ( ) ,
312
+ status : jest . fn ( ) ,
313
+ redirect : jest . fn ( )
314
+ } ;
315
+
316
+ const next = jest . fn ( ) ;
317
+ it ( 'Assign userId from request body to cookieId' , async ( ) => {
318
+ // Call isLoggedIn
319
+ await sessionController . isLoggedIn ( mockReq , mockRes , next ) ;
320
+ expect ( mockRes . redirect ) . toHaveBeenCalledWith ( '/' ) ;
321
+ // Ensure that next() was called
322
+ } ) ;
323
+
324
+ it ( 'Trigger a database query error for findOne' , async ( ) => {
325
+ jest . spyOn ( mongoose . model ( 'Sessions' ) , 'findOne' ) . mockImplementation ( ( ) => {
326
+ throw new Error ( 'Database query error' ) ;
327
+ } ) ;
328
+ // Call isLoggedIn
329
+ await sessionController . isLoggedIn ( mockReq , mockRes , next ) ;
330
+
331
+ // Ensure that next() was called with the error
332
+ expect ( next ) . toHaveBeenCalledWith ( expect . objectContaining ( {
333
+ log : expect . stringMatching ( 'Database query error' ) , // The 'i' flag makes it case-insensitive
334
+ } ) ) ;
335
+ } ) ;
336
+ } ) ;
292
337
338
+ xdescribe ( 'startSession' , ( ) => {
339
+ const mockReq : any = {
340
+ cookies : projectToSave . userId , //trying to trigger if cookies was not assigned
341
+ body : {
342
+ userId : 'sampleUserId' , // Set up a sample userId in the request body
343
+ } ,
344
+ }
345
+
346
+ const mockRes : any = {
347
+ json : jest . fn ( ) ,
348
+ status : jest . fn ( ) ,
349
+ redirect : jest . fn ( )
350
+ } ;
351
+
352
+ jest . spyOn ( mongoose . model ( 'Sessions' ) , 'findOne' ) . mockImplementation ( ( ) => {
353
+ throw new Error ( 'Database query error' ) ;
354
+ } ) ;
355
+
356
+ const next = jest . fn ( ) ;
357
+
358
+ it ( 'Trigger a database query error for findOne' , async ( ) => {
359
+
360
+ jest . spyOn ( mongoose . model ( 'Sessions' ) , 'findOne' ) . mockImplementation ( ( ) => {
361
+ throw new Error ( 'Database query error' ) ;
362
+ } ) ;
363
+ // Call startSession
364
+ await sessionController . startSession ( mockReq , mockRes , next ) ;
365
+
366
+ // Ensure that next() was called with the error
367
+ expect ( next ) . toHaveBeenCalledWith ( expect . objectContaining ( {
368
+ log : expect . stringMatching ( 'Database query error' ) , // The 'i' flag makes it case-insensitive
369
+ } ) ) ;
370
+ } ) ;
371
+
372
+ } ) ;
373
+
374
+ } ) ;
293
375
294
376
} ) ;
295
377
@@ -299,6 +381,7 @@ describe('Server endpoint tests', () => {
299
381
300
382
301
383
384
+
302
385
// describe('marketplaceController Middleware', () => {
303
386
// describe('getProjects tests', () => {
304
387
// it('should add the projects as an array to res.locals', () => {
0 commit comments