feat: testen voor assignment controller geimplementeerd
This commit is contained in:
		
							parent
							
								
									58bfa90474
								
							
						
					
					
						commit
						dbe7807f4a
					
				
					 3 changed files with 97 additions and 19 deletions
				
			
		
							
								
								
									
										96
									
								
								backend/tests/controllers/assignments.test.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										96
									
								
								backend/tests/controllers/assignments.test.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,96 @@ | |||
| import { setupTestApp } from '../setup-tests.js'; | ||||
| import { describe, it, expect, beforeAll, beforeEach, vi, Mock } from 'vitest'; | ||||
| import { Request, Response } from 'express'; | ||||
| import { getAssignmentHandler, getAllAssignmentsHandler, getAssignmentsSubmissionsHandler } from '../../src/controllers/assignments.js'; | ||||
| 
 | ||||
| function createRequestObject(classid: string, assignmentid: string) { | ||||
|     return { | ||||
|         params: {  | ||||
|             classid: classid, | ||||
|             id: assignmentid, | ||||
|         }, | ||||
|         query: {}, | ||||
|     }; | ||||
| } | ||||
| 
 | ||||
| function checkReturnList(jsonMock: Mock, listName: string) { | ||||
| 	expect(jsonMock).toHaveBeenCalled(); | ||||
| 
 | ||||
| 	const result = jsonMock.mock.lastCall![0]; | ||||
| 		 | ||||
| 	expect(listName in result).toBeTruthy(); | ||||
| } | ||||
| 
 | ||||
| function checkReturn404(jsonMock: Mock, statusMock: Mock) { | ||||
| 		expect(statusMock).toHaveBeenCalledWith(404); | ||||
| 		expect(jsonMock).toHaveBeenCalled(); | ||||
| } | ||||
| 
 | ||||
| describe('Assignment controllers', () => { | ||||
|     let req: Partial<Request>; | ||||
|     let res: Partial<Response>; | ||||
| 
 | ||||
|     let jsonMock: Mock; | ||||
|     let statusMock: Mock; | ||||
| 
 | ||||
|     beforeAll(async () => { | ||||
|         await setupTestApp(); | ||||
|     }); | ||||
|    | ||||
|     beforeEach(async () =>  { | ||||
|         jsonMock = vi.fn(); | ||||
|         statusMock = vi.fn().mockReturnThis(); | ||||
| 
 | ||||
|         res = { | ||||
|             json: jsonMock, | ||||
|             status: statusMock, | ||||
|         }; | ||||
|     }); | ||||
| 
 | ||||
|     it('should return a 404 when trying to find a non-existent assignment', async () => { | ||||
|         req = createRequestObject('id01', '43000'); // should not exist
 | ||||
| 
 | ||||
| 		await getAssignmentHandler(req as Request, res as Response); | ||||
| 
 | ||||
| 		checkReturn404(jsonMock, statusMock); | ||||
|     }); | ||||
| 
 | ||||
| 	it('should return a 404 when trying to find an assignment on a non-existing class', async () => { | ||||
|         req = createRequestObject('doesnotexist', '1'); // should not exist
 | ||||
| 
 | ||||
| 		await getAssignmentHandler(req as Request, res as Response); | ||||
| 
 | ||||
| 		checkReturn404(jsonMock, statusMock); | ||||
| 	}); | ||||
| 	 | ||||
| 	it('should return an assignment', async () => { | ||||
| 		req = createRequestObject('id01', '1'); | ||||
| 
 | ||||
| 		await getAssignmentHandler(req as Request, res as Response); | ||||
| 
 | ||||
| 		expect(jsonMock).toHaveBeenCalledWith({ | ||||
| 			id: 1, | ||||
| 			class: 'id01', | ||||
| 			title: 'dire straits', | ||||
| 			description: 'reading', | ||||
| 			learningPath: 'id02', | ||||
| 			language: 'en' | ||||
| 		}); | ||||
| 	}); | ||||
| 
 | ||||
| 	it('should return a list of assignments', async () => { | ||||
| 		req = createRequestObject('id01', 'irrelevant'); | ||||
| 
 | ||||
| 		await getAllAssignmentsHandler(req as Request, res as Response); | ||||
| 
 | ||||
| 		checkReturnList(jsonMock, "assignments"); | ||||
| 	}); | ||||
| 
 | ||||
| 	it('should return a list of submissions for an assignment', async () => { | ||||
| 		req = createRequestObject('id01', '1'); | ||||
| 
 | ||||
| 		await getAssignmentsSubmissionsHandler(req as Request, res as Response); | ||||
| 
 | ||||
| 		checkReturnList(jsonMock, "submissions"); | ||||
| 	}) | ||||
| }) | ||||
|  | @ -4,11 +4,6 @@ import { createClassHandler, getAllClassesHandler, getClassHandler, getClassStud | |||
| import { Request, Response } from 'express'; | ||||
| import { getAllClasses } from '../../src/services/class.js'; | ||||
| 
 | ||||
| async function fetchClass(id: string) { | ||||
|     const data = await fetch(`localhost:3000/class/${id}`); | ||||
|     return data; | ||||
| } | ||||
| 
 | ||||
| describe('Class controllers', () => { | ||||
|     let req: Partial<Request>; | ||||
|     let res: Partial<Response>; | ||||
|  |  | |||
|  | @ -2,19 +2,6 @@ import { setupTestApp } from '../setup-tests.js'; | |||
| import { describe, it, expect, beforeAll, beforeEach, vi, Mock } from 'vitest'; | ||||
| import { Request, Response } from 'express'; | ||||
| import { createGroupHandler, getAllGroupsHandler, getGroupHandler, getGroupSubmissionsHandler } from '../../src/controllers/groups.js'; | ||||
| import { G } from 'vitest/dist/chunks/reporters.66aFHiyX.js'; | ||||
| 
 | ||||
| async function test404<T>( | ||||
|     req: Partial<Request<T>> ,  | ||||
|     res: Partial<Response>,  | ||||
|     handler: (req: Request<T>, res: Response) => Promise<void>,  | ||||
|     expectedMessage: string | ||||
| ) { | ||||
|     await handler(req as Request<T>, res as Response); | ||||
| 
 | ||||
|     expect(res.status).toHaveBeenCalledWith(404); | ||||
|     expect(res.json).toHaveBeenCalledWith({ error: expectedMessage }); | ||||
| } | ||||
| 
 | ||||
| function createRequestObject(classid: string, assignmentid: string, groupNumber: string) { | ||||
|     return { | ||||
|  |  | |||
		Reference in a new issue
	
	 Adriaan Jacquet
						Adriaan Jacquet