Repository navigation
Expand file tree
/
Copy pathindex.test.js
More file actions
61 lines (51 loc) · 1.95 KB
/
Copy pathindex.test.js
File metadata and controls
61 lines (51 loc) · 1.95 KB
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
const fs = require('fs');
const path = require('path');
const request = require('supertest');
const testDataFile = path.join(__dirname, 'test-notifications.json');
let app;
beforeEach(() => {
fs.writeFileSync(
testDataFile,
JSON.stringify({ notifications: [] }, null, 2),
);
process.env.DATA_FILE = testDataFile;
jest.resetModules();
app = require('./index')
});
afterEach(() => {
if (fs.existsSync(testDataFile)) {
fs.unlinkSync(testDataFile);
}
delete process.env.DATA_FILE;
});
test('Retrieves an empty list of notifications', async () => {
const res = await request(app).get('/api/notifications');
expect(res.statusCode).toBe(200);
expect(res.body).toEqual([]);
});
test('Creates a notification', async () => {
const res = await request(app).post('/api/notifications').send({ message: 'Test notification', type: 'success'});
expect(res.statusCode).toBe(200);
expect(res.body.message).toBe('Test notification');
expect(res.body.type).toBe('success');
expect(res.body).toHaveProperty('id');
expect(res.body).toHaveProperty('timestamp')
});
test('Returns a notification that was created', async () => {
const postRes = await request(app).post('/api/notifications').send({ message: 'Test notification', type: 'success'});
expect(postRes.statusCode).toBe(200);
const getRes = await request(app).get('/api/notifications');
expect(getRes.statusCode).toBe(200);
expect(getRes.body).toHaveLength(1);
expect(getRes.body[0].message).toBe('Test notification');
expect(getRes.body[0].type).toBe('success');
expect(getRes.body[0].id).toBe(postRes.body.id);
});
test('Deletes all notifications (DELETE)', async() => {
await request(app).post('/api/notifications').send({ message: 'One', type: 'info' });
const delRes = await request(app).delete('/api/notifications');
expect(delRes.statusCode).toBe(200);
expect(delRes.body.success).toBe(true);
const getRes = await request(app).get('/api/notifications');
expect(getRes.body).toEqual([]);
});