forked from agenda/agendash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-fastify.js
107 lines (87 loc) · 2.37 KB
/
test-fastify.js
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const test = require("ava");
const supertest = require("supertest");
const Fastify = require("fastify");
let Agenda = require("agenda");
Agenda = Agenda.Agenda || Agenda;
const agenda = new Agenda().database(
"mongodb://127.0.0.1/agendash-test-db",
"agendash-test-collection",
{ useUnifiedTopology: true }
);
const fastify = Fastify();
fastify.register(
require("../app")(agenda, {
middleware: "fastify",
})
);
const request = supertest(fastify.server);
test.before.cb((t) => {
agenda.on("ready", () => {
t.end();
});
});
test.before(async () => {
await fastify.ready();
});
test.beforeEach(async () => {
await agenda._collection.deleteMany({}, null);
});
test.serial(
"GET /api with no jobs should return the correct overview",
async (t) => {
const response = await request.get("/api?limit=200&skip=0");
t.is(response.body.overview[0].displayName, "All Jobs");
t.is(response.body.jobs.length, 0);
}
);
test.serial(
"POST /api/jobs/create should confirm the job exists",
async (t) => {
const response = await request
.post("/api/jobs/create")
.send({
jobName: "Test Job",
jobSchedule: "in 2 minutes",
jobRepeatEvery: "",
jobData: {},
})
.set("Accept", "application/json");
t.true("created" in response.body);
agenda._collection.count({}, null, (error, result) => {
t.falsy(error);
if (result !== 1) {
throw new Error("Expected one document in database");
}
});
}
);
test.serial("POST /api/jobs/delete should delete the job", async (t) => {
const job = await agenda
.create("Test Job", {})
.schedule("in 4 minutes")
.save();
const response = await request
.post("/api/jobs/delete")
.send({
jobIds: [job.attrs._id],
})
.set("Accept", "application/json");
t.true("deleted" in response.body);
const count = await agenda._collection.count({}, null);
t.is(count, 0);
});
test.serial("POST /api/jobs/requeue should requeue the job", async (t) => {
const job = await agenda
.create("Test Job", {})
.schedule("in 4 minutes")
.save();
const response = await request
.post("/api/jobs/requeue")
.send({
jobIds: [job.attrs._id],
})
.set("Accept", "application/json");
t.false("newJobs" in response.body);
const count = await agenda._collection.count({}, null);
t.is(count, 2);
});