import { act } from 'react-dom/test-utils' ; import { setupEnvironment } from '../../helpers' ; import { setupValidationTestBed , ValidationTestBed } from './validation.helpers' ; describe ( '<EditPolicy /> error indicators' , ( ) => { let testBed ; const { server , httpRequestsMockHelpers } = setupEnvironment ( ) ; beforeAll ( ( ) => { jest . useFakeTimers ( ) ; } ) ; afterAll ( ( ) => { jest . useRealTimers ( ) ; server . restore ( ) ; } ) ; beforeEach ( async ( ) => { httpRequestsMockHelpers . setDefaultResponses ( ) ; await act ( async ( ) => { testBed = await setupValidationTestBed ( ) ; } ) ; const { component } = testBed ; component . update ( ) ; } ) ; test ( 'shows phase error indicators correctly' , async ( ) => { const { actions } = testBed ; expect ( actions . errors . havePhaseCallout ( 'hot' ) ) . toBe ( false ) ; expect ( actions . errors . havePhaseCallout ( 'warm' ) ) . toBe ( false ) ; expect ( actions . errors . havePhaseCallout ( 'cold' ) ) . toBe ( false ) ; await actions . hot . toggleForceMerge ( ) ; await actions . hot . setForcemergeSegmentsCount ( '-22' ) ; actions . errors . waitForValidation ( ) ; expect ( actions . errors . havePhaseCallout ( 'hot' ) ) . toBe ( true ) ; expect ( actions . errors . havePhaseCallout ( 'warm' ) ) . toBe ( false ) ; expect ( actions . errors . havePhaseCallout ( 'cold' ) ) . toBe ( false ) ; await actions . togglePhase ( 'warm' ) ; await actions . warm . toggleForceMerge ( ) ; await actions . warm . setForcemergeSegmentsCount ( '-22' ) ; actions . errors . waitForValidation ( ) ; expect ( actions . errors . havePhaseCallout ( 'hot' ) ) . toBe ( true ) ; expect ( actions . errors . havePhaseCallout ( 'warm' ) ) . toBe ( true ) ; expect ( actions . errors . havePhaseCallout ( 'cold' ) ) . toBe ( false ) ; await actions . togglePhase ( 'cold' ) ; await actions . cold . setReplicas ( '-33' ) ; actions . errors . waitForValidation ( ) ; expect ( actions . errors . havePhaseCallout ( 'hot' ) ) . toBe ( true ) ; expect ( actions . errors . havePhaseCallout ( 'warm' ) ) . toBe ( true ) ; expect ( actions . errors . havePhaseCallout ( 'cold' ) ) . toBe ( true ) ; await actions . hot . setForcemergeSegmentsCount ( '1' ) ; actions . errors . waitForValidation ( ) ; expect ( actions . errors . havePhaseCallout ( 'hot' ) ) . toBe ( false ) ; expect ( actions . errors . havePhaseCallout ( 'warm' ) ) . toBe ( true ) ; expect ( actions . errors . havePhaseCallout ( 'cold' ) ) . toBe ( true ) ; await actions . warm . setForcemergeSegmentsCount ( '1' ) ; actions . errors . waitForValidation ( ) ; expect ( actions . errors . havePhaseCallout ( 'hot' ) ) . toBe ( false ) ; expect ( actions . errors . havePhaseCallout ( 'warm' ) ) . toBe ( false ) ; expect ( actions . errors . havePhaseCallout ( 'cold' ) ) . toBe ( true ) ; await actions . cold . setReplicas ( '1' ) ; actions . errors . waitForValidation ( ) ; expect ( actions . errors . havePhaseCallout ( 'hot' ) ) . toBe ( false ) ; expect ( actions . errors . havePhaseCallout ( 'warm' ) ) . toBe ( false ) ; expect ( actions . errors . havePhaseCallout ( 'cold' ) ) . toBe ( false ) ; } ) ; test ( 'global error callout should show, after clicking the "Save" button, if there are any form errors' , async ( ) => { const { actions } = testBed ; expect ( actions . errors . haveGlobalCallout ( ) ) . toBe ( false ) ; expect ( actions . errors . havePhaseCallout ( 'hot' ) ) . toBe ( false ) ; expect ( actions . errors . havePhaseCallout ( 'warm' ) ) . toBe ( false ) ; expect ( actions . errors . havePhaseCallout ( 'cold' ) ) . toBe ( false ) ; await actions . toggleSaveAsNewPolicy ( ) ; await actions . setPolicyName ( '' ) ; actions . errors . waitForValidation ( ) ; await actions . savePolicy ( ) ; expect ( actions . errors . haveGlobalCallout ( ) ) . toBe ( true ) ; expect ( actions . errors . havePhaseCallout ( 'hot' ) ) . toBe ( false ) ; expect ( actions . errors . havePhaseCallout ( 'warm' ) ) . toBe ( false ) ; expect ( actions . errors . havePhaseCallout ( 'cold' ) ) . toBe ( false ) ; } ) ; test ( 'clears all error indicators if last erring field is unmounted' , async ( ) => { const { actions } = testBed ; await actions . togglePhase ( 'cold' ) ; await actions . cold . setMinAgeValue ( '7' ) ; await actions . cold . setSearchableSnapshot ( '' ) ; await actions . savePolicy ( ) ; actions . errors . waitForValidation ( ) ; expect ( actions . errors . haveGlobalCallout ( ) ) . toBe ( true ) ; expect ( actions . errors . havePhaseCallout ( 'hot' ) ) . toBe ( false ) ; expect ( actions . errors . havePhaseCallout ( 'warm' ) ) . toBe ( false ) ; expect ( actions . errors . havePhaseCallout ( 'cold' ) ) . toBe ( true ) ; await actions . cold . toggleSearchableSnapshot ( ) ; expect ( actions . errors . haveGlobalCallout ( ) ) . toBe ( false ) ; expect ( actions . errors . havePhaseCallout ( 'hot' ) ) . toBe ( false ) ; expect ( actions . errors . havePhaseCallout ( 'warm' ) ) . toBe ( false ) ; expect ( actions . errors . havePhaseCallout ( 'cold' ) ) . toBe ( false ) ; } ) ; } ) ;