feat: answer tests
This commit is contained in:
		
							parent
							
								
									da1d502ee6
								
							
						
					
					
						commit
						42526900d3
					
				
					 4 changed files with 93 additions and 16 deletions
				
			
		|  | @ -5,7 +5,7 @@ import {createAnswer, deleteAnswer, getAnswer, getAnswersByQuestion, updateAnswe | ||||||
| import {FALLBACK_SEQ_NUM} from "../config"; | import {FALLBACK_SEQ_NUM} from "../config"; | ||||||
| import {AnswerData} from "@dwengo-1/common/interfaces/answer"; | import {AnswerData} from "@dwengo-1/common/interfaces/answer"; | ||||||
| 
 | 
 | ||||||
| export async function getAnswersHandler(req: Request, res: Response): Promise<void> { | export async function getAllAnswersHandler(req: Request, res: Response): Promise<void> { | ||||||
|     const hruid = req.params.hruid; |     const hruid = req.params.hruid; | ||||||
|     const version = req.params.version; |     const version = req.params.version; | ||||||
|     const language = req.query.lang as string; |     const language = req.query.lang as string; | ||||||
|  |  | ||||||
|  | @ -3,13 +3,13 @@ import { | ||||||
|     createAnswerHandler, |     createAnswerHandler, | ||||||
|     deleteAnswerHandler, |     deleteAnswerHandler, | ||||||
|     getAnswerHandler, |     getAnswerHandler, | ||||||
|     getAnswersHandler, |     getAllAnswersHandler, | ||||||
|     updateAnswerHandler |     updateAnswerHandler | ||||||
| } from "../controllers/answers"; | } from "../controllers/answers"; | ||||||
| 
 | 
 | ||||||
| const router = express.Router({ mergeParams: true }); | const router = express.Router({ mergeParams: true }); | ||||||
| 
 | 
 | ||||||
| router.get('/', getAnswersHandler); | router.get('/', getAllAnswersHandler); | ||||||
| 
 | 
 | ||||||
| router.post('/', createAnswerHandler) | router.post('/', createAnswerHandler) | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
							
								
								
									
										90
									
								
								backend/tests/controllers/answers.test.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										90
									
								
								backend/tests/controllers/answers.test.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,90 @@ | ||||||
|  | import {Request, Response} from "express"; | ||||||
|  | import {beforeAll, beforeEach, describe, expect, it, Mock, vi} from "vitest"; | ||||||
|  | import {setupTestApp} from "../setup-tests"; | ||||||
|  | import {Language} from "@dwengo-1/common/util/language"; | ||||||
|  | import {getAllAnswersHandler, getAnswerHandler, updateAnswerHandler} from "../../src/controllers/answers"; | ||||||
|  | import {BadRequestException} from "../../src/exceptions/bad-request-exception"; | ||||||
|  | import {NotFoundException} from "../../src/exceptions/not-found-exception"; | ||||||
|  | 
 | ||||||
|  | describe('Questions controllers', () => { | ||||||
|  |     let req: Partial<Request>; | ||||||
|  |     let res: Partial<Response>; | ||||||
|  | 
 | ||||||
|  |     let jsonMock: Mock; | ||||||
|  | 
 | ||||||
|  |     beforeAll(async () => { | ||||||
|  |         await setupTestApp(); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     beforeEach(() => { | ||||||
|  |         jsonMock = vi.fn(); | ||||||
|  |         res = { | ||||||
|  |             json: jsonMock, | ||||||
|  |         }; | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     it('Get answers list', async () => { | ||||||
|  |         req = { | ||||||
|  |             params: {hruid: 'id05', version: '1', seq: '2'}, | ||||||
|  |             query: {lang: Language.English, full: 'true'}, | ||||||
|  |         }; | ||||||
|  | 
 | ||||||
|  |         await getAllAnswersHandler(req as Request, res as Response); | ||||||
|  |         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({answers: expect.anything()})); | ||||||
|  | 
 | ||||||
|  |         const result = jsonMock.mock.lastCall?.[0]; | ||||||
|  |         // console.log(result.answers);
 | ||||||
|  |         expect(result.questions).to.have.length.greaterThan(1); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     it('Get answer', async () => { | ||||||
|  |         req = { | ||||||
|  |             params: {hruid: 'id05', version: '1', seq: '2', seqAnswer: '2'}, | ||||||
|  |             query: {lang: Language.English, full: 'true'}, | ||||||
|  |         }; | ||||||
|  | 
 | ||||||
|  |         await getAnswerHandler(req as Request, res as Response); | ||||||
|  |         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({answer: expect.anything()})); | ||||||
|  | 
 | ||||||
|  |         // const result = jsonMock.mock.lastCall?.[0];
 | ||||||
|  |         // console.log(result.answer);
 | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     it('Get answer hruid does not exist', async () => { | ||||||
|  |         req = { | ||||||
|  |             params: { hruid: 'id_not_exist'}, | ||||||
|  |             query: { lang: Language.English, full: 'true' }, | ||||||
|  |         }; | ||||||
|  | 
 | ||||||
|  |         await expect( async () => getAnswerHandler(req as Request, res as Response)) | ||||||
|  |             .rejects.toThrow(NotFoundException); | ||||||
|  |     }) | ||||||
|  | 
 | ||||||
|  |     it('Get answer no hruid given', async () => { | ||||||
|  |         req = { | ||||||
|  |             params: {}, | ||||||
|  |             query: { lang: Language.English, full: 'true' }, | ||||||
|  |         }; | ||||||
|  | 
 | ||||||
|  |         await expect( async () => getAnswerHandler(req as Request, res as Response)) | ||||||
|  |             .rejects.toThrow(BadRequestException); | ||||||
|  |     }) | ||||||
|  | 
 | ||||||
|  |     it('Update question', async() => { | ||||||
|  |         const newContent = "updated question"; | ||||||
|  |         req = { | ||||||
|  |             params: { hruid: 'id05', version: '1', seq: '2', seqAnswer: '2'}, | ||||||
|  |             query: { lang: Language.English }, | ||||||
|  |             body: { content: newContent } | ||||||
|  |         }; | ||||||
|  | 
 | ||||||
|  |         await updateAnswerHandler(req as Request, res as Response); | ||||||
|  | 
 | ||||||
|  |         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ answer: expect.anything() })); | ||||||
|  | 
 | ||||||
|  |         const result = jsonMock.mock.lastCall?.[0]; | ||||||
|  |         // console.log(result.question);
 | ||||||
|  |         expect(result.answer.content).to.eq(newContent); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  | }); | ||||||
|  | @ -67,19 +67,6 @@ describe('Questions controllers', () => { | ||||||
|         // console.log(result.question);
 |         // console.log(result.question);
 | ||||||
|     }) |     }) | ||||||
| 
 | 
 | ||||||
|     it('Get question with fallback sequence number and version', async () => { |  | ||||||
|         req = { |  | ||||||
|             params: { hruid: 'id05'}, |  | ||||||
|             query: { lang: Language.English, full: 'true' }, |  | ||||||
|         }; |  | ||||||
| 
 |  | ||||||
|         await getQuestionHandler(req as Request, res as Response); |  | ||||||
|         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ question: expect.anything() })); |  | ||||||
| 
 |  | ||||||
|         // const result = jsonMock.mock.lastCall?.[0];
 |  | ||||||
|         // console.log(result.question);
 |  | ||||||
|     }) |  | ||||||
| 
 |  | ||||||
|     it('Get question hruid does not exist', async () => { |     it('Get question hruid does not exist', async () => { | ||||||
|         req = { |         req = { | ||||||
|             params: { hruid: 'id_not_exist'}, |             params: { hruid: 'id_not_exist'}, | ||||||
|  |  | ||||||
		Reference in a new issue
	
	 Gabriellvl
						Gabriellvl