refactor: tests adriaan
This commit is contained in:
		
							parent
							
								
									ded1a5908e
								
							
						
					
					
						commit
						2d841e7955
					
				
					 7 changed files with 99 additions and 178 deletions
				
			
		|  | @ -2,6 +2,9 @@ import { setupTestApp } from '../setup-tests.js'; | |||
| import { describe, it, expect, beforeAll, beforeEach, vi, Mock } from 'vitest'; | ||||
| import { createClassHandler, deleteClassHandler, getAllClassesHandler, getClassHandler, getClassStudentsHandler, getTeacherInvitationsHandler } from '../../src/controllers/classes.js'; | ||||
| import { Request, Response } from 'express'; | ||||
| import {NotFoundException} from "../../src/exceptions/not-found-exception"; | ||||
| import {BadRequestException} from "../../src/exceptions/bad-request-exception"; | ||||
| import {getClass01} from "../test_assets/classes/classes.testdata"; | ||||
| describe('Class controllers', () => { | ||||
|     let req: Partial<Request>; | ||||
|     let res: Partial<Response>; | ||||
|  | @ -44,107 +47,61 @@ describe('Class controllers', () => { | |||
|         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ class: expect.anything() })); | ||||
|     }); | ||||
| 
 | ||||
|     it('should return 404 and error if class is not found', async () => { | ||||
|     it('Error class not found', async () => { | ||||
|         req = { | ||||
|             params: { id: 'doesnotexist'}, | ||||
|         } | ||||
| 
 | ||||
|         await getClassHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         expect(statusMock).toHaveBeenCalledWith(404); | ||||
|         expect(jsonMock).toHaveBeenCalledWith({ error: 'Class not found' }); | ||||
|         await expect(async () => getClassHandler(req as Request, res as Response)).rejects.toThrow(NotFoundException); | ||||
|     }); | ||||
| 
 | ||||
|     it('should return 200 if class is not found', async () => { | ||||
|         req = { | ||||
|             params: { id: 'id01'}, | ||||
|         } | ||||
| 
 | ||||
|         await getClassHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         // status can either not be called or called with code 200
 | ||||
|         expect( | ||||
|             statusMock.mock.calls.length === 0 || statusMock.mock.calls.some(([arg]) => arg === 200) | ||||
|         ).toBe(true); | ||||
|     }); | ||||
| 
 | ||||
|     it('should return 201 for creating a new class', async () => { | ||||
|         req = { | ||||
|             body: { displayName: 'coolenieuweklas' }, | ||||
|         }; | ||||
| 
 | ||||
|         await createClassHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         expect(statusMock).toHaveBeenCalledWith(201); | ||||
|         // TODO: return json should be a classDTO and not named (fixed in #130)
 | ||||
|         //expect(jsonMock).toHaveBeenCalledWith();
 | ||||
| 
 | ||||
|         // TODO: check if class is actually added to db
 | ||||
|     }); | ||||
|     it.todo('return json should be a classDTO and not named (fixed in #130)') | ||||
|     it.todo('check if class is actually added to db'); | ||||
| 
 | ||||
|     it('should return 400 for trying to create a class without name', async () => { | ||||
|     it('Error create a class without name', async () => { | ||||
|         req = { | ||||
|             body: {}, | ||||
|         }; | ||||
| 
 | ||||
|         await createClassHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         expect(statusMock).toHaveBeenCalledWith(400); | ||||
|         expect(jsonMock).toHaveBeenCalledWith({ error: 'Missing one or more required fields: displayName' }); | ||||
|         await expect(async () => createClassHandler(req as Request, res as Response)).rejects.toThrow(BadRequestException); | ||||
|     }); | ||||
| 
 | ||||
|     it('should return a list of students when calling getClassStudentsHandler', async () => { | ||||
|     it('return list of students', async () => { | ||||
|         req = { | ||||
|             params: { id: 'id01' }, | ||||
|             params: { id: getClass01().classId as string }, | ||||
|             query: {}, | ||||
|         }; | ||||
| 
 | ||||
|         await getClassStudentsHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         checkReturnList(jsonMock, 'students'); | ||||
|         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ students: expect.anything() })); | ||||
|     }); | ||||
| 
 | ||||
|     it('should return 404 not found when calling getClassStudentsHandler on a non-existent class', async () => { | ||||
|     it('Error students on a non-existent class', async () => { | ||||
|         req = { | ||||
|             params: { id: 'doesnotexist' }, | ||||
|             query: {}, | ||||
|         }; | ||||
| 
 | ||||
|         await getClassStudentsHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         expect(statusMock).toHaveBeenCalledWith(404); | ||||
|         expect(jsonMock).toHaveBeenCalledWith({ error: 'Class not found' }); | ||||
|         await expect(async () => getClassStudentsHandler(req as Request, res as Response)).rejects.toThrow(NotFoundException); | ||||
|     }); | ||||
| 
 | ||||
|     it('should return 200 and a list of teacher-invitations', async () => { | ||||
|         const classId = getClass01().classId as string; | ||||
|         req = { | ||||
|             params: { id: 'id01' }, | ||||
|             params: { id: classId }, | ||||
|             query: {}, | ||||
|         }; | ||||
| 
 | ||||
|         await getTeacherInvitationsHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         expect(jsonMock).toHaveBeenCalledWith({"invitations": [ | ||||
|                 { | ||||
|                     "class": "id01", | ||||
|                     "receiver": "LimpBizkit", | ||||
|                     "sender": "FooFighters", | ||||
|                 } | ||||
|             ]}); | ||||
|         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ invitations: expect.anything() })); | ||||
|     }); | ||||
| 
 | ||||
|     it('should return 404 not found when calling teacher-invitations on a non-existent class', async () => { | ||||
|     it('Error teacher-invitations on a non-existent class', async () => { | ||||
|         req = { | ||||
|             params: { id: 'doesnotexist' }, | ||||
|             query: {}, | ||||
|         }; | ||||
| 
 | ||||
|         await getTeacherInvitationsHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         expect(jsonMock).toHaveBeenCalledWith({ error: 'Class not found' }); | ||||
|         expect(statusMock).toHaveBeenCalledWith(404); | ||||
|         await expect(async () => getTeacherInvitationsHandler(req as Request, res as Response)).rejects.toThrow(NotFoundException); | ||||
|     }); | ||||
| 
 | ||||
|     it('should return a list of classes', async () => { | ||||
|  | @ -154,11 +111,7 @@ describe('Class controllers', () => { | |||
| 
 | ||||
|         await getAllClassesHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         expect(jsonMock).toHaveBeenCalled(); | ||||
| 
 | ||||
|         const result = jsonMock.mock.lastCall![0]; | ||||
| 
 | ||||
|         expect("classes" in result).toBeTruthy(); | ||||
|         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ classes: expect.anything() })); | ||||
|     }) | ||||
| 
 | ||||
| }); | ||||
|  |  | |||
		Reference in a new issue
	
	 Gabriellvl
						Gabriellvl