-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.test.js
260 lines (184 loc) · 8.69 KB
/
app.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
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
const request = require('supertest');
const app = require('./app');
const environment = process.env.NODE_ENV || 'development'
const configuration = require('./knexfile')[environment]
const database = require('knex')(configuration);
describe('API', () => {
beforeEach(async () => {
await database.seed.run()
})
describe('GET all projects', () => {
it('should return a 200 and all of the projects', async () => {
const expectedProjects = await database('projects').select();
const response = await request(app).get('/api/v1/projects');
const projects = response.body;
console.log(projects)
expect(response.status).toBe(200);
expect(projects.projects[0].name).toEqual(expectedProjects[0].name)
})
})
describe('GET one project', () => {
it('should return a 200 and one project', async () => {
const expectedProject = await database('projects').first();
const id = expectedProject.id
const response = await request(app).get(`/api/v1/projects/${id}`);
const project = response.body.project[0];
expect(response.status).toBe(200);
expect(project.name).toEqual(expectedProject.name);
})
it('should return a 404 status code and a messge project not found', async () => {
const invalidId = -1;
const response = await request(app).get(`/api/v1/projects/${invalidId}`);
expect(response.status).toBe(404);
expect(response.body.error).toEqual(`Cannot find project with id ${invalidId}`)
})
})
describe('GET all palletes', () => {
it('should return a 200 status code and all palettes of one project', async () => {
const project = await database('projects').first();
const id = project.id;
const expectedPalettes = await database('palettes').where('project_id', id).select();
const response = await request(app).get(`/api/v1/projects/${id}/palettes`);
const palettes = response.body.palettes;
if (!palettes.error) {
expect(response.status).toBe(200);
expect(palettes[0].name).toEqual(expectedPalettes[0].name)
} else {
expect(response.status).toBe(404);
expect(response.body.error).toEqual(`Cannot find palettes under project with id ${id}`)
}
})
it('should return status code 404 and message Cannot find project with id if the project was not exist', async () => {
const invalidId = -1;
const response = await request(app).get(`/api/v1/projects/${invalidId}/palettes`);
expect(response.status).toBe(404);
expect(response.body.error).toEqual(`Cannot find project with id ${invalidId}`)
})
it('should return status code 404 and message Cannot find palettes if there were no palettes', async () => {
const project = await database('projects').where('name', 'Seed Project 3').select();
const id = project[0].id;
const response = await request(app).get(`/api/v1/projects/${id}/palettes`);
console.log(response.body.palettes)
expect(response.status).toBe(404);
expect(response.body.palettes).toEqual(`Cannot find palettes under this project`)
})
})
describe('GET one palette', () => {
it('should return 200 status code and one palette', async () => {
const expectedPalette = await database('palettes').first();
const id = expectedPalette.id;
const response = await request(app).get(`/api/v1/projects/palettes/${id}`)
const palette = response.body[0];
expect(response.status).toBe(200);
expect(palette.name).toEqual(expectedPalette.name);
})
it('should return 404 and message cannot find palette with id', async () => {
const invalidId = -1;
const response = await request(app).get(`/api/v1/projects/palettes/${invalidId}`)
expect(response.status).toBe(404);
expect(response.body.error).toEqual(`Cannot find palette with id ${invalidId}`)
})
})
describe('POST a project', () => {
it('should post new projects and return status code 201 with he id of new item', async () => {
const newProject = {name: 'Project test 2'};
const response = await request(app).post('/api/v1/projects').send(newProject)
const id = response.body.id
const project = await database('projects').where('id', id).select();
expect(response.status).toBe(201);
expect(project[0].name).toEqual(newProject.name);
})
it('should send status code 422 with message: Expected format: { name: <String> }. You\'re missing a name property.', async () => {
const newProject = {};
const response = await request(app).post('/api/v1/projects').send(newProject)
expect(response.status).toBe(422);
expect(response.body.error).toEqual('Expected format: { name: <String> }. You\'re missing a name property.');
})
})
describe('POST a palette', () => {
it('should post new projects and return status code 201 with he id of new item', async () => {
const project = await database('projects').first();
const id = project.id
const newPalette = {
name: 'Project test 2',
color_1: '#fffff',
color_2: '#fffff',
color_3: '#fffff',
color_4: '#fffff',
color_5: '#fffff'
};
const response = await request(app).post(`/api/v1/projects/${id}`).send(newPalette)
const paletteId = response.body.id
const palette = await database('palettes').where('id', paletteId).select();
expect(response.status).toBe(201);
expect(palette[0].name).toEqual(newPalette.name);
expect(palette[0].color_3).toEqual(newPalette.color_3);
})
it('should send status code 422 with message mentiong missing parameters', async () => {
const project = await database('projects').first();
const id = project.id
const newPalette = {};
let setRequiredParameter
for (let requiredParameter of ['color_5', 'color_4', 'color_3', 'color_2', 'color_1', 'name']) {
if (!newPalette[requiredParameter]) {
setRequiredParameter = requiredParameter
}
}
const response = await request(app).post(`/api/v1/projects/${id}`).send(newPalette)
expect(response.status).toBe(422);
expect(response.body.error).toEqual(`Expected format: { name: <String>, color_1: <String>, color_2: <String>, color_3: <String>, color_4: <String>, color_5: <String> }. You\'re missing a "${setRequiredParameter}" property.`);
})
})
describe('Delete project', () => {
it('should return status code of 201 with the id of deleted project', async () => {
const expectedProject = await database('projects').first();
const id = expectedProject.id;
const res = await request(app).delete(`/api/v1/projects/${id}`);
const projectId = parseInt(res.body.id);
expect(res.status).toBe(201);
expect(projectId).toEqual(id);
})
})
describe('Delete palette', () => {
it('should return status code of 201 with the id of deleted palette', async () => {
const expectedPalette = await database('projects').first();
const id = expectedPalette.id;
const res = await request(app).delete(`/api/v1/projects/palettes/${id}`);
const projectId = parseInt(res.body.id);
expect(res.status).toBe(201);
expect(projectId).toEqual(id);
})
})
describe('PATCH project', () => {
it('should return status code of 201 with the id of updated project', async () => {
const expectedProject = await database('projects').first();
const id = expectedProject.id;
const updatedProject = {
name: 'new name'
}
const res = await request(app).patch(`/api/v1/projects/${id}`).send({...updatedProject})
const project = await database('projects').where({ id }).select();
expect(res.status).toBe(201);
expect(project[0].name).toEqual('new name');
})
it('should return status code of 424 with message Cannot found project with id', async () => {
const invalidId = -1;
const updatedProject = false
const res = await request(app).patch(`/api/v1/projects/${invalidId}`).send({...updatedProject})
expect(res.status).toBe(424);
})
})
describe('PATCH palettes', () => {
it('should return status code of 201 with the id of updated palette', async () => {
const expectedPalette = await database('palettes').first();
const id = expectedPalette.id;
const updatedPalette = {
name: 'new name of palette'
}
const res = await request(app).patch(`/api/v1/projects/palettes/${id}`).send({...updatedPalette})
const palette = await database('palettes').where({ id }).select();
expect(res.status).toBe(201);
expect(palette[0].name).toEqual('new name of palette');
})
})
})