Skip to content

Commit

Permalink
test(problems): Add comprehensive tests for problem controller functions
Browse files Browse the repository at this point in the history
- Added tests for `getIndex` to ensure the problem generator index is returned successfully.
- Mocked `problemGenerator` in the test suite for `generateProblem`.
- Tested the successful generation of a problem with the `generateProblem` function.
- Added test cases to handle generator not found (404) and internal server errors (500) for `generateProblem`.
- Ensured proper response validation for all scenarios.
  • Loading branch information
TKanX committed Sep 26, 2024
1 parent 5ad35df commit 866ae41
Showing 1 changed file with 97 additions and 0 deletions.
97 changes: 97 additions & 0 deletions tests/controllers/problemController.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/**
* @file tests/controllers/problemController.test.js
* @description Tests for problem controller.
*/

const problemController = require("../../src/controllers/problemController");
const problemGenerator = require("../../src/problem-generators");
const index = require("../../src/problem-generators/index.json");

jest.mock("../../src/problem-generators");

describe("ProblemController - getIndex", () => {
it("should return the problem generator index successfully", () => {
const req = {};
const res = {
success: jest.fn(),
};

problemController.getIndex(req, res);

expect(res.success).toHaveBeenCalledWith(
index,
"Problem generator index retrieved successfully."
);
});
});

describe("ProblemController - generateProblem", () => {
it("should generate a problem successfully", () => {
const req = {
params: { 0: "topic1/topic2" },
query: { options: "someOptions" },
};
const res = {
success: jest.fn(),
notFound: jest.fn(),
internalServerError: jest.fn(),
};
const generatedProblem = { problem: "generatedProblem" };
problemGenerator.prototype.generateOne.mockReturnValue(generatedProblem);

problemController.generateProblem(req, res);

expect(problemGenerator.prototype.generateOne).toHaveBeenCalledWith({
path: ["topic1", "topic2"],
options: "someOptions",
});
expect(res.success).toHaveBeenCalledWith(
generatedProblem,
"Problem generated successfully."
);
});

it("should return 404 if generator is not found", () => {
const req = {
params: { 0: "invalidTopic" },
query: { options: "someOptions" },
};
const res = {
success: jest.fn(),
notFound: jest.fn(),
internalServerError: jest.fn(),
};
problemGenerator.prototype.generateOne.mockImplementation(() => {
throw new Error("Generator not found");
});

problemController.generateProblem(req, res);

expect(res.notFound).toHaveBeenCalledWith(
"Generator not found.",
"GENERATOR_NOT_FOUND"
);
});

it("should return 500 if there is an error generating the problem", () => {
const req = {
params: { 0: "topic1/topic2" },
query: { options: "someOptions" },
};
const res = {
success: jest.fn(),
notFound: jest.fn(),
internalServerError: jest.fn(),
};
problemGenerator.prototype.generateOne.mockImplementation(() => {
throw new Error("Some other error");
});

problemController.generateProblem(req, res);

expect(res.internalServerError).toHaveBeenCalledWith(
"Error generating problem.",
"GENERATE_PROBLEM_ERROR"
);
});
});

0 comments on commit 866ae41

Please sign in to comment.