|
3 | 3 | */
|
4 | 4 | import { Request, Response } from 'jest-express';
|
5 | 5 |
|
6 |
| -import Project from '../../../models/project'; |
| 6 | +import Project, { |
| 7 | + createMock, |
| 8 | + createInstanceMock |
| 9 | +} from '../../../models/project'; |
7 | 10 | import User from '../../../models/user';
|
8 | 11 | import deleteProject from '../../project.controller/deleteProject';
|
9 | 12 | import { deleteObjectsFromS3 } from '../../aws.controller';
|
10 | 13 |
|
11 | 14 | jest.mock('../../../models/project');
|
12 | 15 | jest.mock('../../aws.controller');
|
13 | 16 |
|
14 |
| -// TODO: incomplete test, 500 response status needs to be added |
15 |
| - |
16 | 17 | describe('project.controller', () => {
|
17 |
| - let request; |
18 |
| - let response; |
| 18 | + describe('deleteProject()', () => { |
| 19 | + let ProjectMock; |
| 20 | + let ProjectInstanceMock; |
19 | 21 |
|
20 |
| - beforeEach(() => { |
21 |
| - request = new Request(); |
22 |
| - response = new Response(); |
23 |
| - Project.findById = jest.fn(); |
24 |
| - }); |
| 22 | + beforeEach(() => { |
| 23 | + ProjectMock = createMock(); |
| 24 | + ProjectInstanceMock = createInstanceMock(); |
| 25 | + }); |
25 | 26 |
|
26 |
| - afterEach(() => { |
27 |
| - request.resetMocked(); |
28 |
| - response.resetMocked(); |
29 |
| - }); |
| 27 | + afterEach(() => { |
| 28 | + ProjectMock.restore(); |
| 29 | + ProjectInstanceMock.restore(); |
| 30 | + }); |
30 | 31 |
|
31 |
| - it('returns 403 if project is not owned by authenticated user', async () => { |
32 |
| - const user = new User(); |
33 |
| - const project = new Project(); |
34 |
| - project.user = user; |
| 32 | + it('returns 403 if project is not owned by authenticated user', (done) => { |
| 33 | + const user = new User(); |
| 34 | + const project = new Project(); |
| 35 | + project.user = user; |
35 | 36 |
|
36 |
| - request.setParams({ project_id: project._id }); |
37 |
| - request.user = { _id: 'abc123' }; |
| 37 | + const request = new Request(); |
| 38 | + request.setParams({ project_id: project._id }); |
| 39 | + request.user = { _id: 'abc123' }; |
38 | 40 |
|
39 |
| - Project.findById.mockResolvedValue(project); |
| 41 | + const response = new Response(); |
40 | 42 |
|
41 |
| - await deleteProject(request, response); |
| 43 | + ProjectMock.expects('findById').resolves(project); |
42 | 44 |
|
43 |
| - expect(response.status).toHaveBeenCalledWith(403); |
44 |
| - expect(response.json).toHaveBeenCalledWith({ |
45 |
| - message: 'Authenticated user does not match owner of project' |
| 45 | + const promise = deleteProject(request, response); |
| 46 | + |
| 47 | + function expectations() { |
| 48 | + expect(response.status).toHaveBeenCalledWith(403); |
| 49 | + expect(response.json).toHaveBeenCalledWith({ |
| 50 | + message: 'Authenticated user does not match owner of project' |
| 51 | + }); |
| 52 | + |
| 53 | + done(); |
| 54 | + } |
| 55 | + |
| 56 | + promise.then(expectations, expectations).catch(expectations); |
46 | 57 | });
|
47 |
| - }); |
48 | 58 |
|
49 |
| - it('returns 404 if project does not exist', async () => { |
50 |
| - request.setParams({ project_id: 'random_id' }); |
51 |
| - request.user = { _id: 'abc123' }; |
| 59 | + it('returns 404 if project does not exist', (done) => { |
| 60 | + const user = new User(); |
| 61 | + const project = new Project(); |
| 62 | + project.user = user; |
| 63 | + |
| 64 | + const request = new Request(); |
| 65 | + request.setParams({ project_id: project._id }); |
| 66 | + request.user = { _id: 'abc123' }; |
52 | 67 |
|
53 |
| - Project.findById.mockResolvedValue(null); |
| 68 | + const response = new Response(); |
54 | 69 |
|
55 |
| - await deleteProject(request, response); |
| 70 | + ProjectMock.expects('findById').resolves(null); |
56 | 71 |
|
57 |
| - expect(response.status).toHaveBeenCalledWith(404); |
58 |
| - expect(response.json).toHaveBeenCalledWith({ |
59 |
| - message: 'Project with that id does not exist' |
| 72 | + const promise = deleteProject(request, response); |
| 73 | + |
| 74 | + function expectations() { |
| 75 | + expect(response.status).toHaveBeenCalledWith(404); |
| 76 | + expect(response.json).toHaveBeenCalledWith({ |
| 77 | + message: 'Project with that id does not exist' |
| 78 | + }); |
| 79 | + |
| 80 | + done(); |
| 81 | + } |
| 82 | + |
| 83 | + promise.then(expectations, expectations).catch(expectations); |
60 | 84 | });
|
61 |
| - }); |
62 | 85 |
|
63 |
| - it('delete project and dependent files from S3', async () => { |
64 |
| - const user = new User(); |
65 |
| - const project = new Project(); |
66 |
| - project.user = user; |
67 |
| - project.remove = jest.fn().mockResolvedValue(); |
| 86 | + it('deletes project and dependent files from S3 ', (done) => { |
| 87 | + const user = new User(); |
| 88 | + const project = new Project(); |
| 89 | + project.user = user; |
| 90 | + |
| 91 | + const request = new Request(); |
| 92 | + request.setParams({ project_id: project._id }); |
| 93 | + request.user = { _id: user._id }; |
68 | 94 |
|
69 |
| - request.setParams({ project_id: project._id }); |
70 |
| - request.user = { _id: user._id }; |
| 95 | + const response = new Response(); |
71 | 96 |
|
72 |
| - Project.findById.mockResolvedValue(project); |
73 |
| - deleteObjectsFromS3.mockResolvedValue(); |
| 97 | + ProjectMock.expects('findById').resolves(project); |
74 | 98 |
|
75 |
| - await deleteProject(request, response); |
| 99 | + ProjectInstanceMock.expects('remove').yields(); |
76 | 100 |
|
77 |
| - expect(deleteObjectsFromS3).toHaveBeenCalled(); |
78 |
| - expect(project.remove).toHaveBeenCalled(); |
79 |
| - expect(response.status).toHaveBeenCalledWith(200); |
| 101 | + const promise = deleteProject(request, response); |
| 102 | + |
| 103 | + function expectations() { |
| 104 | + expect(response.status).toHaveBeenCalledWith(200); |
| 105 | + expect(response.json).not.toHaveBeenCalled(); |
| 106 | + expect(deleteObjectsFromS3).toHaveBeenCalled(); |
| 107 | + |
| 108 | + done(); |
| 109 | + } |
| 110 | + |
| 111 | + promise.then(expectations, expectations).catch(expectations); |
| 112 | + }); |
80 | 113 | });
|
81 | 114 | });
|
0 commit comments