import { act } from 'react-dom/test-utils' ; import { i18nTexts } from '../../../../public/application/sections/edit_policy/i18n_texts' ; import { PhaseWithTiming } from '../../../../common/types' ; import { setupEnvironment } from '../../helpers' ; import { setupValidationTestBed , ValidationTestBed } from './validation.helpers' ; describe ( '<EditPolicy /> timing validation' , ( ) => { let testBed ; let actions ; const { server , httpRequestsMockHelpers } = setupEnvironment ( ) ; beforeAll ( ( ) => { jest . useFakeTimers ( ) ; } ) ; afterAll ( ( ) => { jest . useRealTimers ( ) ; server . restore ( ) ; } ) ; beforeEach ( async ( ) => { httpRequestsMockHelpers . setDefaultResponses ( ) ; httpRequestsMockHelpers . setLoadPolicies ( [ ] ) ; await act ( async ( ) => { testBed = await setupValidationTestBed ( ) ; } ) ; const { component } = testBed ; component . update ( ) ; ( { actions } = testBed ) ; await actions . setPolicyName ( 'mypolicy' ) ; } ) ; [ { name : `doesn't allow empty timing` , value : '' , error : [ i18nTexts . editPolicy . errors . numberRequired ] , } , { name : `allows 0 for timing` , value : '0' , error : [ ] , } , { name : `doesn't allow -1 for timing` , value : '-1' , error : [ i18nTexts . editPolicy . errors . nonNegativeNumberRequired ] , } , { name : `doesn't allow decimals for timing (with dot)` , value : '5.5' , error : [ i18nTexts . editPolicy . errors . integerRequired ] , } , { name : `doesn't allow decimals for timing (with comma)` , value : '5,5' , error : [ i18nTexts . editPolicy . errors . integerRequired ] , } , ] . forEach ( ( testConfig ) => { [ 'warm' , 'cold' , 'delete' , 'frozen' ] . forEach ( ( phase ) => { const { name , value , error } = testConfig ; test ( `${ phase }: ${ name }` , async ( ) => { await actions . togglePhase ( phase as PhaseWithTiming ) ; await actions [ phase as PhaseWithTiming ] . setMinAgeValue ( '111' ) ; await actions [ phase as PhaseWithTiming ] . setMinAgeValue ( value ) ; actions . errors . waitForValidation ( ) ; actions . errors . expectMessages ( error ) ; } ) ; } ) ; } ) ; test ( 'should validate that min_age is equal or greater than previous phase min_age' , async ( ) => { await actions . togglePhase ( 'warm' ) ; await actions . togglePhase ( 'cold' ) ; await actions . togglePhase ( 'frozen' ) ; await actions . togglePhase ( 'delete' ) ; await actions . warm . setMinAgeValue ( '10' ) ; await actions . cold . setMinAgeValue ( '9' ) ; actions . errors . waitForValidation ( ) ; actions . errors . expectMessages ( [ 'Must be greater or equal than the warm phase value (10d)' ] , 'cold' ) ; await actions . frozen . setMinAgeValue ( '8' ) ; actions . errors . waitForValidation ( ) ; actions . errors . expectMessages ( [ 'Must be greater or equal than the cold phase value (9d)' ] , 'frozen' ) ; await actions . delete . setMinAgeValue ( '7' ) ; actions . errors . waitForValidation ( ) ; actions . errors . expectMessages ( [ 'Must be greater or equal than the frozen phase value (8d)' ] , 'delete' ) ; await actions . togglePhase ( 'warm' ) ; actions . errors . expectMessages ( [ ] , 'cold' ) ; await actions . cold . setMinAgeUnits ( 'h' ) ; actions . errors . expectMessages ( [ ] , 'frozen' ) ; actions . errors . expectMessages ( [ 'Must be greater or equal than the frozen phase value (8d)' ] , 'delete' ) ; await actions . delete . setMinAgeValue ( '9' ) ; actions . errors . expectMessages ( [ ] , 'delete' ) ; } ) ; } ) ;