|
| 1 | +import { User } from "../src/User"; |
| 2 | +import { UserFullName } from "../src/UserFullName"; |
| 3 | +import { UserFullNameByIdFinder } from "../src/UserFullNameByIdFinder"; |
| 4 | +import { UserId } from "../src/UserId"; |
| 5 | +import { UserLastName } from "../src/UserLastName"; |
| 6 | +import { UserName } from "../src/UserName"; |
| 7 | +import { UserRepository } from "../src/UserRepository"; |
| 8 | + |
| 9 | +describe("UserFullNameByIdFinder", () => { |
| 10 | + const validUuid = "550e8400-e29b-41d4-a716-446655440000"; |
| 11 | + |
| 12 | + const mockUserRepository: UserRepository = { |
| 13 | + search: jest.fn((id: string) => { |
| 14 | + if (id === validUuid) { |
| 15 | + return new User( |
| 16 | + new UserId(validUuid), |
| 17 | + new UserFullName(new UserName("Javier"), new UserLastName("Ferrer")), |
| 18 | + ); |
| 19 | + } |
| 20 | + |
| 21 | + return null; |
| 22 | + }), |
| 23 | + }; |
| 24 | + |
| 25 | + it("throw an error searching a non existing user", () => { |
| 26 | + const nonExistingUuid = "550e8400-e29b-41d4-a716-446655440001"; |
| 27 | + |
| 28 | + const finder = new UserFullNameByIdFinder(mockUserRepository); |
| 29 | + |
| 30 | + expect(() => { |
| 31 | + finder.find(nonExistingUuid); |
| 32 | + }).toThrow(`The user ${nonExistingUuid} does not exist`); |
| 33 | + |
| 34 | + expect(mockUserRepository.search).toHaveBeenCalledWith(nonExistingUuid); |
| 35 | + }); |
| 36 | + |
| 37 | + it("return an existing user full name", () => { |
| 38 | + const finder = new UserFullNameByIdFinder(mockUserRepository); |
| 39 | + |
| 40 | + const result = finder.find(validUuid); |
| 41 | + |
| 42 | + expect(result).toBe("Javier Ferrer"); |
| 43 | + expect(mockUserRepository.search).toHaveBeenCalledWith(validUuid); |
| 44 | + }); |
| 45 | +}); |
0 commit comments