-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.test.js
151 lines (118 loc) · 5.31 KB
/
api.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
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
const fetch = require("node-fetch");
const baseUrl = 'http://localhost:3000';
async function fetchWithVersion(url, version, options = {}) {
const headers = { 'API-Version': version, ...options.headers };
return fetch(url, { ...options, headers });
}
describe('User API', () => {
describe('V1 Tests', () => {
it('GET /users should return basic user details (V1)', async () => {
const response = await fetchWithVersion(`${baseUrl}/users`, 'v1');
const data = await response.json();
expect(response.status).toBe(200);
expect(Array.isArray(data)).toBe(true);
data.forEach(user => {
expect(Object.keys(user)).toEqual(['id', 'name', 'email']);
});
});
it('GET /users/:id for an existing user should return the user details', async () => {
const userId = 1;
const response = await fetchWithVersion(`${baseUrl}/users/${userId}`, 'v1');
const data = await response.json();
expect(response.status).toBe(200);
expect(typeof data).toBe('object')
expect(Object.keys(data)).toEqual(['id', 'name', 'email']);
expect(data.id).toBe(userId);
});
it('GET /users/:id for a non-existing user should return a not found response', async () => {
const nonExistingUserId = 9999;
const response = await fetchWithVersion(`${baseUrl}/users/${nonExistingUserId}`, 'v1');
expect(response.status).toBe(404);
});
it('PUT /users/:id should update basic user details (V1)', async () => {
const updatedUserInfo = { name: "New Name", email: "[email protected]" };
const response = await fetchWithVersion(`${baseUrl}/users/1`, 'v1', {
method: 'PUT',
body: JSON.stringify(updatedUserInfo),
headers: { 'Content-Type': 'application/json' }
});
const data = await response.json();
expect(response.status).toBe(200);
expect(data).toEqual(expect.objectContaining(updatedUserInfo));
});
});
describe('V2 Tests', () => {
it('GET /users should return all user details (V2)', async () => {
const response = await fetchWithVersion(`${baseUrl}/users`, 'v2');
const data = await response.json();
expect(response.status).toBe(200);
expect(Array.isArray(data)).toBe(true);
data.forEach(user => {
expect(Object.keys(user)).toEqual(['id', 'name', 'email', 'role', 'favorite_movie', 'location']);
});
});
it('GET /users/:id for an existing user should return the user details (V2)', async () => {
const userId = 1;
const response = await fetchWithVersion(`${baseUrl}/users/${userId}`, 'v2');
const data = await response.json();
expect(response.status).toBe(200);
expect(typeof data).toBe('object')
expect(Object.keys(data)).toEqual(['id', 'name', 'email', 'role', 'favorite_movie', 'location']);
expect(data.id).toBe(userId);
});
it('GET /users/:id for a non-existing user should return a not found response', async () => {
const nonExistingUserId = 9999;
const response = await fetchWithVersion(`${baseUrl}/users/${nonExistingUserId}`, 'v2');
expect(response.status).toBe(404);
});
it('PUT /users/:id should update all user details (V2)', async () => {
const updatedUserInfo = {
name: "Updated Name",
email: "[email protected]",
role: "Updated Role",
favorite_movie: "Updated Movie",
location: "Updated Location"
};
const response = await fetchWithVersion(`${baseUrl}/users/1`, 'v2', {
method: 'PUT',
body: JSON.stringify(updatedUserInfo),
headers: { 'Content-Type': 'application/json' }
});
const data = await response.json();
expect(response.status).toBe(200);
expect(data).toEqual(expect.objectContaining(updatedUserInfo));
});
it('GET /users with pagination parameters should return paginated results', async () => {
const limit = 10;
const page = 1;
const response = await fetchWithVersion(`${baseUrl}/users?limit=${limit}&page=${page}`, 'v2');
const data = await response.json();
expect(response.status).toBe(200);
expect(Array.isArray(data)).toBe(true);
expect(data.length).toBeLessThanOrEqual(limit)
});
it('GET /users with different page should return different results', async () => {
const limit = 10;
const page1 = 1;
const page2 = 2;
const response1 = await fetchWithVersion(`${baseUrl}/users?limit=${limit}&page=${page1}`, 'v2');
const data1 = await response1.json();
const response2 = await fetchWithVersion(`${baseUrl}/users?limit=${limit}&page=${page2}`, 'v2');
const data2 = await response2.json();
expect(response1.status).toBe(200);
expect(response2.status).toBe(200);
expect(Array.isArray(data1)).toBe(true);
expect(Array.isArray(data2)).toBe(true);
expect(data1).not.toEqual(data2)
});
it('GET /users with last page parameter should return fewer or no users', async () => {
const limit = 10;
const lastPage = 100;
const response = await fetch(`${baseUrl}/users?limit=${limit}&page=${lastPage}`);
const data = await response.json();
expect(response.status).toBe(200);
expect(Array.isArray(data)).toBe(true);
expect(data.length).toBeLessThanOrEqual(limit)
});
});
});