-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver.test.ts
73 lines (64 loc) · 2.12 KB
/
server.test.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
import supertest from 'supertest';
import app from '../src/index';
const request = supertest(app);
// get all users
describe('GET /users', () => {
it('should get all users', async () => {
const response = await request.get('/users');
expect(response.status).toBe(200);
expect(response.body).toEqual([
{ id: 1, name: 'John' },
{ id: 2, name: 'Jane' },
{ id: 3, name: 'Bob' },
]);
});
}
);
// get user by id
describe('GET /user/:id', () => {
it('should get user name by ID', async () => {
const response = await request.get('/user/1');
expect(response.status).toBe(200);
expect(response.body).toEqual({ id: 1, name: 'John' });
});
it('should return 404 for non-existing user ID', async () => {
const response = await request.get('/user/99');
expect(response.status).toBe(404);
expect(response.body).toEqual({ error: 'User not found' });
});
});
// post a new user
describe('POST /user', () => {
it('should add a new user', async () => {
const requestBody = { id: 9, name: 'test' };
const response = await request.post('/user').send(requestBody);
expect(response.status).toBe(200);
const response2 = await request.get('/user/9');
expect(response2.status).toBe(200);
expect(response2.body).toEqual({ id: 9, name: 'test' });
});
}
);
// delete a user
describe('DELETE /user/:id', () => {
it('should delete a user', async () => {
const response = await request.delete('/user/1');
expect(response.status).toBe(200);
const response2 = await request.get('/user/1');
expect(response2.status).toBe(404);
expect(response2.body).toEqual({ error: 'User not found' });
});
}
);
// update a user
describe('PUT /user/:id', () => {
it('should update a user', async () => {
const requestBody = { id: 2, name: 'test' };
const response = await request.put('/user/2').send(requestBody);
expect(response.status).toBe(200);
const response2 = await request.get('/user/2');
expect(response2.status).toBe(200);
expect(response2.body).toEqual({ id: 2, name: 'test' });
});
}
);