|
| 1 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | +import { AddressesService } from './addresses.service'; |
| 3 | +import { getRepositoryToken } from '@nestjs/typeorm'; |
| 4 | +import { Address } from './entities/address.entity'; |
| 5 | +import { Repository } from 'typeorm'; |
| 6 | +import { HttpException, HttpStatus } from '@nestjs/common'; |
| 7 | +import { CreateAddressDto } from './dto/create-address.dto'; |
| 8 | +import { UpdateAddressDto } from './dto/update-address.dto'; |
| 9 | + |
| 10 | +const mockAddressRepository = () => ({ |
| 11 | + findOne: jest.fn(), |
| 12 | + save: jest.fn(), |
| 13 | + update: jest.fn(), |
| 14 | + delete: jest.fn(), |
| 15 | +}); |
| 16 | + |
| 17 | +type MockRepository<T = any> = Partial<Record<keyof Repository<T>, jest.Mock>>; |
| 18 | + |
| 19 | +describe('AddressesService', () => { |
| 20 | + let service: AddressesService; |
| 21 | + let addressRepository: MockRepository<Address>; |
| 22 | + |
| 23 | + beforeEach(async () => { |
| 24 | + const module: TestingModule = await Test.createTestingModule({ |
| 25 | + providers: [ |
| 26 | + AddressesService, |
| 27 | + { |
| 28 | + provide: getRepositoryToken(Address), |
| 29 | + useFactory: mockAddressRepository, |
| 30 | + }, |
| 31 | + ], |
| 32 | + }).compile(); |
| 33 | + |
| 34 | + service = module.get<AddressesService>(AddressesService); |
| 35 | + addressRepository = module.get<MockRepository<Address>>( |
| 36 | + getRepositoryToken(Address) |
| 37 | + ); |
| 38 | + }); |
| 39 | + |
| 40 | + describe('create', () => { |
| 41 | + it('should create a new address', async () => { |
| 42 | + const createAddressDto: CreateAddressDto = { |
| 43 | + street: 'ul. Marszałkowska 1', |
| 44 | + city: 'Warszawa', |
| 45 | + administrativeArea: 'Mazowieckie', |
| 46 | + postalCode: '00-001', |
| 47 | + country: 'Poland', |
| 48 | + }; |
| 49 | + |
| 50 | + const savedAddress = { id: 1, ...createAddressDto }; |
| 51 | + addressRepository.findOne.mockResolvedValue(null); |
| 52 | + addressRepository.save.mockResolvedValue(savedAddress); |
| 53 | + |
| 54 | + const result = await service.create(createAddressDto); |
| 55 | + expect(result).toEqual(savedAddress); |
| 56 | + expect(addressRepository.findOne).toHaveBeenCalledWith({ |
| 57 | + where: { |
| 58 | + street: createAddressDto.street, |
| 59 | + city: createAddressDto.city, |
| 60 | + administrativeArea: createAddressDto.administrativeArea, |
| 61 | + postalCode: createAddressDto.postalCode, |
| 62 | + country: createAddressDto.country, |
| 63 | + }, |
| 64 | + }); |
| 65 | + expect(addressRepository.save).toHaveBeenCalledWith(createAddressDto); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should throw an error if address already exists', async () => { |
| 69 | + const createAddressDto: CreateAddressDto = { |
| 70 | + street: 'ul. Marszałkowska 1', |
| 71 | + city: 'Warszawa', |
| 72 | + administrativeArea: 'Mazowieckie', |
| 73 | + postalCode: '00-001', |
| 74 | + country: 'Poland', |
| 75 | + }; |
| 76 | + |
| 77 | + addressRepository.findOne.mockResolvedValue(createAddressDto); |
| 78 | + |
| 79 | + await expect(service.create(createAddressDto)).rejects.toThrow( |
| 80 | + new HttpException( |
| 81 | + { |
| 82 | + status: HttpStatus.CONFLICT, |
| 83 | + error: 'Address already exists', |
| 84 | + }, |
| 85 | + HttpStatus.CONFLICT |
| 86 | + ) |
| 87 | + ); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + describe('findOne', () => { |
| 92 | + it('should return an address if found', async () => { |
| 93 | + const address = { |
| 94 | + id: 1, |
| 95 | + street: 'ul. Marszałkowska 1', |
| 96 | + city: 'Warszawa', |
| 97 | + administrativeArea: 'Mazowieckie', |
| 98 | + postalCode: '00-001', |
| 99 | + country: 'Poland', |
| 100 | + }; |
| 101 | + addressRepository.findOne.mockResolvedValue(address); |
| 102 | + |
| 103 | + const result = await service.findOne(1); |
| 104 | + expect(result).toEqual(address); |
| 105 | + expect(addressRepository.findOne).toHaveBeenCalledWith({ |
| 106 | + where: { id: 1 }, |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should throw a NotFoundException if address is not found', async () => { |
| 111 | + addressRepository.findOne.mockResolvedValue(null); |
| 112 | + |
| 113 | + await expect(service.findOne(1)).rejects.toThrow( |
| 114 | + new HttpException( |
| 115 | + { |
| 116 | + status: HttpStatus.NOT_FOUND, |
| 117 | + error: `Address with id 1 not found`, |
| 118 | + }, |
| 119 | + HttpStatus.NOT_FOUND |
| 120 | + ) |
| 121 | + ); |
| 122 | + }); |
| 123 | + }); |
| 124 | + |
| 125 | + describe('update', () => { |
| 126 | + it('should update an address if found', async () => { |
| 127 | + const updateAddressDto: UpdateAddressDto = { |
| 128 | + street: 'ul. Marszałkowska 2', |
| 129 | + city: 'Warszawa', |
| 130 | + administrativeArea: 'Mazowieckie', |
| 131 | + postalCode: '00-002', |
| 132 | + country: 'Poland', |
| 133 | + }; |
| 134 | + |
| 135 | + const address = { id: 1, ...updateAddressDto }; |
| 136 | + addressRepository.findOne.mockResolvedValue(address); |
| 137 | + addressRepository.update.mockResolvedValue(address); |
| 138 | + |
| 139 | + const result = await service.update(1, updateAddressDto); |
| 140 | + expect(result).toEqual(address); |
| 141 | + expect(addressRepository.findOne).toHaveBeenCalledWith({ |
| 142 | + where: { id: 1 }, |
| 143 | + }); |
| 144 | + expect(addressRepository.update).toHaveBeenCalledWith( |
| 145 | + 1, |
| 146 | + updateAddressDto |
| 147 | + ); |
| 148 | + }); |
| 149 | + |
| 150 | + it('should throw a NotFoundException if address is not found', async () => { |
| 151 | + addressRepository.findOne.mockResolvedValue(null); |
| 152 | + |
| 153 | + await expect( |
| 154 | + service.update(1, {} as UpdateAddressDto) |
| 155 | + ).rejects.toThrow( |
| 156 | + new HttpException( |
| 157 | + { |
| 158 | + status: HttpStatus.NOT_FOUND, |
| 159 | + error: `Address with id 1 not found`, |
| 160 | + }, |
| 161 | + HttpStatus.NOT_FOUND |
| 162 | + ) |
| 163 | + ); |
| 164 | + }); |
| 165 | + }); |
| 166 | + |
| 167 | + describe('remove', () => { |
| 168 | + it('should remove an address if found', async () => { |
| 169 | + const address = { |
| 170 | + id: 1, |
| 171 | + street: 'ul. Marszałkowska 1', |
| 172 | + city: 'Warszawa', |
| 173 | + administrativeArea: 'Mazowieckie', |
| 174 | + postalCode: '00-001', |
| 175 | + country: 'Poland', |
| 176 | + }; |
| 177 | + addressRepository.findOne.mockResolvedValue(address); |
| 178 | + addressRepository.delete.mockResolvedValue(address); |
| 179 | + |
| 180 | + await service.remove(1); |
| 181 | + expect(addressRepository.findOne).toHaveBeenCalledWith({ |
| 182 | + where: { id: 1 }, |
| 183 | + }); |
| 184 | + expect(addressRepository.delete).toHaveBeenCalledWith(1); |
| 185 | + }); |
| 186 | + |
| 187 | + it('should throw a NotFoundException if address is not found', async () => { |
| 188 | + const id = 1; |
| 189 | + jest.spyOn(addressRepository, 'findOne').mockResolvedValue(null); |
| 190 | + |
| 191 | + await expect(service.remove(id)).rejects.toThrow( |
| 192 | + new HttpException( |
| 193 | + { |
| 194 | + status: HttpStatus.NOT_FOUND, |
| 195 | + error: `Address with id ${id} not found`, |
| 196 | + }, |
| 197 | + HttpStatus.NOT_FOUND |
| 198 | + ) |
| 199 | + ); |
| 200 | + }); |
| 201 | + }); |
| 202 | +}); |
0 commit comments