Skip to content

Commit 35d09fc

Browse files
committed
added more tests to navbar
2 parents 6bde2d7 + c251f89 commit 35d09fc

19 files changed

+606
-205
lines changed

__tests__/projects.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ describe('Project endpoints tests', () => {
1818
server.listen(done);
1919
});
2020
afterAll((done) => {
21-
Mongoose.disconnect();
22-
server.close(done);
21+
Mongoose.disconnect().then(() => {
22+
// Close the HTTP server
23+
server.close(done);
24+
});
2325
});
2426
// test saveProject endpoint
2527
describe('/saveProject', () => {

__tests__/server.test.tsx

Lines changed: 223 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1+
/**
2+
* @jest-environment node
3+
*/
4+
5+
16
import marketplaceController from '../server/controllers/marketplaceController';
27
import app from '../server/server';
38
import mockData from '../mockData';
9+
import { profileEnd } from 'console';
10+
import { Projects } from '../server/models/reactypeModels';
411
const request = require('supertest');
512
const mongoose = require('mongoose');
613
const mockNext = jest.fn(); // Mock nextFunction
@@ -16,44 +23,53 @@ beforeAll(async () => {
1623
});
1724

1825
afterAll(async () => {
26+
27+
const result = await Projects.deleteMany({});//clear the projects collection after tests are done
28+
console.log(`${result.deletedCount} documents deleted.`);
1929
await mongoose.connection.close();
2030
});
2131

32+
2233
describe('Server endpoint tests', () => {
2334
it('should pass this test request', async () => {
2435
const response = await request(app).get('/test');
2536
expect(response.status).toBe(200);
2637
expect(response.text).toBe('test request is working');
2738
});
28-
describe('Marketplace endpoint testing', () => {
29-
it('get requests to /getMarketplaceProjects should return an array of projects', async () => {
30-
const response = await request(app).get('/getMarketplaceProjects');
31-
expect(response.status).toBe(200);
32-
expect(Array.isArray(response.body)).toBe(true);
33-
});
34-
it('the return array should be populated with project objects', async () => {
35-
const response = await request(app).get('/getMarketplaceProjects');
36-
expect(response.status).toBe(200);
37-
expect(Array.isArray(response.body)).toBe(true);
38-
});
39-
});
39+
40+
41+
// // test saveProject endpoint
42+
// describe('/login', () => {
43+
// describe('/POST', () => {
44+
// it('responds with a status of 200 and json object equal to project sent', async () => {
45+
// return request(app)
46+
// .post('/login')
47+
// .set('Cookie', [`ssid=${user.userId}`])
48+
// .set('Accept', 'application/json')
49+
// .send(projectToSave)
50+
// .expect(200)
51+
// .expect('Content-Type', /application\/json/)
52+
// .then((res) => expect(res.body.name).toBe(projectToSave.name));
53+
// });
54+
// // });
55+
// });
56+
// });
57+
4058
// test saveProject endpoint
4159
describe('/saveProject', () => {
42-
describe('/POST', () => {
60+
describe('POST', () => {
4361
it('responds with a status of 200 and json object equal to project sent', async () => {
44-
// const response = await request(app).post('/saveProject').set('Accept', 'application/json').send(projectToSave);
45-
// console.log(response);
46-
// console.log(response.body);
47-
// expect(response.status).toBe(200);
4862
return request(app)
4963
.post('/saveProject')
64+
.set('Cookie', [`ssid=${user.userId}`])
5065
.set('Accept', 'application/json')
5166
.send(projectToSave)
5267
.expect(200)
5368
.expect('Content-Type', /application\/json/)
5469
.then((res) => expect(res.body.name).toBe(projectToSave.name));
5570
});
5671
// });
72+
});
5773
});
5874
// test getProjects endpoint
5975
describe('/getProjects', () => {
@@ -62,6 +78,7 @@ describe('Server endpoint tests', () => {
6278
return request(app)
6379
.post('/getProjects')
6480
.set('Accept', 'application/json')
81+
.set('Cookie', [`ssid=${user.userId}`])
6582
.send({ userId: projectToSave.userId })
6683
.expect(200)
6784
.expect('Content-Type', /json/)
@@ -76,20 +93,204 @@ describe('Server endpoint tests', () => {
7693
describe('/deleteProject', () => {
7794
describe('DELETE', () => {
7895
it('responds with status of 200 and json object equal to deleted project', async () => {
79-
const response: Response = await request(app).post('/getProjects').set('Accept', 'application/json').send({ userId: projectToSave.userId });
96+
const response: Response = await request(app).post('/getProjects').set('Accept', 'application/json').set('Cookie', [`ssid=${user.userId}`]).send({ userId: projectToSave.userId });
8097
const _id: String = response.body[0]._id;
81-
const userId: String = user.username;
82-
console.log(_id, userId);
98+
const userId: String = user.userId;
8399
return request(app)
84100
.delete('/deleteProject')
101+
.set('Cookie', [`ssid=${user.userId}`])
85102
.set('Content-Type', 'application/json')
86103
.send({ _id, userId })
87104
.expect(200)
88-
.then((res) => expect(res.body.name).toBe(projectToSave.name)); // @Denton might want to check more of these fields
105+
.then((res) => expect(res.body._id).toBe(_id));
89106
});
90107
});
91108
});
92-
});
109+
110+
//test publishProject endpoint
111+
describe('/publishProject', () => {
112+
describe('POST', () => {
113+
it('responds with status of 200 and json object equal to published project', async () => {
114+
115+
const projObj = await request(app)
116+
.post('/saveProject')
117+
.set('Cookie', [`ssid=${user.userId}`])
118+
.set('Accept', 'application/json')
119+
.send(projectToSave)
120+
const _id: String = projObj.body._id;
121+
const project: String = projObj.body.project;
122+
const comments: String = projObj.body.comments;
123+
const username: String = projObj.body.username;
124+
const name: String = projObj.body.name;
125+
const userId: String = user.userId;
126+
return request(app)
127+
.post('/publishProject')
128+
.set('Content-Type', 'application/json')
129+
.set('Cookie', [`ssid=${user.userId}`])
130+
.send({ _id, project, comments, userId, username, name })//_id, project, comments, userId, username, name
131+
.expect(200)
132+
.then((res) => {
133+
expect(res.body._id).toBe(_id)
134+
expect(res.body.published).toBe(true);
135+
});
136+
});
137+
it('responds with status of 500 and error if userId and cookie ssid do not match', async () => {
138+
const projObj: Response = await request(app).post('/getProjects').set('Accept', 'application/json').set('Cookie', [`ssid=${user.userId}`]).send({ userId: projectToSave.userId });
139+
const _id: String = projObj.body[0]._id;
140+
const project: String = projObj.body[0].project;
141+
const comments: String = projObj.body[0].comments;
142+
const username: String = projObj.body[0].username;
143+
const name: String = projObj.body[0].name;
144+
const userId: String = "ERROR";
145+
return request(app)
146+
.post('/publishProject')
147+
.set('Content-Type', 'application/json')
148+
.set('Cookie', [`ssid=${user.userId}`])
149+
.send({ _id, project, comments, userId, username, name })//_id, project, comments, userId, username, name
150+
.expect(500)
151+
.then((res) => {
152+
expect(res.body.err).not.toBeNull()
153+
});
154+
});
155+
it('responds with status of 500 and error if _id was not a valid mongo ObjectId', async () => {
156+
const projObj: Response = await request(app).post('/getProjects').set('Accept', 'application/json').set('Cookie', [`ssid=${user.userId}`]).send({ userId: projectToSave.userId });
157+
const _id: String = 'ERROR';
158+
const project: String = projObj.body[0].project;
159+
const comments: String = projObj.body[0].comments;
160+
const username: String = user.username;
161+
const name: String = projObj.body[0].name;
162+
const userId: String = user.userId;
163+
164+
return request(app)
165+
.post('/publishProject')
166+
.set('Content-Type', 'application/json')
167+
.set('Cookie', [`ssid=${user.userId}`])
168+
.send({ _id, project, comments, userId, username, name })//_id, project, comments, userId, username, name
169+
.expect(200)
170+
.then((res) => {
171+
expect(res.body._id).not.toEqual(_id)
172+
});
173+
});
174+
});
175+
});
176+
177+
//test getMarketplaceProjects endpoint
178+
describe('/getMarketplaceProjects', () => {//most recent project should be the one from publishProject
179+
180+
describe('GET', () => {
181+
it('responds with status of 200 and json object equal to unpublished project', async () => {
182+
return request(app)
183+
.get('/getMarketplaceProjects')
184+
.set('Content-Type', 'application/json')
185+
.expect(200)
186+
.then((res) => {
187+
expect(Array.isArray(res.body)).toBe(true);
188+
expect(res.body[0]._id).toBeTruthy;
189+
});
190+
});
191+
});
192+
});
193+
194+
//test cloneProject endpoint
195+
describe('/cloneProject/:docId', () => {
196+
describe('GET', () => {
197+
it('responds with status of 200 and json object equal to cloned project', async () => {
198+
199+
const projObj = await request(app)
200+
.get('/getMarketplaceProjects')
201+
.set('Content-Type', 'application/json')
202+
203+
return request(app)
204+
.get(`/cloneProject/${projObj.body[0]._id}`)
205+
.set('Cookie', [`ssid=${user.userId}`]) // Set the cookie
206+
.query({ username: user.username })
207+
.expect(200)
208+
.then((res) => {
209+
expect(res.body.forked).toBeTruthy;
210+
expect(res.body.username).toBe(user.username);
211+
});
212+
});
213+
it('responds with status of 500 and error', async () => {
214+
215+
const projObj = await request(app)
216+
.get('/getMarketplaceProjects')
217+
.set('Content-Type', 'application/json')
218+
219+
return request(app)
220+
.get(`/cloneProject/${projObj.body[0]._id}`)
221+
.set('Cookie', [`ssid=${user.userId}`]) // Set the cookie
222+
.query({ username: [] })
223+
.expect(500)
224+
.then((res) => {
225+
expect(res.body.err).not.toBeNull()
226+
});
227+
});
228+
});
229+
});
230+
231+
//test unpublishProject endpoint
232+
describe('/unpublishProject', () => {
233+
describe('PATCH', () => {
234+
it('responds with status of 200 and json object equal to unpublished project', async () => {
235+
const response: Response = await request(app).post('/getProjects').set('Accept', 'application/json').set('Cookie', [`ssid=${user.userId}`]).send({ userId: projectToSave.userId }); //most recent project should be the one from publishProject
236+
const _id: String = response.body[0]._id;
237+
const userId: String = user.userId;
238+
return request(app)
239+
.patch('/unpublishProject')
240+
.set('Content-Type', 'application/json')
241+
.set('Cookie', [`ssid=${user.userId}`])
242+
.send({ _id, userId })
243+
.expect(200)
244+
.then((res) => {
245+
expect(res.body._id).toBe(_id)
246+
expect(res.body.published).toBe(false);
247+
});
248+
});
249+
it('responds with status of 500 and error if userId and cookie ssid do not match', async () => {
250+
const projObj: Response = await request(app).post('/getProjects').set('Accept', 'application/json').set('Cookie', [`ssid=${user.userId}`]).send({ userId: projectToSave.userId });
251+
const _id: String = projObj.body[0]._id;
252+
const project: String = projObj.body[0].project;
253+
const comments: String = projObj.body[0].comments;
254+
const username: String = projObj.body[0].username;
255+
const name: String = projObj.body[0].name;
256+
let userId: String = user.userId;
257+
await request(app)//publishing a project first
258+
.post('/publishProject')
259+
.set('Content-Type', 'application/json')
260+
.set('Cookie', [`ssid=${user.userId}`])
261+
.send({ _id, project, comments, userId, username, name })
262+
263+
264+
userId = "ERROR";
265+
return request(app)
266+
.patch('/unpublishProject')
267+
.set('Content-Type', 'application/json')
268+
.set('Cookie', [`ssid=${user.userId}`])
269+
.send({ _id, userId })
270+
.expect(500)
271+
.then((res) => {
272+
expect(res.body.err).not.toBeNull()
273+
});
274+
});
275+
it('responds with status of 500 and error if _id was not a string', async () => {
276+
const userId: String = user.userId;
277+
278+
return request(app)
279+
.patch('/unpublishProject')
280+
.set('Content-Type', 'application/json')
281+
.set('Cookie', [`ssid=${user.userId}`])
282+
.send({userId })
283+
.expect(500)
284+
.then((res) => {
285+
expect(res.body.err).not.toBeNull()
286+
});
287+
});
288+
});
289+
});
290+
291+
292+
293+
93294
});
94295

95296

0 commit comments

Comments
 (0)