Skip to content

Commit

Permalink
getJob.controller.test
Browse files Browse the repository at this point in the history
  • Loading branch information
cyri113 committed May 21, 2023
1 parent 7986590 commit 91f3310
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 4 deletions.
110 changes: 110 additions & 0 deletions __test__/controllers/jobs/getJob.controller.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/* eslint-disable @typescript-eslint/consistent-type-assertions */
import { type Request, type Response } from "express";
import { Job, type Queue } from "bullmq";
import { queueByName } from "../../../src/queues";
import getJob from "../../../src/controllers/jobs/getJob.controller";

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

// Mock the Job class
jest.mock("bullmq", () => ({
Job: {
fromId: jest.fn(),
},
}));

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

beforeEach(() => {
req = {
params: {}
} as Request;
res = {
json: jest.fn(),
status: jest.fn().mockReturnThis(),
} as unknown as Response;
});

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

it("should return job details if job exists", async () => {
const jobId = "job123";
const queue = {} as Queue;
const job = {
id: jobId,
name: "Test Job",
getState: jest.fn().mockResolvedValue("completed"),
progress: 100,
data: { key: "value" },
} as unknown as Job;

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

// Mock the Job.fromId function to return the job
(Job.fromId as jest.Mock).mockResolvedValueOnce(job);

await getJob(req, res);

expect(res.json).toHaveBeenCalledWith({
id: job.id,
name: job.name,
status: "completed",
progress: job.progress,
data: job.data,
});
expect(res.status).not.toHaveBeenCalled();
});

it("should return 404 error if job does not exist", async () => {
const queue = {} as Queue;

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

// Mock the Job.fromId function to return null
(Job.fromId as jest.Mock).mockResolvedValueOnce(null);

await getJob(req, res);

expect(res.status).toHaveBeenCalledWith(404);
expect(res.json).toHaveBeenCalledWith({ error: "Job not found" });
});

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 getJob(req, res);

expect(res.status).toHaveBeenCalledWith(400);
expect(res.json).toHaveBeenCalledWith({ error });
});

it("should handle error when Job.fromId throws an error", async () => {
const queue = {} as Queue;
const error = new Error("Failed to fetch job");

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

// Mock the Job.fromId function to throw an error
(Job.fromId as jest.Mock).mockRejectedValueOnce(error);

await getJob(req, res);

expect(res.status).toHaveBeenCalledWith(500);
expect(res.json).toHaveBeenCalledWith({ error: "Failed to get job status" });
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"build": "npx tsc",
"start": "node dist/index.js",
"dev": "env-cmd nodemon ./src/index.ts",
"test": "env-cmd jest --coverage",
"test": "env-cmd jest --coverage --silent",
"prettier": "npx prettier --write .",
"lint": "npx eslint ."
},
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/jobs/getJob.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const getJob = async function (req: Request, res: Response) {
try {
queue = queueByName(type)
} catch (error) {
console.error(error)
console.error('Error getting job queue:', error)
return res.status(400).json({ error });
}

Expand Down
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions src/workers/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import emailWorker from './emailWorker'
import imageProcessingWorker from './imageProcessingWorker'
import emailWorker from './email.worker'
import imageProcessingWorker from './imageProcessing.worker'

export {
emailWorker,
Expand Down

0 comments on commit 91f3310

Please sign in to comment.