-
-
Notifications
You must be signed in to change notification settings - Fork 281
/
JobDispatcher.spec.ts
50 lines (40 loc) · 1.45 KB
/
JobDispatcher.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import {InjectorService} from "@tsed/di";
import {JobDispatcher} from "./JobDispatcher";
import {JobMethods} from "../contracts";
import {JobController} from "../decorators";
import {Queue} from "bullmq";
import {instance, mock, verify, when, objectContaining} from "ts-mockito";
@JobController("example-job", "default", {
backoff: 69
})
class ExampleTestJob implements JobMethods {
handle(payload: {msg: string}) {}
}
@JobController("queue-not-configured", "not-configured")
class NotConfiguredQueueTestJob implements JobMethods {
handle() {}
}
describe("JobDispatcher", () => {
it("should throw an exception when a queue is not configured", () => {
const injector = mock(InjectorService);
when(injector.get("bullmq.queue.default")).thenReturn(undefined);
const dispatcher = new JobDispatcher(instance(injector));
expect(dispatcher.dispatch(NotConfiguredQueueTestJob)).rejects.toThrow(new Error("Queue(not-configured) not defined"));
});
it("should dispatch job", async () => {
const injector = mock(InjectorService);
const queue = mock(Queue);
when(injector.get("bullmq.queue.default")).thenReturn(instance(queue));
const dispatcher = new JobDispatcher(instance(injector));
await dispatcher.dispatch(ExampleTestJob, {msg: "hello test"});
verify(
queue.add(
"example-job",
objectContaining({msg: "hello test"}),
objectContaining({
backoff: 69
})
)
).once();
});
});