import { arrayAtLeastOne , ArrayAtLeastOneConstraint , } from './array-at-least-one.validator' ; describe ( 'arrayAtLeastOne' , ( ) => { const allowedMock = [ 'boots' , 'clothes' , 'motorcycle' ] ; it ( 'should return "true" if the array include one of the values allowed' , ( ) => { const array = [ 'clothes' ] ; const valid = arrayAtLeastOne ( allowedMock , array ) ; expect ( valid ) . toStrictEqual ( true ) ; } ) ; it ( 'should return "true" if the array include two of the values allowed' , ( ) => { const array = [ 'boots' , 'motorcycle' ] ; const valid = arrayAtLeastOne ( allowedMock , array ) ; expect ( valid ) . toStrictEqual ( true ) ; } ) ; it ( 'should return "true" if the array include all the values allowed' , ( ) => { const array = [ 'boots' , 'clothes' , 'motorcycle' ] ; const valid = arrayAtLeastOne ( allowedMock , array ) ; expect ( valid ) . toStrictEqual ( true ) ; } ) ; it ( 'should return "true" if the array include one of the values allowed plus another unknown' , ( ) => { const array = [ 'clothes' , 'SarahConnor' ] ; const valid = arrayAtLeastOne ( allowedMock , array ) ; expect ( valid ) . toStrictEqual ( true ) ; } ) ; it ( 'should return "false" if the array none of the values allowed' , ( ) => { const array = [ 'I will be back 👍' ] ; const valid = arrayAtLeastOne ( allowedMock , array ) ; expect ( valid ) . toStrictEqual ( false ) ; } ) ; } ) ; describe ( 'ArrayAtLeastOneConstraint' , ( ) => { let constraint ; beforeEach ( ( ) => { constraint = new ArrayAtLeastOneConstraint ( ) ; } ) ; describe ( 'getAllowedList' , ( ) => { it ( 'should return value from constraints' , ( ) => { const resultsMock = [ 'boots' , 'clothes' , 'motorcycle' ] ; const validationArguments = { constraints : [ resultsMock ] , } ; const valid = constraint . getAllowedList ( validationArguments ) ; expect ( valid ) . toStrictEqual ( resultsMock ) ; } ) ; it ( 'should not return value from constraints' , ( ) => { const resultsMock = [ 'boots' , 'clothes' , 'motorcycle' ] ; const validationArguments = { constraints : [ 'wrong value' , resultsMock ] , } ; const invalid = constraint . getAllowedList ( validationArguments ) ; expect ( invalid ) . toStrictEqual ( 'wrong value' ) ; } ) ; it ( 'should throw error if there is no constraint' , ( ) => { const validationArguments = { } ; expect ( ( ) => constraint . getAllowedList ( validationArguments ) ) . toThrow ( ) ; } ) ; } ) ; describe ( 'validate' , ( ) => { const argumentsMock = { constraints : [ [ 'boots' , 'clothes' , 'motorcycle' ] ] , } ; it ( 'should return "true" if the array include one of the values allowed' , ( ) => { const value = [ 'clothes' ] ; const valid = constraint . validate ( value , argumentsMock ) ; expect ( valid ) . toStrictEqual ( true ) ; } ) ; it ( 'should return "true" if the value include two of the values allowed' , ( ) => { const value = [ 'boots' , 'motorcycle' ] ; const valid = constraint . validate ( value , argumentsMock ) ; expect ( valid ) . toStrictEqual ( true ) ; } ) ; it ( 'should return "true" if the value include all the values allowed' , ( ) => { const value = [ 'boots' , 'clothes' , 'motorcycle' ] ; const valid = constraint . validate ( value , argumentsMock ) ; expect ( valid ) . toStrictEqual ( true ) ; } ) ; it ( 'should return "true" if the value include one of the values allowed plus another unknown' , ( ) => { const value = [ 'clothes' , 'SarahConnor' ] ; const valid = constraint . validate ( value , argumentsMock ) ; expect ( valid ) . toStrictEqual ( true ) ; } ) ; it ( 'should return "false" if the value none of the values allowed' , ( ) => { const value = [ 'I will be back 👍' ] ; const valid = constraint . validate ( value , argumentsMock ) ; expect ( valid ) . toStrictEqual ( false ) ; } ) ; it ( 'should return "false" the value is not an array' , ( ) => { const value = 'Not an array' ; const valid = constraint . validate ( value , argumentsMock ) ; expect ( valid ) . toStrictEqual ( false ) ; } ) ; } ) ; describe ( 'defaultMessage' , ( ) => { it ( 'should return a formatted error message' , ( ) => { const validationArguments = { value : [ 'I' , 'will' , 'be' , 'back 👍' ] , property : 'Terminator' , constraints : [ [ 'boots' , 'clothes' , 'motorcycle' ] ] , } ; const message = constraint . defaultMessage ( validationArguments ) ; expect ( message ) . toStrictEqual ( 'Terminator allows only theses values: "boots, clothes, motorcycle", got: "I, will, be, back 👍"' ) ; } ) ; it ( 'should return a formatted error message with no-array value' , ( ) => { const validationArguments = { value : 'I will be back 👍' , property : 'Terminator' , constraints : [ [ 'boots' , 'clothes' , 'motorcycle' ] ] , } ; const message = constraint . defaultMessage ( validationArguments ) ; expect ( message ) . toStrictEqual ( 'Terminator allows only theses values: "boots, clothes, motorcycle", got: "I will be back 👍"' ) ; } ) ; it ( 'should return a formatted error message with an undefined value' , ( ) => { const validationArguments = { value : undefined , property : 'Terminator' , constraints : [ [ 'boots' , 'clothes' , 'motorcycle' ] ] , } ; const message = constraint . defaultMessage ( validationArguments ) ; expect ( message ) . toStrictEqual ( 'Terminator allows only theses values: "boots, clothes, motorcycle", got: "undefined"' ) ; } ) ; } ) ; } ) ;