Skip to content

Commit

Permalink
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions webapp/src/tests/Questions.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
});

0 comments on commit ea9f930

Please sign in to comment.