-
Notifications
You must be signed in to change notification settings - Fork 2
/
notes.spec.ts
328 lines (305 loc) · 10.9 KB
/
notes.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import { test, describe, expect, vi, beforeAll } from 'vitest';
import request from 'supertest';
import app from '../../src/index';
import prisma from '../../src/__mocks__/prisma';
import { noteSeed } from './mocks/notes.mock';
import { userSeed } from './mocks/users.mock';
beforeAll(() => {
// Mock the prisma client
vi.mock('../../src/prisma');
// Mock the authenticateToken function
vi.mock('../../src/authenticateToken', () => {
return {
default: (req, res, next) => {
req.user = { id: 'ccf89a7e-b941-4f17-bbe0-4e0c8b2cd272' };
next();
},
};
});
});
describe('View notes', () => {
test('No notes returned - success', async ({}) => {
prisma.note.findMany.mockResolvedValue([]);
const response = await request(app).get('/api/notes');
expect(response.status).toBe(200);
expect(response.body).toStrictEqual([]);
});
test('Single note returned - success', async ({}) => {
prisma.note.findMany.mockResolvedValue([noteSeed[0]]);
const response = await request(app).get('/api/notes');
expect(response.status).toBe(200);
expect(response.body).toEqual([
{
id: 'a1b2c3d4-1234-5678-9abc-abcdef123456',
title: 'Meeting Notes',
content: 'Discussed project timelines and goals.',
createdAt: '2024-02-05T23:43:42.252Z',
updatedAt: '2024-02-05T23:33:42.252Z',
userID: 'ccf89a7e-b941-4f17-bbe0-4e0c8b2cd272',
},
]);
});
test('Many notes returned - success', async ({}) => {
prisma.note.findMany.mockResolvedValue(noteSeed);
const response = await request(app).get('/api/notes');
expect(response.status).toBe(200);
const expectedResult = noteSeed.map((item) => ({
...item,
createdAt: new Date(item.createdAt).toISOString(),
updatedAt: new Date(item.updatedAt).toISOString(),
userID: 'ccf89a7e-b941-4f17-bbe0-4e0c8b2cd272',
}));
expect(response.body).toEqual(expectedResult);
});
test('500 error - failure', async ({}) => {
prisma.note.findMany.mockImplementation(() => {
throw new Error('Test error');
});
const response = await request(app).get('/api/notes');
expect(response.status).toBe(500);
});
});
describe('Create a note', () => {
test('POST with title and content', async ({}) => {
prisma.note.create.mockResolvedValue({
content: 'Test',
title: 'Test',
id: 'a1b2c3d4-1234-5678-9abc-abcdef123456',
updatedAt: new Date('2024-02-05T23:33:42.252Z'),
createdAt: new Date('2024-02-05T23:33:42.252Z'),
userID: 'ccf89a7e-b941-4f17-bbe0-4e0c8b2cd272',
});
const response = await request(app)
.post('/api/notes')
.send({ content: 'Test', title: 'Test' });
expect(response.status).toBe(200);
});
test('POST with with title - failure', async ({}) => {
const response = await request(app)
.post('/api/notes')
.send({ content: 'Test' });
expect(response.status).toBe(400);
expect(response.body).toStrictEqual({
error: 'title and content fields required',
});
});
test('POST without content - failure', async ({}) => {
const response = await request(app)
.post('/api/notes')
.send({ title: 'Test' });
expect(response.status).toBe(400);
});
test('POST without title - failure', async ({}) => {
const response = await request(app)
.post('/api/notes')
.send({ content: 'Test' });
expect(response.status).toBe(400);
});
test('POST with 500 error', async ({}) => {
prisma.note.create.mockImplementation(() => {
throw new Error('Test error');
});
const response = await request(app)
.post('/api/notes')
.send({ content: 'Test', title: 'Test' });
expect(response.status).toBe(500);
expect(response.body).toStrictEqual({
error: 'Oops, something went wrong',
});
});
});
describe('Update a note', () => {
test('PUT update note - success', async ({}) => {
prisma.note.findUnique.mockResolvedValue({
title: 'Test',
content: 'Test',
id: 'a1b2c3d4-1234-5678-9abc-abcdef123457',
updatedAt: new Date('2024-02-05T23:33:42.252Z'),
createdAt: new Date('2024-02-05T23:33:42.252Z'),
userID: 'ccf89a7e-b941-4f17-bbe0-4e0c8b2cd272',
});
prisma.note.update.mockResolvedValue({
title: 'Test update',
content: 'Test',
id: 'a1b2c3d4-1234-5678-9abc-abcdef123457',
updatedAt: new Date('2024-02-05T23:33:42.252Z'),
createdAt: new Date('2024-02-05T23:33:42.252Z'),
userID: 'ccf89a7e-b941-4f17-bbe0-4e0c8b2cd272',
});
const response = await request(app)
.put('/api/notes/a1b2c3d4-1234-5678-9abc-abcdef123457')
.send({ title: 'Test update', content: 'Test', id: 1 });
expect(response.status).toBe(200);
expect(response.body).toStrictEqual({
title: 'Test update',
content: 'Test',
createdAt: '2024-02-05T23:33:42.252Z',
id: 'a1b2c3d4-1234-5678-9abc-abcdef123457',
updatedAt: '2024-02-05T23:33:42.252Z',
userID: 'ccf89a7e-b941-4f17-bbe0-4e0c8b2cd272',
});
});
test('PUT without title - failure', async ({}) => {
const response = await request(app)
.put('/api/notes/a1b2c3d4-1234-5678-9abc-abcdef123457')
.send({ content: 'Test', id: 1 });
expect(response.status).toBe(400);
});
test('PUT without content - failure', async ({}) => {
const response = await request(app)
.put('/api/notes/a1b2c3d4-1234-5678-9abc-abcdef123457')
.send({ title: 'Test' });
expect(response.status).toBe(400);
});
test('PUT without id in url - failure', async ({}) => {
const response = await request(app)
.put('/api/notes/')
.send({ title: 'Test', content: 'Test', id: 1 });
expect(response.status).toBe(404);
});
test('PUT with a 404 error - failure', async ({}) => {
prisma.note.update.mockImplementation(() => {
throw new Error('Test error');
});
const response = await request(app)
.put('/api/notes/a1b2c3d4-1234-5678-9abc-abcdef123457')
.send({ title: 'Test update', content: 'Test', id: 1 });
expect(response.status).toBe(404);
expect(response.body).toStrictEqual({
error: 'Note not found',
});
});
test('PUT with a 500 error - failure', async ({}) => {
prisma.note.findUnique.mockResolvedValue({
title: 'Test',
content: 'Test',
id: 'a1b2c3d4-1234-5678-9abc-abcdef123457',
updatedAt: new Date('2024-02-05T23:33:42.252Z'),
createdAt: new Date('2024-02-05T23:33:42.252Z'),
userID: 'ccf89a7e-b941-4f17-bbe0-4e0c8b2cd272',
});
prisma.note.update.mockImplementation(() => {
throw new Error('Test error');
});
const response = await request(app)
.put('/api/notes/a1b2c3d4-1234-5678-9abc-abcdef123457')
.send({ title: 'Test update', content: 'Test', id: 1 });
expect(response.status).toBe(500);
expect(response.body).toStrictEqual({
error: 'Oops, something went wrong',
});
});
});
describe('Delete a note', () => {
test('DELETE with id error', async ({}) => {
const response = await request(app).delete('/api/notes/1');
expect(prisma.note.delete).toHaveBeenCalled();
expect(response.status).toBe(200);
expect(response.body).toStrictEqual({ status: 'ok' });
});
test('DELETE without id - failure', async ({}) => {
const response = await request(app).delete('/api/notes/');
expect(response.status).toBe(404);
});
test('DELETE with id error', async ({}) => {
prisma.note.delete.mockRejectedValue({});
const response = await request(app).delete('/api/notes/1');
expect(prisma.note.delete).toHaveBeenCalled();
expect(response.status).toBe(500);
expect(response.body).toStrictEqual({
error: 'Oops, something went wrong',
});
});
});
describe('Health check', () => {
test('GET /api/health', async ({}) => {
const response = await request(app).get('/api/health');
expect(response.status).toBe(200);
expect(response.body).toStrictEqual({ status: 'ok' });
});
});
describe('Get Users', () => {
test('No Users returned', async ({}) => {
prisma.user.findMany.mockResolvedValue([]);
const response = await request(app).get('/api/users');
expect(response.status).toBe(200);
expect(response.body).toStrictEqual([]);
});
test('Should get many users returned', async () => {
prisma.user.findMany.mockResolvedValue(userSeed);
const response = await request(app).get('/api/users');
expect(response.status).toBe(200);
const expectedResult = userSeed.map((item) => ({
...item,
createdAt: new Date(item.createdAt).toISOString(),
updatedAt: new Date(item.updatedAt).toISOString(),
}));
expect(response.body[0]).not.toHaveProperty('password');
expect(response.body).toEqual(expectedResult);
});
test('Network Error', async ({}) => {
prisma.user.findMany.mockImplementation(() => {
throw new Error('Test error');
});
const response = await request(app).get('/api/users');
expect(response.status).toBe(500);
expect(response.body).toStrictEqual({
error: 'Oops, something went wrong',
});
});
});
describe('Create User', () => {
test('POST with email, username and password', async ({}) => {
prisma.user.create.mockResolvedValue({
id: 'gcf89a7e-b941-4f17-bbe0-4e0c8b2cd272',
email: '[email protected]',
username: 'Dave',
password: 'check',
updatedAt: new Date('2024-02-05T23:33:42.252Z'),
createdAt: new Date('2024-02-05T23:33:42.252Z'),
});
const response = await request(app)
.post('/api/users')
.send({ email: 'email', username: 'Dave', password: 'check' });
expect(response.status).toBe(200);
});
test('POST without email', async ({}) => {
const response = await request(app)
.post('/api/users')
.send({ username: 'Dave', password: 'check' });
expect(response.status).toBe(400);
expect(response.body).toStrictEqual({
error: 'email, password, and username fields required',
});
});
test('POST without username', async ({}) => {
const response = await request(app)
.post('/api/users')
.send({ email: '[email protected]', password: 'check' });
expect(response.status).toBe(400);
expect(response.body).toStrictEqual({
error: 'email, password, and username fields required',
});
});
test('POST without password', async ({}) => {
const response = await request(app)
.post('/api/users')
.send({ email: '[email protected]', username: 'check' });
expect(response.status).toBe(400);
expect(response.body).toStrictEqual({
error: 'email, password, and username fields required',
});
});
test('POST with error', async ({}) => {
prisma.user.create.mockImplementation(() => {
throw new Error('Test error');
});
const response = await request(app)
.post('/api/users')
.send({ email: 'email', username: 'Dave', password: 'check' });
expect(response.status).toBe(500);
expect(response.body).toStrictEqual({
error: 'Oops, something went wrong',
});
});
});