From da1d502ee628595c4fdd0cc836de884da8d9a0ab Mon Sep 17 00:00:00 2001 From: Gabriellvl Date: Mon, 7 Apr 2025 11:30:15 +0200 Subject: [PATCH] feat: question tests --- backend/src/services/questions.ts | 9 +- backend/tests/controllers/questions.test.ts | 136 ++++++++++++++++++++ 2 files changed, 141 insertions(+), 4 deletions(-) create mode 100644 backend/tests/controllers/questions.test.ts diff --git a/backend/src/services/questions.ts b/backend/src/services/questions.ts index 2e26c960..a3c64b9f 100644 --- a/backend/src/services/questions.ts +++ b/backend/src/services/questions.ts @@ -54,10 +54,11 @@ export async function deleteQuestion(questionId: QuestionId): Promise { + let req: Partial; + let res: Partial; + + let jsonMock: Mock; + + beforeAll(async () => { + await setupTestApp(); + }); + + beforeEach(() => { + jsonMock = vi.fn(); + res = { + json: jsonMock, + }; + }); + + it('Get question list', async () => { + req = { + params: { hruid: 'id05', version: '1' }, + query: { lang: Language.English, full: 'true' }, + }; + + await getAllQuestionsHandler(req as Request, res as Response); + expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ questions: expect.anything() })); + + const result = jsonMock.mock.lastCall?.[0]; + // console.log(result.questions); + expect(result.questions).to.have.length.greaterThan(1); + }); + + it('Get question', async () => { + req = { + params: { hruid: 'id05', version: '1', seq: '1'}, + 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 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 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 () => { + req = { + params: { hruid: 'id_not_exist'}, + query: { lang: Language.English, full: 'true' }, + }; + + await expect( async () => getQuestionHandler(req as Request, res as Response)) + .rejects.toThrow(NotFoundException); + }) + + it('Get question no hruid given', async () => { + req = { + params: {}, + query: { lang: Language.English, full: 'true' }, + }; + + await expect( async () => getQuestionHandler(req as Request, res as Response)) + .rejects.toThrow(BadRequestException); + }) + + /* + it('Create and delete question', async() => { + req = { + params: { hruid: 'id05', version: '1', seq: '2'}, + query: { lang: Language.English }, + }; + + await deleteQuestionHandler(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('Update question', async() => { + const newContent = "updated question"; + req = { + params: { hruid: 'id05', version: '1', seq: '1'}, + query: { lang: Language.English }, + body: { content: newContent } + }; + + await updateQuestionHandler(req as Request, res as Response); + + expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ question: expect.anything() })); + + const result = jsonMock.mock.lastCall?.[0]; + // console.log(result.question); + expect(result.question.content).to.eq(newContent); + }); +});