-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.test.js
More file actions
252 lines (233 loc) · 10 KB
/
Copy pathapp.test.js
File metadata and controls
252 lines (233 loc) · 10 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
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
const environment = process.env.NODE_ENV || 'development';
const configuration = require('./knexfile')[environment];
const database = require('knex')(configuration);
const request = require('supertest')
const app = require('./app')
describe('/api/v1', () => {
beforeEach(async () => {
await database.seed.run();
});
describe('GET /projects', () => {
it('should return all projects in database', async () => {
const expected = await database('projects').select()
const response = await request(app).get('/api/v1/projects')
expect(response.status).toBe(200)
expect(response.body.length).toBe(expected.length)
});
});
describe('GET /projects/:id', () => {
it('should return a single project', async () => {
const expectedProject = await database('projects').first();
const id = expectedProject.id;
const response = await request(app).get(`/api/v1/projects/${id}`);
const result = response.body[0];
expect(response.status).toBe(200);
expect(result.project_name).toEqual(expectedProject.project_name);
});
it('should return an error message if id doesnt exist', async () => {
const id = 99999;
const response = await request(app).get(`/api/v1/projects/${id}`);
const expectedText = `Error: Could not find project ${id}.`;
expect(response.status).toBe(404);
expect(response.text).toEqual(expectedText);
});
});
describe('GET /projects/:id/palettes', () => {
it('should return all palettes in a project based on the request.params.id', async () => {
const project = await database('projects').first()
const id = project.id
const expectedPalettes = await database('palettes').where('project_id', id)
const response = await request(app).get(`/api/v1/projects/${id}/palettes`)
expect(response.status).toBe(200)
expect(response.body.length).toBe(expectedPalettes.length)
})
it('should return an error message if id doesnt exist', async () => {
const id = 99999
const expectedText = `Error: Could not find project ${id}.`
const response = await request(app).get(`/api/v1/projects/${id}`)
expect(response.status).toBe(404)
expect(response.text).toEqual(expectedText)
} )
})
describe('GET /projects/:id/palettes/:palette_id', () => {
it('should return a single palette with the specified id', async () => {
const project = await database('projects').first()
const id = project.id
const palettes = await database('palettes').where('project_id', id).first()
const palette_id = palettes.id
const response = await request(app).get(`/api/v1/projects/${id}/palettes/${palette_id}`)
expect(response.status).toBe(200)
expect(response.body[0].color_one).toEqual(palettes.color_one)
})
it('should return an error message if id doesnt exist', async () => {
const id = 99999;
const response = await request(app).get(`/api/v1/projects/${id}/palettes`);
const expectedText = `There are no palettes for project ${id}.`;
expect(response.status).toBe(404);
expect(response.text).toEqual(expectedText);
});
})
describe('PUT /projects/:id', () => {
it('should update a project name based on request.params.id', async () => {
const project = await database('projects').first()
const id = project.id
const newProject = {
project_name: 'Justin Rules'
}
const response = await request(app).put(`/api/v1/projects/${id}`).send(newProject)
expect(response.status).toBe(200)
expect(response.body.project_name).toBe(newProject.project_name)
})
it('should return an error if the project id is invalid', async () => {
const newProject = {
id: 99999,
project_name: "Justin Rules"
}
const expectedText = `Project ${newProject.id} was not found.`
const response = await request(app).put(`/api/v1/projects/${newProject.id}`).send(newProject)
expect(response.status).toBe(404)
expect(response.text).toEqual(expectedText)
})
it('should return an error if there are missing parameters', async () => {
const project = await database('projects').first()
const projectId = project.id
const newProject = {
id: projectId
}
const expectedText = `Error: Missing project_name.`
const response = await request(app).put(`/api/v1/projects/${projectId}`).send(newProject)
expect(response.status).toBe(422)
expect(response.text).toEqual(expectedText)
})
})
describe('PUT /projects/:id/palettes/:palette_id', () => {
it('should return a palette from a project based on the request.params.id', async () => {
const project = await database('projects').first()
const id = project.id
const palettes = await database('palettes').where('project_id', id).first()
const palette_id = palettes.id.toString()
const newPalette = {
color_one: 'asdf',
color_two: 'asdf',
color_three: 'asdf',
color_four: 'asdf',
color_five: 'asdf'
}
const response = await request(app).put(`/api/v1/projects/${id}/palettes/${palette_id}`).send(newPalette)
expect(response.status).toBe(200)
expect(response.body).toEqual({palette_id: palette_id, ...newPalette})
})
it('should return an error if there is a missing parameter', async () => {
const palette = await database('palettes').first()
const projectId = palette.project_id
const paletteId = palette.id
const newPalette = {
color_one: 'asdf',
color_three: 'asdf',
color_four: 'asdf',
color_five: 'asdf'
}
const response = await request(app).put(`/api/v1/projects/${projectId}/palettes/${paletteId}`).send(newPalette)
expect(response.status).toBe(422)
expect(response.text).toEqual(`Error: Missing color_two.`)
})
it('should return an error if the id is incorrect', async () => {
const palette = await database('palettes').first()
const projectId = palette.project_id
const paletteId = 9999999999
const newPalette = {
color_one: 'asdf',
color_two: 'asdf',
color_three: 'asdf',
color_four: 'asdf',
color_five: 'asdf'
}
const response = await request(app).put(`/api/v1/projects/${projectId}/palettes/${paletteId}`).send(newPalette)
expect(response.status).toBe(404)
expect(response.text).toEqual(`Palette ${paletteId} does not exist.`)
})
})
describe('POST /projects', () => {
it('should post a new project to the db', async () => {
const newProject = { project_name: 'Pugs, man. Pugs.' }
const response = await request(app)
.post('/api/v1/projects')
.send(newProject);
const projects = await database('projects').where('id', response.body.id).select();
const project = projects[0];
expect(response.status).toBe(201);
expect(project.project_name).toEqual(newProject.project_name);
});
it('should give an error message if project_name isnt given', async () => {
const newProject = { project_name: '' }
const response = await request(app)
.post('/api/v1/projects')
.send(newProject);
const expectedMsg = `Error: Expected format: { project_name: <String> }. You're missing the project_name property.`;
expect(response.status).toBe(422);
expect(response.text).toBe(expectedMsg);
});
});
describe('POST /projects/:id/palettes', () => {
it('should add a new palette', async () => {
const project = await database('projects').first()
const project_id = project.id
const newPalette = {
project_id,
color_one: 'asdf',
color_two: 'asdf',
color_three: 'asdf',
color_four: 'asdf',
color_five: 'asdf'
}
const response = await request(app).post(`/api/v1/projects/${project_id}/palettes`).send(newPalette)
expect(response.status).toBe(201);
expect(response.body.project_id).toEqual(project_id)
})
it('should return a status 422 and an error if a required parameter is missing', async () => {
const project = await database('projects').first()
const project_id = project.id
const newPalette = {
color_one: 'a',
color_three: 'b',
color_four: 'c',
color_five: 'd'
}
const response = await request(app).post(`/api/v1/projects/${project_id}/palettes`).send(newPalette)
expect(response.status).toBe(422)
})
})
describe('DELETE /projects/:id', () => {
it('should delete a project from the database', async () => {
const projectToDelete = await database('projects').first();
const id = projectToDelete.id
const response = await request(app).delete(`/api/v1/projects/${id}`);
const expectedMsg = `Project with the id: ${id} and it's palettes have been deleted.`;
expect(response.status).toBe(200);
expect(response.text).toBe(expectedMsg);
})
it('should return a 404 status and error message if project cant be found', async () => {
const id = 9999999
const response = await request(app).delete(`/api/v1/projects/${id}`)
expect(response.status).toBe(404)
expect(response.text).toEqual(`Project with the id:${id} was not found.`)
})
})
describe('DELETE /palettes/:id', () => {
it('should delete a palette by id', async () => {
const paletteToDelete = await database('palettes').first();
const id = paletteToDelete.id
const response = await request(app).delete(`/api/v1/projects/:id/palettes/${id}`);
const expectedMsg = `Palette successfully deleted.`;
expect(response.status).toBe(200);
expect(response.text).toEqual(expectedMsg);
})
it('should return a 404 and error message if palette cannot be found', async () => {
const id = 999999
const expectedText = `Palette with id: ${id} was not found.`
const response = await request(app).delete(`/api/v1/projects/:id/palettes/${id}`)
expect(response.status).toBe(404)
expect(response.text).toEqual(expectedText)
})
})
});