Skip to content

Commit

Permalink
createJob tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cyri113 committed May 21, 2023
1 parent 5bba625 commit b9e70da
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions __test__/controllers/jobs/createJob.controller.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/* eslint-disable @typescript-eslint/consistent-type-assertions */
import { type Request, type Response } from "express";
import { type Queue } from "bullmq";
import { queueByName } from "../../../src/queues";
import createJob from "../../../src/controllers/jobs/createJob.controller";

console.error = jest.fn()

// Mock the queueByName function
jest.mock("../../../src/queues", () => ({
queueByName: jest.fn(),
}));

// Mock the queue's add method
const addMock = jest.fn();

// Create a mock queue object
const queueMock = {
add: addMock,
} as unknown as Queue;

describe("createJob", () => {
let req: Request;
let res: Response;

beforeEach(() => {
req = {
body: {
type: "email",
data: { key: "value" },
},
} as Request;
res = {
json: jest.fn(),
status: jest.fn().mockReturnThis(),
} as unknown as Response;
});

afterEach(() => {
jest.clearAllMocks();
});

it("should add the job to the appropriate queue and return the jobId", async () => {
// Mock the queueByName function to return the queue
(queueByName as jest.Mock).mockReturnValueOnce(queueMock);

// Mock the queue's add method to return a job
const jobId = "job123";
const job = { id: jobId };
addMock.mockResolvedValueOnce(job);

await createJob(req, res);

expect(queueByName).toHaveBeenCalledWith("email");
expect(addMock).toHaveBeenCalledWith("email", { key: "value" });
expect(res.json).toHaveBeenCalledWith({ jobId });
expect(res.status).not.toHaveBeenCalled();
});

it("should handle error when queueByName throws an error", async () => {
const error = new Error("No Queue called unknownQueue");

// Mock the queueByName function to throw an error
(queueByName as jest.Mock).mockImplementationOnce(() => {
throw error;
});

await createJob(req, res);

expect(queueByName).toHaveBeenCalledWith("email");
expect(addMock).not.toHaveBeenCalled();
expect(res.status).toHaveBeenCalledWith(400);
expect(res.json).toHaveBeenCalledWith({ error });
expect(console.error).toHaveBeenCalled();
});

it("should handle error when the queue's add method throws an error", async () => {
const error = new Error("Failed to add job");
addMock.mockRejectedValueOnce(error);

// Mock the queueByName function to return the queue
(queueByName as jest.Mock).mockReturnValueOnce(queueMock);

await createJob(req, res);

expect(queueByName).toHaveBeenCalledWith("email");
expect(addMock).toHaveBeenCalledWith("email", { key: "value" });
expect(res.status).toHaveBeenCalledWith(500);
expect(res.json).toHaveBeenCalledWith({ error: "Failed to create job" });
expect(console.error).toHaveBeenCalledWith("Error creating job:", error);
});
});

0 comments on commit b9e70da

Please sign in to comment.