import { act } from '@testing-library/react' ; import type { ReactWrapper } from 'enzyme' ; import React from 'react' ; import { findTestSubject , mountWithIntl , nextTick , shallowWithIntl } from '@kbn/test/jest' ; import type { SummarizedCopyToSpaceResult } from '../lib' ; import type { ImportRetry } from '../types' ; import type { ResolveAllConflictsProps } from './resolve_all_conflicts' ; import { ResolveAllConflicts } from './resolve_all_conflicts' ; describe ( 'ResolveAllConflicts' , ( ) => { const summarizedCopyResult = { objects : [ { type : 'type-1' , id : 'id-1' , conflict : undefined } , { type : 'type-2' , id : 'id-2' , conflict : { error : { type : 'conflict' } } } , { type : 'type-3' , id : 'id-3' , conflict : { error : { type : 'conflict' , destinationId : 'dest-3' } } , } , { type : 'type-4' , id : 'id-4' , conflict : { error : { type : 'ambiguous_conflict' , destinations : [ { id : 'dest-4a' } , { id : 'dest-4b' } ] , } , } , } , { type : 'type-5' , id : 'id-5' , conflict : { error : { type : 'ambiguous_conflict' , destinations : [ { id : 'dest-5a' } , { id : 'dest-5b' } ] , } , } , } , ] , } as unknown as SummarizedCopyToSpaceResult ; const retries = [ { type : 'type-1' , id : 'id-1' , overwrite : false } , { type : 'type-5' , id : 'id-5' , overwrite : true , destinationId : 'dest-5b' } , ] ; const onRetriesChange = jest . fn ( ) ; const onDestinationMapChange = jest . fn ( ) ; const getOverwriteOption = ( wrapper ) => findTestSubject ( wrapper , 'cts-resolve-all-conflicts-overwrite' ) ; const getSkipOption = ( wrapper ) => findTestSubject ( wrapper , 'cts-resolve-all-conflicts-skip' ) ; beforeEach ( ( ) => { jest . resetAllMocks ( ) ; } ) ; const props = { summarizedCopyResult , retries , onRetriesChange , onDestinationMapChange , } ; const openPopover = async ( wrapper ) => { await act ( async ( ) => { wrapper . setState ( { isPopoverOpen : true } ) ; await nextTick ( ) ; wrapper . update ( ) ; } ) ; } ; it ( 'should render as expected' , async ( ) => { const wrapper = shallowWithIntl ( < ResolveAllConflicts { ... props } / > ) ; expect ( wrapper ) . toMatchInlineSnapshot ( `
      <EuiPopover
        anchorPosition="downLeft"
        button={
          <EuiLink
            className="spcCopyToSpace__resolveAllConflictsLink"
            onClick={[Function]}
          >
            <FormattedMessage
              defaultMessage="(resolve all)"
              id="xpack.spaces.management.copyToSpace.resolveAllConflictsLink"
              values={Object {}}
            />
          </EuiLink>
        }
        closePopover={[Function]}
        display="inlineBlock"
        hasArrow={true}
        id="resolveAllConflictsVisibilityPopover"
        isOpen={false}
        ownFocus={true}
        panelPaddingSize="none"
      >
        <EuiContextMenuPanel
          hasFocus={true}
          items={
            Array [
              <EuiContextMenuItem
                data-test-subj="cts-resolve-all-conflicts-overwrite"
                onClick={[Function]}
              >
                Overwrite all
              </EuiContextMenuItem>,
              <EuiContextMenuItem
                data-test-subj="cts-resolve-all-conflicts-skip"
                onClick={[Function]}
              >
                Skip all
              </EuiContextMenuItem>,
            ]
          }
        />
      </EuiPopover>
    ` ) ; } ) ; it ( 'should add overwrite retries when "Overwrite all" is selected' , async ( ) => { const wrapper = mountWithIntl ( < ResolveAllConflicts { ... props } / > ) ; await openPopover ( wrapper ) ; expect ( onRetriesChange ) . not . toHaveBeenCalled ( ) ; getOverwriteOption ( wrapper ) . simulate ( 'click' ) ; expect ( onRetriesChange ) . toHaveBeenCalledWith ( [ { type : 'type-1' , id : 'id-1' , overwrite : false } , { type : 'type-5' , id : 'id-5' , overwrite : true , destinationId : 'dest-5b' } , { type : 'type-2' , id : 'id-2' , overwrite : true } , { type : 'type-3' , id : 'id-3' , overwrite : true , destinationId : 'dest-3' } , { type : 'type-4' , id : 'id-4' , overwrite : true , destinationId : 'dest-4a' } , ] ) ; expect ( onDestinationMapChange ) . not . toHaveBeenCalled ( ) ; } ) ; it ( 'should remove overwrite retries when "Skip all" is selected' , async ( ) => { const wrapper = mountWithIntl ( < ResolveAllConflicts { ... props } / > ) ; await openPopover ( wrapper ) ; expect ( onRetriesChange ) . not . toHaveBeenCalled ( ) ; expect ( onDestinationMapChange ) . not . toHaveBeenCalled ( ) ; getSkipOption ( wrapper ) . simulate ( 'click' ) ; expect ( onRetriesChange ) . toHaveBeenCalledWith ( [ { type : 'type-1' , id : 'id-1' , overwrite : false } , ] ) ; expect ( onDestinationMapChange ) . toHaveBeenCalledWith ( undefined ) ; } ) ; } ) ;