fix: backend controller tests
This commit is contained in:
		
							parent
							
								
									bf6f10c5cb
								
							
						
					
					
						commit
						f97aab21fc
					
				
					 5 changed files with 136 additions and 65 deletions
				
			
		|  | @ -17,6 +17,10 @@ import { EntityAlreadyExistsException } from '../../src/exceptions/entity-alread | |||
| import { getStudentRequestsHandler } from '../../src/controllers/students.js'; | ||||
| import { TeacherDTO } from '@dwengo-1/common/interfaces/teacher'; | ||||
| import { getClassHandler } from '../../src/controllers/classes'; | ||||
| import { getFooFighters, getTestleerkracht1 } from '../test_assets/users/teachers.testdata.js'; | ||||
| import { getClass02 } from '../test_assets/classes/classes.testdata.js'; | ||||
| import { getPinkFloyd, TEST_STUDENTS } from '../test_assets/users/students.testdata.js'; | ||||
| import { getClassJoinRequest01, getClassJoinRequest02 } from '../test_assets/classes/class-join-requests.testdata.js'; | ||||
| 
 | ||||
| describe('Teacher controllers', () => { | ||||
|     let req: Partial<Request>; | ||||
|  | @ -36,7 +40,8 @@ describe('Teacher controllers', () => { | |||
|     }); | ||||
| 
 | ||||
|     it('Get teacher', async () => { | ||||
|         req = { params: { username: 'FooFighters' } }; | ||||
|         const teacher = getFooFighters(); | ||||
|         req = { params: { username: teacher.username } }; | ||||
| 
 | ||||
|         await getTeacherHandler(req as Request, res as Response); | ||||
| 
 | ||||
|  | @ -77,12 +82,13 @@ describe('Teacher controllers', () => { | |||
|         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ teacher: expect.objectContaining(teacher) })); | ||||
|     }); | ||||
| 
 | ||||
|     it('Create duplicate student', async () => { | ||||
|     it('Create duplicate teacher', async () => { | ||||
|         const teacher = getFooFighters(); | ||||
|         req = { | ||||
|             body: { | ||||
|                 username: 'FooFighters', | ||||
|                 firstName: 'Dave', | ||||
|                 lastName: 'Grohl', | ||||
|                 username: teacher.username, | ||||
|                 firstName: teacher.firstName, | ||||
|                 lastName: teacher.lastName, | ||||
|             }, | ||||
|         }; | ||||
| 
 | ||||
|  | @ -105,20 +111,23 @@ describe('Teacher controllers', () => { | |||
|         const result = jsonMock.mock.lastCall?.[0]; | ||||
| 
 | ||||
|         const teacherUsernames = result.teachers.map((s: TeacherDTO) => s.username); | ||||
|         expect(teacherUsernames).toContain('testleerkracht1'); | ||||
| 
 | ||||
|         const teacher = getTestleerkracht1(); | ||||
|         expect(teacherUsernames).toContain(teacher.username); | ||||
| 
 | ||||
|         expect(result.teachers).toHaveLength(5); | ||||
|     }); | ||||
| 
 | ||||
|     it('Deleting non-existent student', async () => { | ||||
|     it('Deleting non-existent teacher', async () => { | ||||
|         req = { params: { username: 'doesnotexist' } }; | ||||
| 
 | ||||
|         await expect(async () => deleteTeacherHandler(req as Request, res as Response)).rejects.toThrow(NotFoundException); | ||||
|     }); | ||||
| 
 | ||||
|     it('Get teacher classes', async () => { | ||||
|         const class_ = getClass02(); | ||||
|         req = { | ||||
|             params: { username: 'testleerkracht1' }, | ||||
|             params: { username: class_.teachers[0].username }, | ||||
|             query: { full: 'true' }, | ||||
|         }; | ||||
| 
 | ||||
|  | @ -131,9 +140,10 @@ describe('Teacher controllers', () => { | |||
|         expect(result.classes.length).toBeGreaterThan(0); | ||||
|     }); | ||||
| 
 | ||||
|     it('Get teacher students', async () => { | ||||
|     it('Get teacher teachers', async () => { | ||||
|         const teacher = getTestleerkracht1(); | ||||
|         req = { | ||||
|             params: { username: 'testleerkracht1' }, | ||||
|             params: { username: teacher.username }, | ||||
|             query: { full: 'true' }, | ||||
|         }; | ||||
| 
 | ||||
|  | @ -168,8 +178,9 @@ describe('Teacher controllers', () => { | |||
|      */ | ||||
| 
 | ||||
|     it('Get join requests by class', async () => { | ||||
|         const jr = getClassJoinRequest01(); | ||||
|         req = { | ||||
|             params: { classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' }, | ||||
|             params: { classId: jr.class.classId! }, | ||||
|         }; | ||||
| 
 | ||||
|         await getStudentJoinRequestHandler(req as Request, res as Response); | ||||
|  | @ -182,8 +193,9 @@ describe('Teacher controllers', () => { | |||
|     }); | ||||
| 
 | ||||
|     it('Update join request status', async () => { | ||||
|         const jr = getClassJoinRequest01(); | ||||
|         req = { | ||||
|             params: { classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89', studentUsername: 'PinkFloyd' }, | ||||
|             params: { classId: jr.class.classId!, studentUsername: jr.requester.username }, | ||||
|             body: { accepted: 'true' }, | ||||
|         }; | ||||
| 
 | ||||
|  | @ -192,7 +204,7 @@ describe('Teacher controllers', () => { | |||
|         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ request: expect.anything() })); | ||||
| 
 | ||||
|         req = { | ||||
|             params: { username: 'PinkFloyd' }, | ||||
|             params: { username: jr.requester.username }, | ||||
|         }; | ||||
| 
 | ||||
|         await getStudentRequestsHandler(req as Request, res as Response); | ||||
|  | @ -201,11 +213,11 @@ describe('Teacher controllers', () => { | |||
|         expect(status).toBeTruthy(); | ||||
| 
 | ||||
|         req = { | ||||
|             params: { id: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' }, | ||||
|             params: { id: jr.class.classId! }, | ||||
|         }; | ||||
| 
 | ||||
|         await getClassHandler(req as Request, res as Response); | ||||
|         const students: string[] = jsonMock.mock.lastCall?.[0].class.students; | ||||
|         expect(students).contains('PinkFloyd'); | ||||
|         expect(students).contains(jr.requester.username); | ||||
|     }); | ||||
| }); | ||||
|  |  | |||
		Reference in a new issue
	
	 laurejablonski
						laurejablonski