import { act } from 'react-dom/test-utils' ; import { setupEnvironment , pageHelpers , nextTick } from './helpers' ; import { PolicyForm } from '../../public/application/components/policy_form' ; import { PolicyFormTestBed } from './helpers/policy_form.helpers' ; import { POLICY_EDIT } from './helpers/constant' ; import { TIME_UNITS } from '../../common/constants' ; const { setup } = pageHelpers . policyEdit ; const { setup : setupPolicyAdd } = pageHelpers . policyAdd ; const EXPIRE_AFTER_VALUE = '5' ; const EXPIRE_AFTER_UNIT = TIME_UNITS . MINUTE ; describe ( '<PolicyEdit />' , ( ) => { let testBed ; let testBedPolicyAdd ; const { server , httpRequestsMockHelpers } = setupEnvironment ( ) ; afterAll ( ( ) => { server . restore ( ) ; } ) ; describe ( 'on component mount' , ( ) => { beforeEach ( async ( ) => { httpRequestsMockHelpers . setGetPolicyResponse ( { policy : POLICY_EDIT } ) ; httpRequestsMockHelpers . setLoadIndicesResponse ( { indices : [ 'my_index' ] , dataStreams : [ 'my_data_stream' ] , } ) ; httpRequestsMockHelpers . setLoadRepositoriesResponse ( { repositories : [ { name : POLICY_EDIT . repository } ] , } ) ; testBed = await setup ( ) ; await act ( async ( ) => { await nextTick ( ) ; testBed . component . update ( ) ; } ) ; } ) ; test ( 'should set the correct page title' , ( ) => { const { exists , find } = testBed ; expect ( exists ( 'pageTitle' ) ) . toBe ( true ) ; expect ( find ( 'pageTitle' ) . text ( ) ) . toEqual ( 'Edit policy' ) ; } ) ; describe ( 'policy with pre-existing repository that was deleted' , ( ) => { beforeEach ( async ( ) => { httpRequestsMockHelpers . setGetPolicyResponse ( { policy : POLICY_EDIT } ) ; httpRequestsMockHelpers . setLoadIndicesResponse ( { indices : [ 'my_index' ] , dataStreams : [ 'my_data_stream' ] , } ) ; httpRequestsMockHelpers . setLoadRepositoriesResponse ( { repositories : [ { name : 'this-is-a-new-repository' } ] , } ) ; testBed = await setup ( ) ; await act ( async ( ) => { await nextTick ( ) ; testBed . component . update ( ) ; } ) ; } ) ; test ( 'should show repository-not-found warning' , ( ) => { const { exists , find } = testBed ; expect ( exists ( 'repositoryNotFoundWarning' ) ) . toBe ( true ) ; expect ( find ( 'repositorySelect' ) . props ( ) . value ) . toBe ( '' ) ; } ) ; describe ( 'validation' , ( ) => { test ( 'should block navigating to next step' , ( ) => { const { exists , find , actions } = testBed ; actions . clickNextButton ( ) ; expect ( exists ( 'repositoryNotFoundWarning' ) ) . toBe ( true ) ; expect ( find ( 'repositorySelect' ) . props ( ) . value ) . toBe ( '' ) ; } ) ; } ) ; } ) ; /**
         * As the "edit" policy component uses the same form underneath that
         * the "create" policy, we won't test it again but simply make sure that
         * the same form component is indeed shared between the 2 app sections.
         */ test ( 'should use the same Form component as the "<PolicyAdd />" section' , async ( ) => { testBedPolicyAdd = await setupPolicyAdd ( ) ; await act ( async ( ) => { await nextTick ( ) ; testBedPolicyAdd . component . update ( ) ; } ) ; const formEdit = testBed . component . find ( PolicyForm ) ; const formAdd = testBedPolicyAdd . component . find ( PolicyForm ) ; expect ( formEdit . length ) . toBe ( 1 ) ; expect ( formAdd . length ) . toBe ( 1 ) ; } ) ; test ( 'should disable the policy name field' , ( ) => { const { find } = testBed ; const nameInput = find ( 'nameInput' ) ; expect ( nameInput . props ( ) . disabled ) . toEqual ( true ) ; } ) ; describe ( 'form payload' , ( ) => { it ( 'should send the correct payload with changed values' , async ( ) => { const { form , actions } = testBed ; const { snapshotName } = POLICY_EDIT ; const editedSnapshotName = `${ snapshotName }-edited` ; form . setInputValue ( 'snapshotNameInput' , editedSnapshotName ) ; actions . clickNextButton ( ) ; form . toggleEuiSwitch ( 'ignoreUnavailableIndicesToggle' ) ; actions . clickNextButton ( ) ; form . setInputValue ( 'expireAfterValueInput' , EXPIRE_AFTER_VALUE ) ; form . setInputValue ( 'expireAfterUnitSelect' , EXPIRE_AFTER_UNIT ) ; actions . clickNextButton ( ) ; await act ( async ( ) => { actions . clickSubmitButton ( ) ; await nextTick ( ) ; } ) ; const latestRequest = server . requests [ server . requests . length - 1 ] ; const { name , isManagedPolicy , schedule , repository , retention } = POLICY_EDIT ; const expected = { name , isManagedPolicy , schedule , repository , config : { ignoreUnavailable : true , } , retention : { ... retention , expireAfterValue : Number ( EXPIRE_AFTER_VALUE ) , expireAfterUnit : EXPIRE_AFTER_UNIT , } , snapshotName : editedSnapshotName , } ; expect ( JSON . parse ( JSON . parse ( latestRequest . requestBody ) . body ) ) . toEqual ( expected ) ; } ) ; it ( 'should provide a default time unit value for retention' , async ( ) => { const { form , actions } = testBed ; actions . clickNextButton ( ) ; actions . clickNextButton ( ) ; form . setInputValue ( 'expireAfterValueInput' , EXPIRE_AFTER_VALUE ) ; actions . clickNextButton ( ) ; await act ( async ( ) => { actions . clickSubmitButton ( ) ; await nextTick ( ) ; } ) ; const latestRequest = server . requests [ server . requests . length - 1 ] ; const { name , isManagedPolicy , schedule , repository , retention , config , snapshotName } = POLICY_EDIT ; const expected = { name , isManagedPolicy , schedule , repository , config , snapshotName , retention : { ... retention , expireAfterValue : Number ( EXPIRE_AFTER_VALUE ) , expireAfterUnit : TIME_UNITS . DAY , } , } ; expect ( JSON . parse ( JSON . parse ( latestRequest . requestBody ) . body ) ) . toEqual ( expected ) ; } ) ; } ) ; } ) ; } ) ;