|
| 1 | +import sinon from 'sinon'; |
| 2 | +import MovieController from '../../../src/controllers/movie'; |
| 3 | +import Movie from '../../../src/models/movie'; |
| 4 | + |
| 5 | +describe('Controller: Movie', () => { |
| 6 | + const defaultMovie = { |
| 7 | + __v: 0, |
| 8 | + _id: '5e90f0600bf2272ecf8e82d2', |
| 9 | + name: 'Star Wars A New Hope', |
| 10 | + description: 'Loren ipsun dolor', |
| 11 | + year: 1977, |
| 12 | + }; |
| 13 | + |
| 14 | + const defaultRequest = { |
| 15 | + params: {}, |
| 16 | + }; |
| 17 | + |
| 18 | + describe('get()', () => { |
| 19 | + it('should return a list of movies', async () => { |
| 20 | + const response = { |
| 21 | + send: sinon.spy(), |
| 22 | + }; |
| 23 | + |
| 24 | + Movie.find = sinon.stub(); |
| 25 | + Movie.find.withArgs({}).resolves(defaultMovie); |
| 26 | + |
| 27 | + const movieController = new MovieController(Movie); |
| 28 | + |
| 29 | + await movieController.get(defaultRequest, response); |
| 30 | + |
| 31 | + sinon.assert.calledWith(response.send, defaultMovie); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should return 400 when an error occurs', async () => { |
| 35 | + const request = {}; |
| 36 | + const response = { |
| 37 | + send: sinon.spy(), |
| 38 | + status: sinon.stub(), |
| 39 | + }; |
| 40 | + |
| 41 | + response.status.withArgs(400).returns(response); |
| 42 | + |
| 43 | + Movie.find = sinon.stub(); |
| 44 | + Movie.find.withArgs({}).rejects({ message: 'Error' }); |
| 45 | + |
| 46 | + const movieController = new MovieController(Movie); |
| 47 | + |
| 48 | + await movieController.get(request, response); |
| 49 | + |
| 50 | + sinon.assert.calledWith(response.send, 'Error'); |
| 51 | + }); |
| 52 | + }); |
| 53 | +}); |
0 commit comments