-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.test.js
More file actions
103 lines (88 loc) · 3.71 KB
/
server.test.js
File metadata and controls
103 lines (88 loc) · 3.71 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
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
////TESTING/////////////////////
const request = require('supertest');
const app = require('./server');
const {test} = require("./knexfile"); // Assuming your Express app is exported from app.js
describe('GET /all-users', () => {
it('should respond with status code 200 and return an array of users', async () => {
const response = await request(app).get('/all-users');
expect(response.status).toBe(200);
expect(Array.isArray(response.body)).toBe(true);
},10000);
});
describe('GET /single-user/:id', () => {
it('should respond with status code 200 and return the user object', async () => {
const response = await request(app).get('/single-user/4');
expect(response.status).toBe(200);
expect(Array.isArray(response.body)).toBe(true);
expect(response.body[0]).toHaveProperty('id');
expect(response.body[0]).toHaveProperty('name');
expect(response.body[0]).toHaveProperty('email');
expect(response.body[0]).toHaveProperty('phone_number');
expect(response.body[0]).toHaveProperty('is_active');
expect(response.body[0]).toHaveProperty('created_at');
expect(response.body[0]).toHaveProperty('updated_at');
}, 10000);
it('should respond with status code 404 and no body if user does not exist', async () => {
const response = await request(app).get('/single-user/100');
expect(response.status).toBe(404);
expect(response.body).toEqual({});
}, 10000);
});
describe('POST /create-user', () => {
it('should respond with status code 201 and a success message', async () => {
const newUser = {
id: 24,
name: 'Navin1',
email: 'navin1@example.com',
phone_number: '1233448459',
status: false
};
const response = await request(app)
.post('/create-user')
.send(newUser);
expect(response.status).toBe(201);
expect(response.text).toBe('user added successfully');
}, 10000);
});
describe('PATCH /update-user/:id', () => {
it('should respond with status code 200 and a success message', async () => {
const updatedUser = {
phone_number: '5871967825',
is_active: true
};
const response = await request(app)
.patch('/update-user/9')
.send(updatedUser);
expect(response.status).toBe(200);
expect(response.text).toBe('User updated successfully');
}, 10000);
});
describe('PUT /update-f-user/:id', () => {
it('should respond with status code 200 and a success message', async () => {
const updatedUser = {
name: 'narendra',
email: 'naramail@example.com',
phone_number: '8937183574',
is_active: true
};
const response = await request(app)
.put('/update-f-user/13') // Assuming user with ID 1 exists
.send(updatedUser);
expect(response.status).toBe(200);
expect(response.text).toBe('User replaced successfully');
}, 10000);
});
describe('DELETE /delete-user/:id', () => {
it('should respond with status code 200 and a success message', async () => {
const response = await request(app).delete('/delete-user/14'); // Assuming user with ID 1 exists
expect(response.status).toBe(200);
expect(response.text).toBe('User deleted successfully');
}, 10000);
});
describe('GET /batch-users-req', () => {
it('should respond with status code 200 and return an array of users', async () => {
const response = await request(app).get('/batch-users-req?pg=1&lm=4');
expect(response.status).toBe(200);
expect(Array.isArray(response.body)).toBe(true);
}, 10000);
});