|
| 1 | +const CheckProjectMembershipByListUseCase = require('../../../api/src/application/use-cases/list/CheckProjectMembershipByListUseCase'); |
| 2 | +const ListDto = require('../../../api/src/application/dtos/list.dto'); |
| 3 | +const { createList, createUser } = require('../../fake-data/fake-entities'); |
| 4 | + |
| 5 | +describe('CheckProjectMembershipByListUseCase', () => { |
| 6 | + let userId; |
| 7 | + let listId; |
| 8 | + let dbResponse; |
| 9 | + let mockListRepository; |
| 10 | + let checkProjectMembershipByListUseCase; |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + userId = createUser().id; |
| 14 | + listId = createList().id; |
| 15 | + dbResponse = createList(); |
| 16 | + |
| 17 | + mockListRepository = { |
| 18 | + checkProjectMembershipByList: jest.fn().mockResolvedValue(dbResponse), |
| 19 | + }; |
| 20 | + |
| 21 | + checkProjectMembershipByListUseCase = |
| 22 | + new CheckProjectMembershipByListUseCase({ |
| 23 | + listRepository: mockListRepository, |
| 24 | + }); |
| 25 | + }); |
| 26 | + |
| 27 | + test('It should return a list with its project', async () => { |
| 28 | + const result = await checkProjectMembershipByListUseCase.execute( |
| 29 | + userId, |
| 30 | + listId, |
| 31 | + ); |
| 32 | + |
| 33 | + expect( |
| 34 | + mockListRepository.checkProjectMembershipByList, |
| 35 | + ).toHaveBeenCalledTimes(1); |
| 36 | + expect(result).toMatchObject(new ListDto(dbResponse)); |
| 37 | + }); |
| 38 | + |
| 39 | + test('It should return an error because the userId was not provided', async () => { |
| 40 | + await expect( |
| 41 | + checkProjectMembershipByListUseCase.execute(null, listId), |
| 42 | + ).rejects.toThrow(/was not provided/); |
| 43 | + expect( |
| 44 | + mockListRepository.checkProjectMembershipByList, |
| 45 | + ).not.toHaveBeenCalled(); |
| 46 | + }); |
| 47 | + |
| 48 | + test('It should return an error because the listId was not provided', async () => { |
| 49 | + await expect( |
| 50 | + checkProjectMembershipByListUseCase.execute(userId, null), |
| 51 | + ).rejects.toThrow(/was not provided/); |
| 52 | + expect( |
| 53 | + mockListRepository.checkProjectMembershipByList, |
| 54 | + ).not.toHaveBeenCalled(); |
| 55 | + }); |
| 56 | + |
| 57 | + test('It should return an empty object because the operation db did not find anything', async () => { |
| 58 | + mockListRepository.checkProjectMembershipByList.mockResolvedValue(0); |
| 59 | + |
| 60 | + const result = await checkProjectMembershipByListUseCase.execute( |
| 61 | + userId, |
| 62 | + listId, |
| 63 | + ); |
| 64 | + expect( |
| 65 | + mockListRepository.checkProjectMembershipByList, |
| 66 | + ).toHaveBeenCalledTimes(1); |
| 67 | + expect(result).toEqual({}); |
| 68 | + }); |
| 69 | + |
| 70 | + afterEach(() => { |
| 71 | + jest.clearAllMocks(); |
| 72 | + }); |
| 73 | +}); |
0 commit comments