| 1 |  |  | import { mock, instance, when, verify, anything } from 'ts-mockito'; | 
            
                                                        
            
                                    
            
            
                | 2 |  |  | import { ProductRepository } from 'src/Infrastructure/Product/Repository/ProductRepository'; | 
            
                                                        
            
                                    
            
            
                | 3 |  |  | import { Product } from 'src/Domain/Product/Product.entity'; | 
            
                                                        
            
                                    
            
            
                | 4 |  |  | import { RemoveProductCommandHandler } from './RemoveProductCommandHandler'; | 
            
                                                        
            
                                    
            
            
                | 5 |  |  | import { RemoveProductCommand } from './RemoveProductCommand'; | 
            
                                                        
            
                                    
            
            
                | 6 |  |  | import { ProductNotFoundException } from 'src/Domain/Product/Exception/ProductNotFoundException'; | 
            
                                                        
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 8 |  |  | describe('RemoveProductCommandHandler', () => { | 
            
                                                        
            
                                    
            
            
                | 9 |  |  |   let productRepository: ProductRepository; | 
            
                                                        
            
                                    
            
            
                | 10 |  |  |   let removedProduct: Product; | 
            
                                                        
            
                                    
            
            
                | 11 |  |  |   let handler: RemoveProductCommandHandler; | 
            
                                                        
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 13 |  |  |   const command = new RemoveProductCommand('17efcbee-bd2f-410e-9e99-51684b592bad'); | 
            
                                                        
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 15 |  |  |   beforeEach(() => { | 
            
                                                        
            
                                    
            
            
                | 16 |  |  |     productRepository = mock(ProductRepository); | 
            
                                                        
            
                                    
            
            
                | 17 |  |  |     removedProduct = mock(Product); | 
            
                                                        
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 19 |  |  |     handler = new RemoveProductCommandHandler( | 
            
                                                        
            
                                    
            
            
                | 20 |  |  |       instance(productRepository) | 
            
                                                        
            
                                    
            
            
                | 21 |  |  |     ); | 
            
                                                        
            
                                    
            
            
                | 22 |  |  |   }); | 
            
                                                        
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 24 |  |  |   it('testProductRemovedSuccessfully', async () => { | 
            
                                                        
            
                                    
            
            
                | 25 |  |  |     when(productRepository.findOneById('17efcbee-bd2f-410e-9e99-51684b592bad')) | 
            
                                                        
            
                                    
            
            
                | 26 |  |  |       .thenResolve(instance(removedProduct)); | 
            
                                                        
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 28 |  |  |     await handler.execute(command); | 
            
                                                        
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 30 |  |  |     verify( | 
            
                                                        
            
                                    
            
            
                | 31 |  |  |       productRepository.remove(instance(removedProduct)) | 
            
                                                        
            
                                    
            
            
                | 32 |  |  |     ).once(); | 
            
                                                        
            
                                    
            
            
                | 33 |  |  |     verify(productRepository.findOneById('17efcbee-bd2f-410e-9e99-51684b592bad')).once(); | 
            
                                                        
            
                                    
            
            
                | 34 |  |  |   }); | 
            
                                                        
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 36 |  |  |   it('testProductNotFound', async () => { | 
            
                                                        
            
                                    
            
            
                | 37 |  |  |     when(productRepository.findOneById('17efcbee-bd2f-410e-9e99-51684b592bad')) | 
            
                                                        
            
                                    
            
            
                | 38 |  |  |       .thenResolve(null); | 
            
                                                        
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 40 |  |  |     try { | 
            
                                                        
            
                                    
            
            
                | 41 |  |  |       expect(await handler.execute(command)).toBeUndefined(); | 
            
                                                        
            
                                    
            
            
                | 42 |  |  |     } catch (e) { | 
            
                                                        
            
                                    
            
            
                | 43 |  |  |       expect(e).toBeInstanceOf(ProductNotFoundException); | 
            
                                                        
            
                                    
            
            
                | 44 |  |  |       expect(e.message).toBe('products.errors.not_found'); | 
            
                                                        
            
                                    
            
            
                | 45 |  |  |       verify(productRepository.findOneById('17efcbee-bd2f-410e-9e99-51684b592bad')).once(); | 
            
                                                        
            
                                    
            
            
                | 46 |  |  |       verify(productRepository.remove(anything())).never(); | 
            
                                                        
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 48 |  |  |   }); | 
            
                                                        
            
                                    
            
            
                | 49 |  |  | }); | 
            
                                                        
            
                                    
            
            
                | 50 |  |  |  |