-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.test.js
322 lines (308 loc) · 10.9 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
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
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 /projects', () => {
let response;
beforeEach(async () => {
response = await request(app).get('/api/v1/projects');
});
it('should respond with all of the projects', async () => {
const expectedProjects = await database('projects').select();
const projects = response.body;
expect(projects[0].id).toEqual(expectedProjects[0].id);
expect(projects[0].project_name).toEqual(
expectedProjects[0].project_name
);
});
it('should respond with a status of 200', async () => {
expect(response.status).toBe(200);
});
});
describe('GET /projects/:id', () => {
let mockId;
let response;
beforeEach(async () => {
mockId = await database('projects')
.first('id')
.then(obj => obj.id);
response = await request(app).get(`/api/v1/projects/${mockId}`);
});
it('should respond with the specific project', async () => {
const expectedProject = await database('projects')
.select()
.where('id', mockId);
const project = response.body;
expect(project[0].id).toEqual(expectedProject[0].id);
expect(project[0].project_name).toEqual(expectedProject[0].project_name);
});
it('should respond with a status code of 200', async () => {
expect(response.status).toBe(200);
});
it('should respond with a status code of 404', async () => {
mockId = -1;
response = await request(app).get(`/api/v1/projects/${mockId}`);
expect(response.status).toBe(404);
});
});
describe('GET / projects/:id/palettes', () => {
let mockId;
let response;
beforeEach(async () => {
mockId = await database('projects')
.first('id')
.then(obj => obj.id);
response = await request(app).get(`/api/v1/projects/${mockId}/palettes`);
});
it('should respond with all the palettes for the specified project', async () => {
const expectedProject = await database('palettes')
.select()
.where('project_id', mockId);
const palletes = response.body;
expect(palletes[0].id).toEqual(expectedProject[0].id);
expect(palletes[0].project_name).toEqual(expectedProject[0].project_name);
});
it('should return a status of 200 if the project is found', async () => {
expect(response.status).toBe(200);
});
it('should return a status of 404 if the project is not found', async () => {
mockId = -1;
response = await request(app).get(`/api/v1/projects/${mockId}/palettes`);
expect(response.status).toBe(404);
});
});
describe('GET palettes/search', () => {
let mockColor;
let response;
beforeEach(async () => {
mockColor = await database('palettes')
.first('color_1')
.then(palette => palette.color_1);
response = await request(app).get(
`/api/v1/palettes/search?hex=${mockColor}`
);
});
it('should return the projects whose match the query', async () => {
const expectedPalette = await database('palettes')
.select()
.where(function() {
this.where('color_1', mockColor)
.orWhere('color_2', mockColor)
.orWhere('color_3', mockColor)
.orWhere('color_4', mockColor)
.orWhere('color_5', mockColor);
});
const palette = response.body;
expect(palette[0].color_1).toEqual(expectedPalette[0].color_1);
});
it('should return a status of 200 if palettes are found the search query', async () => {
expect(response.status).toEqual(200);
});
it('should return a status of 404 if palettes are not found in the search query', async () => {
mockColor = 'zzzzzzzzzzzzzzzzzzzzzzzzzz';
response = await request(app).get(
`/api/v1/palettes/search?hex=${mockColor}`
);
expect(response.status).toEqual(404);
});
});
describe('POST /projects', () => {
let body;
let response;
beforeEach(async () => {
body = { project: { project_name: 'name' } };
response = await request(app)
.post('/api/v1/projects')
.send(body);
});
it('should be able to create a new project and return the id of the project', async () => {
const newProject = await database('projects')
.where('id', response.body)
.select();
expect(newProject[0].project_name).toEqual(body.project.project_name);
});
it('should send back a status of 200 when the project is add correctly', async () => {
expect(response.status).toBe(201);
});
it('should send back a status of 422 when the project is not added correctly', async () => {
body = { project: { prot_name: 'name' } };
response = await request(app)
.post('/api/v1/projects')
.send(body);
expect(response.status).toBe(422);
});
});
describe('POST /palettes', () => {
let mockId;
let newPalette;
let response;
beforeEach(async () => {
mockId = await database('projects')
.first('id')
.then(obj => obj.id);
newPalette = {
palette: {
project_id: mockId,
palette_name: 'Autumn Eclipse',
color_1: '333322',
color_2: '777777',
color_3: '884422',
color_4: 'eeeeff',
color_5: 'aa9999'
}
};
response = await request(app)
.post('/api/v1/palettes')
.send(newPalette);
});
it('should be able to create a new palette and return the id of the palette', async () => {
const expectedPalette = await database('palettes')
.where('id', response.body.id)
.select();
expect(expectedPalette[0].id).toEqual(response.body.id);
expect(expectedPalette[0].palette_name).toEqual(
newPalette.palette.palette_name
);
});
it('should resond with a status of 201 if the palette is posted correctly', async () => {
expect(response.status).toBe(201);
});
it('should respond with a status of 422 if the palette did not include all needed values', async () => {
newPalette = {
palette: {
project_id: mockId
}
};
response = await request(app)
.post('/api/v1/palettes')
.send(newPalette);
expect(response.status).toBe(422);
});
});
describe('DELETE /palettes', () => {
let mockId;
let response;
beforeEach(async () => {
mockId = await database('palettes')
.first('id')
.then(obj => obj.id);
response = await request(app).delete(`/api/v1/palettes/${mockId}`);
});
it("should delete a palette and return that palette's id", async () => {
const deletedItem = await database('palettes')
.where('id', mockId)
.select();
expect(response.body.id).toEqual(mockId);
expect(deletedItem.length).toEqual(0);
});
it('should return a status of 201 if the palette was successfully deleted', async () => {
expect(response.status).toBe(201);
});
it('should return a status of 404', async () => {
mockId = -1;
response = await request(app).delete(`/api/v1/palettes/${mockId}`);
expect(response.status).toBe(404);
});
});
describe('DELETE /projects', () => {
let mockId;
let response;
beforeEach(async () => {
mockId = await database('projects')
.first('id')
.then(obj => obj.id);
response = await request(app).delete(`/api/v1/projects/${mockId}`);
});
it('should delete a project and return that project id', async () => {
const deletedPalettes = await database('palettes')
.where('project_id', mockId)
.select();
const deletedProjects = await database('projects')
.where('id', mockId)
.select();
expect(deletedProjects.length).toEqual(0);
expect(deletedPalettes.length).toEqual(0);
expect(response.body.id).toEqual(mockId);
});
it('should return a status of 201 if the project and its palettes was successfully deleted', async () => {
expect(response.status).toBe(201);
});
it('should return a status of 404 if the delete was unsuccessful', async () => {
mockId = -1;
response = await request(app).delete(`/api/v1/projects/${mockId}`);
expect(response.status).toBe(404);
});
});
describe('PATCH /palettes/:id', () => {
let mockId;
beforeEach(async () => {
mockId = await database('palettes')
.first('id')
.then(obj => obj.id);
});
it('should update the colors on the chosen palette', async () => {
const palette = {
palette_name: 'autumn foliage',
color_1: '111111',
color_2: '222222',
color_3: '333333',
color_4: '444444',
color_5: '555555'
};
const response = await request(app)
.patch(`/api/v1/palettes/${mockId}`)
.send(palette);
const expectedUpdate = await database('palettes').where('id', mockId);
expect(expectedUpdate[0].color_1).toEqual('111111');
expect(expectedUpdate[0].color_2).toEqual('222222');
expect(parseInt(response.body.id)).toEqual(mockId);
expect(response.status).toBe(202);
});
it('should respond with a status code of 422 if the required parameters are not given', async () => {
const palette = {
palette_name: 'autumn foliage',
color_4: '444444',
color_6: '555555'
};
const response = await request(app)
.patch(`/api/v1/palettes/${mockId}`)
.send(palette);
const expectedPalette = await database('palettes').where('id', mockId);
expect(response.status).toBe(422);
expect(expectedPalette.color_4).not.toEqual(palette.color_4);
});
});
describe('PATCH /projects', () => {
let mockId;
beforeEach(async () => {
mockId = await database('projects')
.first('id')
.then(obj => obj.id);
});
it("should update a project's name and respond with the project id and status code of 202", async () => {
const body = {
project_name: 'Edited Project Title'
};
const response = await request(app)
.patch(`/api/v1/projects/${mockId}`)
.send(body);
const expectedProject = await database('projects').where('id', mockId);
expect(expectedProject[0].project_name).toEqual(body.project_name);
expect(response.status).toBe(202);
});
it('should respond with a 422 if the required parameters were not met', async () => {
const body = {
project_name: ''
};
const response = await request(app)
.patch(`/api/v1/projects/${mockId}`)
.send(body);
expect(response.status).toBe(422);
});
});
});