|
| 1 | +/* eslint-disable max-classes-per-file */ |
| 2 | +/* eslint-disable class-methods-use-this */ |
1 | 3 | import sinon from 'sinon'; |
2 | 4 | import MovieController from '../../../src/controllers/movie'; |
3 | 5 | import Movie from '../../../src/models/movie'; |
@@ -50,4 +52,137 @@ describe('Controller: Movie', () => { |
50 | 52 | sinon.assert.calledWith(response.send, 'Error'); |
51 | 53 | }); |
52 | 54 | }); |
| 55 | + |
| 56 | + describe('getById()', () => { |
| 57 | + it('should return a one movie', async () => { |
| 58 | + const fakeId = 'fake-id'; |
| 59 | + const request = { |
| 60 | + params: { |
| 61 | + id: fakeId, |
| 62 | + }, |
| 63 | + }; |
| 64 | + const response = { |
| 65 | + send: sinon.spy(), |
| 66 | + }; |
| 67 | + |
| 68 | + Movie.find = sinon.stub(); |
| 69 | + Movie.find.withArgs({ _id: fakeId }).resolves(defaultMovie); |
| 70 | + |
| 71 | + const movieController = new MovieController(Movie); |
| 72 | + await movieController.getById(request, response); |
| 73 | + |
| 74 | + sinon.assert.calledWith(response.send, defaultMovie); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should return 400 when an error occurs', async () => { |
| 78 | + const fakeId = 'fake-id'; |
| 79 | + const request = { |
| 80 | + params: { |
| 81 | + id: fakeId, |
| 82 | + }, |
| 83 | + }; |
| 84 | + const response = { |
| 85 | + send: sinon.spy(), |
| 86 | + status: sinon.stub(), |
| 87 | + }; |
| 88 | + |
| 89 | + response.status.withArgs(400).returns(response); |
| 90 | + |
| 91 | + Movie.find = sinon.stub(); |
| 92 | + Movie.find.withArgs({ _id: fakeId }).rejects({ message: 'Error' }); |
| 93 | + |
| 94 | + const movieController = new MovieController(Movie); |
| 95 | + |
| 96 | + await movieController.getById(request, response); |
| 97 | + |
| 98 | + sinon.assert.calledWith(response.send, 'Error'); |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + describe('create()', () => { |
| 103 | + it('should save a new movie successfully', async () => { |
| 104 | + const request = { |
| 105 | + body: defaultMovie[0], |
| 106 | + ...defaultMovie, |
| 107 | + }; |
| 108 | + const response = { |
| 109 | + send: sinon.spy(), |
| 110 | + status: sinon.stub(), |
| 111 | + }; |
| 112 | + class fakeMovie { |
| 113 | + save() {} |
| 114 | + } |
| 115 | + response.status.withArgs(201).returns(response); |
| 116 | + sinon.stub(fakeMovie.prototype, 'save').withArgs().resolves(); |
| 117 | + |
| 118 | + const movieController = new MovieController(fakeMovie); |
| 119 | + |
| 120 | + await movieController.create(request, response); |
| 121 | + |
| 122 | + sinon.assert.calledWith(response.send); |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + describe('update()', () => { |
| 127 | + it('should respond with 200 when the movie has been updated', async () => { |
| 128 | + const fakeId = 'fake-id'; |
| 129 | + const updatedMovie = { |
| 130 | + _id: fakeId, |
| 131 | + name: 'Updated movie', |
| 132 | + description: 'Updated description', |
| 133 | + year: 2020, |
| 134 | + }; |
| 135 | + const request = { |
| 136 | + params: { |
| 137 | + id: fakeId, |
| 138 | + }, |
| 139 | + body: updatedMovie, |
| 140 | + }; |
| 141 | + const response = { |
| 142 | + sendStatus: sinon.spy(), |
| 143 | + }; |
| 144 | + |
| 145 | + class fakeMovie { |
| 146 | + static updateOne() {} |
| 147 | + } |
| 148 | + |
| 149 | + const updateOneStub = sinon.stub(fakeMovie, 'updateOne'); |
| 150 | + |
| 151 | + updateOneStub |
| 152 | + .withArgs({ _id: fakeId }, updatedMovie) |
| 153 | + .resolves(updatedMovie); |
| 154 | + const movieController = new MovieController(fakeMovie); |
| 155 | + |
| 156 | + await movieController.update(request, response); |
| 157 | + sinon.assert.calledWith(response.sendStatus, 200); |
| 158 | + }); |
| 159 | + }); |
| 160 | + |
| 161 | + describe('delete()', () => { |
| 162 | + it('should respond with 204 when the movie has been deleted', async () => { |
| 163 | + const fakeId = 'fake-id'; |
| 164 | + const request = { |
| 165 | + params: { |
| 166 | + id: fakeId, |
| 167 | + }, |
| 168 | + }; |
| 169 | + const response = { |
| 170 | + sendStatus: sinon.spy(), |
| 171 | + }; |
| 172 | + |
| 173 | + class fakeMovie { |
| 174 | + static deleteOne() {} |
| 175 | + } |
| 176 | + |
| 177 | + const deleteOneStub = sinon.stub(fakeMovie, 'deleteOne'); |
| 178 | + |
| 179 | + deleteOneStub.withArgs({ _id: fakeId }).resolves(); |
| 180 | + |
| 181 | + const movieController = new MovieController(fakeMovie); |
| 182 | + |
| 183 | + await movieController.delete(request, response); |
| 184 | + |
| 185 | + sinon.assert.calledWith(response.sendStatus, 204); |
| 186 | + }); |
| 187 | + }); |
53 | 188 | }); |
0 commit comments