import { Validator , ValidationComposite } from '@/application/validation' ; import { mock , MockProxy } from 'jest-mock-extended' ; describe ( 'ValidationComposite' , ( ) => { let sut ; let validator1 ; let validator2 ; let validators ; beforeAll ( ( ) => { validator1 = mock < Validator > ( ) ; validator1 . validate . mockReturnValue ( undefined ) ; validator2 = mock < Validator > ( ) ; validator2 . validate . mockReturnValue ( undefined ) ; validators = [ validator1 , validator2 ] ; } ) ; beforeEach ( ( ) => { sut = new ValidationComposite ( validators ) ; } ) ; it ( 'should return undefined if all Validators return undefined' , ( ) => { const error = sut . validate ( ) ; expect ( error ) . toBeUndefined ( ) ; } ) ; it ( 'should return the first error' , ( ) => { validator1 . validate . mockReturnValueOnce ( new Error ( 'error_1' ) ) ; validator2 . validate . mockReturnValueOnce ( new Error ( 'error_2' ) ) ; const error = sut . validate ( ) ; expect ( error ) . toEqual ( new Error ( 'error_1' ) ) ; } ) ; it ( 'should return error if any Validator fails' , ( ) => { validator2 . validate . mockReturnValueOnce ( new Error ( 'error_2' ) ) ; const error = sut . validate ( ) ; expect ( error ) . toEqual ( new Error ( 'error_2' ) ) ; } ) ; } ) ;