Issues (12)

tests/units/queue.spec.js (12 issues)

Labels
Severity
1
"use strict";
2
3
const QueueManager = require("./../../lib/cjs/queue");
4
const bullmq = require("bullmq");
5
6
jest.mock("bullmq", () => ({
0 ignored issues
show
The variable jest seems to be never declared. If this is a global, consider adding a /** global: jest */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
7
	Queue: jest.fn(),
0 ignored issues
show
The variable jest seems to be never declared. If this is a global, consider adding a /** global: jest */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
8
	Worker: jest.fn(),
9
	QueueScheduler: jest.fn()
10
}));
11
12
describe('QueueManager', () => {
13
	let mockConfig;
14
15
	beforeEach(() => {
16
		mockConfig = {
17
			storage: {
18
				host: "localhost",
19
				port: 6379,
20
				username: "user",
21
				password: "password"
22
			}
23
		};
24
25
		// Clear instances before each test
26
		QueueManager._instance = null;
27
		jest.clearAllMocks();
0 ignored issues
show
The variable jest seems to be never declared. If this is a global, consider adding a /** global: jest */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
28
	});
29
30
	describe('getInstance', () => {
31
		it('should throw an error if instance is not initialized and no config is passed', () => {
32
			expect(() => QueueManager.getInstance()).toThrow("Need init instance");
33
		});
34
35
		it('should create a new instance with config when none exists', () => {
36
			const instance = QueueManager.getInstance(mockConfig);
37
			expect(instance).toBeInstanceOf(QueueManager);
38
			expect(instance.connection).toEqual({
39
				host: "localhost",
40
				port: 6379,
41
				username: "user",
42
				password: "password"
43
			});
44
		});
45
46
		it('should return the existing instance if it is already initialized', () => {
47
			const firstInstance = QueueManager.getInstance(mockConfig);
48
			const secondInstance = QueueManager.getInstance();
49
			expect(secondInstance).toBe(firstInstance);
50
		});
51
	});
52
53
	describe('getQueue', () => {
54
		it('should create a new queue if it does not exist', () => {
55
			const queueName = "testQueue";
56
			const options = { defaultJobOptions: { removeOnComplete: true } };
57
			const mockQueue = { add: jest.fn() };
0 ignored issues
show
The variable jest seems to be never declared. If this is a global, consider adding a /** global: jest */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
58
59
			bullmq.Queue.mockReturnValue(mockQueue);
60
61
			const instance = QueueManager.getInstance(mockConfig);
62
			const queue = instance.getQueue(queueName, options);
63
64
			expect(bullmq.Queue).toHaveBeenCalledWith(queueName, expect.objectContaining({
0 ignored issues
show
The variable expect seems to be never declared. If this is a global, consider adding a /** global: expect */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
65
				connection: mockConfig.storage,
66
				defaultJobOptions: {
67
					removeOnComplete: true,
68
				}
69
			}));
70
			expect(queue).toBe(mockQueue);
71
		});
72
73
		it('should return the existing queue if it is already created', () => {
74
			const queueName = "testQueue";
75
			const mockQueue = { add: jest.fn() };
0 ignored issues
show
The variable jest seems to be never declared. If this is a global, consider adding a /** global: jest */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
76
			bullmq.Queue.mockReturnValue(mockQueue);
77
78
			const instance = QueueManager.getInstance(mockConfig);
79
			const firstQueue = instance.getQueue(queueName);
80
			const secondQueue = instance.getQueue(queueName);
81
82
			expect(firstQueue).toBe(secondQueue);
83
			expect(bullmq.Queue).toHaveBeenCalledTimes(1);
84
		});
85
	});
86
87
	describe('getWorker', () => {
88
		it('should create a new worker if it does not exist', () => {
89
			const queueName = "testWorkerQueue";
90
			const handleJob = jest.fn();
0 ignored issues
show
The variable jest seems to be never declared. If this is a global, consider adding a /** global: jest */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
91
			const mockWorker = { on: jest.fn() };
92
93
			bullmq.Worker.mockReturnValue(mockWorker);
94
95
			const instance = QueueManager.getInstance(mockConfig);
96
			const worker = instance.getWorker(queueName, handleJob);
97
98
			expect(bullmq.Worker).toHaveBeenCalledWith(queueName, handleJob, expect.objectContaining({
0 ignored issues
show
The variable expect seems to be never declared. If this is a global, consider adding a /** global: expect */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
99
				connection: mockConfig.storage
100
			}));
101
			expect(worker).toBe(mockWorker);
102
		});
103
104
		it('should return the existing worker if it is already created', () => {
105
			const queueName = "testWorkerQueue";
106
			const handleJob = jest.fn();
0 ignored issues
show
The variable jest seems to be never declared. If this is a global, consider adding a /** global: jest */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
107
			const mockWorker = { on: jest.fn() };
108
109
			bullmq.Worker.mockReturnValue(mockWorker);
110
111
			const instance = QueueManager.getInstance(mockConfig);
112
			const firstWorker = instance.getWorker(queueName, handleJob);
113
			const secondWorker = instance.getWorker(queueName, handleJob);
114
115
			expect(firstWorker).toBe(secondWorker);
116
			expect(bullmq.Worker).toHaveBeenCalledTimes(1);
117
		});
118
	});
119
120
	describe('getScheduler', () => {
121
		it('should create a new scheduler if it does not exist', () => {
122
			const queueName = "testSchedulerQueue";
123
			const mockScheduler = { run: jest.fn() };
0 ignored issues
show
The variable jest seems to be never declared. If this is a global, consider adding a /** global: jest */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
124
125
			bullmq.QueueScheduler.mockReturnValue(mockScheduler);
126
127
			const instance = QueueManager.getInstance(mockConfig);
128
			const scheduler = instance.getScheduler(queueName);
129
130
			expect(bullmq.QueueScheduler).toHaveBeenCalledWith(queueName, expect.objectContaining({
0 ignored issues
show
The variable expect seems to be never declared. If this is a global, consider adding a /** global: expect */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
131
				connection: mockConfig.storage
132
			}));
133
			expect(scheduler).toBe(mockScheduler);
134
		});
135
136
		it('should return the existing scheduler if it is already created', () => {
137
			const queueName = "testSchedulerQueue";
138
			const mockScheduler = { run: jest.fn() };
0 ignored issues
show
The variable jest seems to be never declared. If this is a global, consider adding a /** global: jest */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
139
140
			bullmq.QueueScheduler.mockReturnValue(mockScheduler);
141
142
			const instance = QueueManager.getInstance(mockConfig);
143
			const firstScheduler = instance.getScheduler(queueName);
144
			const secondScheduler = instance.getScheduler(queueName);
145
146
			expect(firstScheduler).toBe(secondScheduler);
147
			expect(bullmq.QueueScheduler).toHaveBeenCalledTimes(1);
148
		});
149
	});
150
151
	describe('getElementBullMq', () => {
152
		it('should return the correct BullMQ component', () => {
153
			const instance = QueueManager.getInstance(mockConfig);
154
			const queueComponent = instance.getElementBullMq('Queue');
155
			const workerComponent = instance.getElementBullMq('Worker');
156
			const schedulerComponent = instance.getElementBullMq('QueueScheduler');
157
158
			expect(queueComponent).toBe(bullmq.Queue);
159
			expect(workerComponent).toBe(bullmq.Worker);
160
			expect(schedulerComponent).toBe(bullmq.QueueScheduler);
161
		});
162
	});
163
});
164