From ea9f930ce544068537e682ee65f78c39b8e7356d Mon Sep 17 00:00:00 2001 From: Gonzalo Alonso Fernandez Date: Sun, 10 Mar 2024 10:38:07 +0100 Subject: [PATCH] feat: creating the questions.js tests. --- webapp/src/tests/Questions.test.js | 52 ++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 webapp/src/tests/Questions.test.js diff --git a/webapp/src/tests/Questions.test.js b/webapp/src/tests/Questions.test.js new file mode 100644 index 00000000..7fe6e37a --- /dev/null +++ b/webapp/src/tests/Questions.test.js @@ -0,0 +1,52 @@ +import MockAdapter from "axios-mock-adapter"; +import { getQuestion, answerQuestion } from "components/game/Questions"; +import axios, { HttpStatusCode } from "axios"; + +const mockAxios = new MockAdapter(axios); + +describe("Question Service tests", () => { + describe("getQuestion function", () => { + beforeEach(() => { + mockAxios.reset(); + }); + + it("successfully retrieves a question", async () => { + // Mock axios + const mockQuestion = { + questionId: 123, + text: "What is the meaning of life?", + }; + + mockAxios.onGet(process.env.REACT_APP_API_ENDPOINT + "/questions/new").replyOnce( + HttpStatusCode.Ok, + mockQuestion + ); + + const result = await getQuestion(); + + expect(result).toEqual(mockQuestion); + }); + }); + + describe("answerQuestion function", () => { + beforeEach(() => { + mockAxios.reset(); + }); + + it("successfully answers a question", async () => { + const mockResponse = { + success: true, + message: "Answer submitted successfully.", + }; + + mockAxios.onPost(process.env.REACT_APP_API_ENDPOINT + "/questions/123/answer").replyOnce( + HttpStatusCode.Ok, + mockResponse + ); + + const result = await answerQuestion(123, "a1"); + + expect(result).toEqual(mockResponse); + }); + }); +});