|
| 1 | +import marketplaceController from '../server/controllers/marketplaceController'; |
| 2 | +import app from '../server/server'; |
| 3 | +import mockData from '../mockData'; |
| 4 | +const request = require('supertest'); |
| 5 | +const mongoose = require('mongoose'); |
| 6 | +const mockNext = jest.fn(); // Mock nextFunction |
| 7 | +const MONGO_DB = process.env.MONGO_DB_TEST; |
| 8 | +const { state, projectToSave, user } = mockData |
| 9 | +const PORT = 8080; |
| 10 | + |
| 11 | +beforeAll(async () => { |
| 12 | + await mongoose.connect(MONGO_DB, { |
| 13 | + useNewUrlParser: true, |
| 14 | + useUnifiedTopology: true, |
| 15 | + }); |
| 16 | +}); |
| 17 | + |
| 18 | +afterAll(async () => { |
| 19 | + await mongoose.connection.close(); |
| 20 | +}); |
| 21 | + |
| 22 | +describe('Server endpoint tests', () => { |
| 23 | + it('should pass this test request', async () => { |
| 24 | + const response = await request(app).get('/test'); |
| 25 | + expect(response.status).toBe(200); |
| 26 | + expect(response.text).toBe('test request is working'); |
| 27 | + }); |
| 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 | + }); |
| 40 | + // test saveProject endpoint |
| 41 | + describe('/saveProject', () => { |
| 42 | + describe('/POST', () => { |
| 43 | + 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); |
| 48 | + return request(app) |
| 49 | + .post('/saveProject') |
| 50 | + .set('Accept', 'application/json') |
| 51 | + .send(projectToSave) |
| 52 | + .expect(200) |
| 53 | + .expect('Content-Type', /application\/json/) |
| 54 | + .then((res) => expect(res.body.name).toBe(projectToSave.name)); |
| 55 | + }); |
| 56 | + // }); |
| 57 | + }); |
| 58 | + // test getProjects endpoint |
| 59 | + describe('/getProjects', () => { |
| 60 | + describe('POST', () => { |
| 61 | + it('responds with status of 200 and json object equal to an array of user projects', () => { |
| 62 | + return request(app) |
| 63 | + .post('/getProjects') |
| 64 | + .set('Accept', 'application/json') |
| 65 | + .send({ userId: projectToSave.userId }) |
| 66 | + .expect(200) |
| 67 | + .expect('Content-Type', /json/) |
| 68 | + .then((res) => { |
| 69 | + expect(Array.isArray(res.body)).toBeTruthy; |
| 70 | + expect(res.body[0].name).toBe(state.name); |
| 71 | + }); |
| 72 | + }); |
| 73 | + }); |
| 74 | + }); |
| 75 | + // test deleteProject endpoint |
| 76 | + describe('/deleteProject', () => { |
| 77 | + describe('DELETE', () => { |
| 78 | + 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 }); |
| 80 | + const _id: String = response.body[0]._id; |
| 81 | + const userId: String = user.username; |
| 82 | + console.log(_id, userId); |
| 83 | + return request(app) |
| 84 | + .delete('/deleteProject') |
| 85 | + .set('Content-Type', 'application/json') |
| 86 | + .send({ _id, userId }) |
| 87 | + .expect(200) |
| 88 | + .then((res) => expect(res.body.name).toBe(projectToSave.name)); // @Denton might want to check more of these fields |
| 89 | + }); |
| 90 | + }); |
| 91 | + }); |
| 92 | +}); |
| 93 | +}); |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | +// describe('marketplaceController Middleware', () => { |
| 102 | +// describe('getProjects tests', () => { |
| 103 | +// it('should add the projects as an array to res.locals', () => { |
| 104 | +// const req = {}; |
| 105 | +// const res = { locals: {} }; |
| 106 | +// console.log(marketplaceController.getPublishedProjects); |
| 107 | +// console.log(typeof marketplaceController.getPublishedProjects); |
| 108 | +// marketplaceController.getPublishedProjects(req, res, mockNext); |
| 109 | +// expect(Array.isArray(res.locals.publishedProjects)).toBe(true); |
| 110 | +// expect(mockNext).toHaveBeenCalled(); |
| 111 | +// }); |
| 112 | +// }); |
| 113 | + |
| 114 | + |
| 115 | +// it('should send an error response if there is an error in the middleware', () => { |
| 116 | +// const req = { user: { isAuthenticated: false } }; |
| 117 | +// const res = mockResponse(); |
| 118 | + |
| 119 | +// marketplaceController.authenticateMiddleware(req, res, mockNext); |
| 120 | + |
| 121 | +// expect(res.status).toHaveBeenCalledWith(500); |
| 122 | +// expect(res.json).toHaveBeenCalledWith({ err: 'Error in marketplaceController.getPublishedProjects, check server logs for details' }); |
| 123 | +// }); |
| 124 | +// }); |
0 commit comments