|
| 1 | +import { DataSource, Repository } from 'typeorm'; |
| 2 | +import { Product } from '../../models/Product'; |
| 3 | +import logger from '../../utils/logger'; |
| 4 | + |
| 5 | +// Mock dependencies before importing the class that uses them |
| 6 | +jest.mock('../../utils/logger'); |
| 7 | +jest.mock('tsyringe', () => ({ |
| 8 | + injectable: () => jest.fn(), |
| 9 | + inject: () => jest.fn(), |
| 10 | + container: { |
| 11 | + register: jest.fn(), |
| 12 | + }, |
| 13 | +})); |
| 14 | + |
| 15 | +// Import after mocking dependencies |
| 16 | +import { ProductRepository } from '../../repositories/ProductRepository'; |
| 17 | + |
| 18 | +describe('ProductRepository', () => { |
| 19 | + let productRepository: ProductRepository; |
| 20 | + let mockDataSource: jest.Mocked<DataSource>; |
| 21 | + let mockRepository: jest.Mocked<Repository<Product>>; |
| 22 | + |
| 23 | + const testProduct: Product = { |
| 24 | + id: 1, |
| 25 | + name: 'Test Product', |
| 26 | + description: 'A sample product for testing', |
| 27 | + price: 19.99, |
| 28 | + available_quantity: 100, |
| 29 | + sells: [], |
| 30 | + }; |
| 31 | + |
| 32 | + beforeEach(() => { |
| 33 | + // Reset all mocks |
| 34 | + jest.clearAllMocks(); |
| 35 | + |
| 36 | + // Create mock repository |
| 37 | + mockRepository = { |
| 38 | + findOneBy: jest.fn(), |
| 39 | + save: jest.fn(), |
| 40 | + update: jest.fn(), |
| 41 | + delete: jest.fn(), |
| 42 | + } as unknown as jest.Mocked<Repository<Product>>; |
| 43 | + |
| 44 | + // Create mock data source |
| 45 | + mockDataSource = { |
| 46 | + getRepository: jest.fn().mockReturnValue(mockRepository), |
| 47 | + } as unknown as jest.Mocked<DataSource>; |
| 48 | + |
| 49 | + // Create ProductRepository instance |
| 50 | + productRepository = new ProductRepository(mockDataSource); |
| 51 | + }); |
| 52 | + |
| 53 | + describe('constructor', () => { |
| 54 | + it('should initialize repository correctly', () => { |
| 55 | + expect(mockDataSource.getRepository).toHaveBeenCalledWith(Product); |
| 56 | + expect(logger.info).toHaveBeenCalledWith( |
| 57 | + 'β
[ProductRepository] Initialized ProductRepository', |
| 58 | + ); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + describe('findProductByName', () => { |
| 63 | + it('should find product by name successfully', async () => { |
| 64 | + // Arrange |
| 65 | + mockRepository.findOneBy.mockResolvedValue(testProduct); |
| 66 | + const productName = 'Test Product'; |
| 67 | + |
| 68 | + // Act |
| 69 | + const result = await productRepository.findProductByName(productName); |
| 70 | + |
| 71 | + // Assert |
| 72 | + expect(result).toEqual(testProduct); |
| 73 | + expect(mockRepository.findOneBy).toHaveBeenCalledWith({ |
| 74 | + name: productName, |
| 75 | + }); |
| 76 | + expect(logger.info).toHaveBeenCalledWith( |
| 77 | + expect.stringContaining( |
| 78 | + `Searching for product with name: ${productName}`, |
| 79 | + ), |
| 80 | + ); |
| 81 | + expect(logger.info).toHaveBeenCalledWith( |
| 82 | + expect.stringContaining(`Found product with name: ${productName}`), |
| 83 | + ); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should return null when product is not found', async () => { |
| 87 | + // Arrange |
| 88 | + mockRepository.findOneBy.mockResolvedValue(null); |
| 89 | + const productName = 'Nonexistent Product'; |
| 90 | + |
| 91 | + // Act |
| 92 | + const result = await productRepository.findProductByName(productName); |
| 93 | + |
| 94 | + // Assert |
| 95 | + expect(result).toBeNull(); |
| 96 | + expect(mockRepository.findOneBy).toHaveBeenCalledWith({ |
| 97 | + name: productName, |
| 98 | + }); |
| 99 | + expect(logger.warn).toHaveBeenCalledWith( |
| 100 | + expect.stringContaining(`No product found with name: ${productName}`), |
| 101 | + ); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should throw error when database query fails', async () => { |
| 105 | + // Arrange |
| 106 | + const errorMessage = 'Database connection failed'; |
| 107 | + mockRepository.findOneBy.mockRejectedValue(new Error(errorMessage)); |
| 108 | + const productName = 'Test Product'; |
| 109 | + |
| 110 | + // Act & Assert |
| 111 | + await expect( |
| 112 | + productRepository.findProductByName(productName), |
| 113 | + ).rejects.toThrow(`Error finding product by name: ${errorMessage}`); |
| 114 | + expect(logger.error).toHaveBeenCalledWith( |
| 115 | + 'β [ProductRepository] Error finding product by name:', |
| 116 | + expect.objectContaining({ error: expect.any(Error) }), |
| 117 | + ); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should handle non-Error objects in catch block', async () => { |
| 121 | + // Arrange |
| 122 | + mockRepository.findOneBy.mockRejectedValue('Some non-error object'); |
| 123 | + const productName = 'Test Product'; |
| 124 | + |
| 125 | + // Act & Assert |
| 126 | + await expect( |
| 127 | + productRepository.findProductByName(productName), |
| 128 | + ).rejects.toThrow('Error finding product by name: Unknown error'); |
| 129 | + expect(logger.error).toHaveBeenCalledWith( |
| 130 | + 'β [ProductRepository] Error finding product by name:', |
| 131 | + expect.objectContaining({ error: 'Some non-error object' }), |
| 132 | + ); |
| 133 | + }); |
| 134 | + }); |
| 135 | + |
| 136 | + describe('inherited methods', () => { |
| 137 | + it('should inherit CRUD operations from GenericRepository', () => { |
| 138 | + // Verify that ProductRepository has inherited methods |
| 139 | + expect(productRepository.createEntity).toBeDefined(); |
| 140 | + expect(productRepository.findEntityById).toBeDefined(); |
| 141 | + expect(productRepository.updateEntity).toBeDefined(); |
| 142 | + expect(productRepository.deleteEntity).toBeDefined(); |
| 143 | + }); |
| 144 | + }); |
| 145 | + |
| 146 | + describe('error handling', () => { |
| 147 | + it('should handle database connection errors', async () => { |
| 148 | + // Arrange |
| 149 | + mockRepository.findOneBy.mockRejectedValue( |
| 150 | + new Error('Connection refused'), |
| 151 | + ); |
| 152 | + const productName = 'Test Product'; |
| 153 | + |
| 154 | + // Act & Assert |
| 155 | + await expect( |
| 156 | + productRepository.findProductByName(productName), |
| 157 | + ).rejects.toThrow('Error finding product by name: Connection refused'); |
| 158 | + }); |
| 159 | + |
| 160 | + it('should handle unexpected error types', async () => { |
| 161 | + // Arrange |
| 162 | + mockRepository.findOneBy.mockRejectedValue({ |
| 163 | + customError: 'Custom error object', |
| 164 | + }); |
| 165 | + const productName = 'Test Product'; |
| 166 | + |
| 167 | + // Act & Assert |
| 168 | + await expect( |
| 169 | + productRepository.findProductByName(productName), |
| 170 | + ).rejects.toThrow('Error finding product by name: Unknown error'); |
| 171 | + }); |
| 172 | + }); |
| 173 | + |
| 174 | + describe('logging behavior', () => { |
| 175 | + it('should log appropriate messages for successful operations', async () => { |
| 176 | + // Arrange |
| 177 | + mockRepository.findOneBy.mockResolvedValue(testProduct); |
| 178 | + const productName = 'Test Product'; |
| 179 | + |
| 180 | + // Act |
| 181 | + await productRepository.findProductByName(productName); |
| 182 | + |
| 183 | + // Assert |
| 184 | + expect(logger.info).toHaveBeenCalledTimes(4); // Search start and success |
| 185 | + expect(logger.warn).not.toHaveBeenCalled(); |
| 186 | + expect(logger.error).not.toHaveBeenCalled(); |
| 187 | + }); |
| 188 | + |
| 189 | + it('should log warning for not found cases', async () => { |
| 190 | + // Arrange |
| 191 | + mockRepository.findOneBy.mockResolvedValue(null); |
| 192 | + const productName = 'Nonexistent Product'; |
| 193 | + |
| 194 | + // Act |
| 195 | + await productRepository.findProductByName(productName); |
| 196 | + |
| 197 | + // Assert |
| 198 | + expect(logger.warn).toHaveBeenCalledTimes(1); |
| 199 | + expect(logger.error).not.toHaveBeenCalled(); |
| 200 | + }); |
| 201 | + }); |
| 202 | +}); |
0 commit comments