forked from agenda/agendash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
92 lines (74 loc) · 2.22 KB
/
test.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
const test = require('ava');
const supertest = require('supertest');
const express = require('express');
const Agenda = require('agenda');
const agenda = new Agenda().database('mongodb://127.0.0.1/agendash-test-db', 'agendash-test-collection');
const app = express();
app.use('/', require('./app')(agenda));
const request = supertest(app);
test.before.cb(t => {
agenda.on('ready', () => {
t.end();
});
});
test.beforeEach(async () => {
await agenda._collection.deleteMany({}, null);
});
test.serial('GET /api with no jobs should return the correct overview', async t => {
const res = await request.get('/api');
t.is(res.body.overview[0].displayName, 'All Jobs');
t.is(res.body.jobs.length, 0);
});
test.serial('POST /api/jobs/create should confirm the job exists', async t => {
const res = await request.post('/api/jobs/create')
.send({
jobName: 'Test Job',
jobSchedule: 'in 2 minutes',
jobRepeatEvery: '',
jobData: {}
})
.set('Accept', 'application/json');
t.true('created' in res.body);
agenda._collection.count({}, null, (err, res) => {
t.ifError(err);
if (res !== 1) {
throw new Error('Expected one document in database');
}
});
});
test.serial('POST /api/jobs/delete should delete the job', async t => {
const job = await new Promise(resolve => {
agenda.create('Test Job', {})
.schedule('in 4 minutes')
.save(async (err, job) => {
t.ifError(err);
return resolve(job);
});
});
const res = await request.post('/api/jobs/delete')
.send({
jobIds: [job.attrs._id]
})
.set('Accept', 'application/json');
t.true('deleted' in res.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 new Promise(resolve => {
agenda.create('Test Job', {})
.schedule('in 4 minutes')
.save(async (err, job) => {
t.ifError(err);
return resolve(job);
});
});
const res = await request.post('/api/jobs/requeue')
.send({
jobIds: [job.attrs._id]
})
.set('Accept', 'application/json');
t.false('newJobs' in res.body);
const count = await agenda._collection.count({}, null);
t.is(count, 2);
});