import { coordinateMigration } from './migration_coordinator' ; import { createSavedObjectsMigrationLoggerMock } from '../mocks' ; describe ( 'coordinateMigration' , ( ) => { const log = createSavedObjectsMigrationLoggerMock ( ) ; test ( 'waits for isMigrated, if there is an index conflict' , async ( ) => { const pollInterval = 1 ; const runMigration = jest . fn ( ( ) => { throw { body : { error : { index : '.foo' , type : 'resource_already_exists_exception' } } } ; } ) ; const isMigrated = jest . fn ( ) ; isMigrated . mockResolvedValueOnce ( false ) . mockResolvedValueOnce ( true ) ; await coordinateMigration ( { log , runMigration , pollInterval , isMigrated , } ) ; expect ( runMigration ) . toHaveBeenCalledTimes ( 1 ) ; expect ( isMigrated ) . toHaveBeenCalledTimes ( 2 ) ; const warnings = log . warning . mock . calls . filter ( ( msg ) => /deleting index \.foo/ . test ( msg ) ) ; expect ( warnings . length ) . toEqual ( 1 ) ; } ) ; test ( 'does not poll if the runMigration succeeds' , async ( ) => { const pollInterval = 1 ; const runMigration = jest . fn < any , any > ( ( ) => Promise . resolve ( ) ) ; const isMigrated = jest . fn ( ( ) => Promise . resolve ( true ) ) ; await coordinateMigration ( { log , runMigration , pollInterval , isMigrated , } ) ; expect ( isMigrated ) . not . toHaveBeenCalled ( ) ; } ) ; test ( 'does not swallow exceptions' , async ( ) => { const pollInterval = 1 ; const runMigration = jest . fn ( ( ) => { throw new Error ( 'Doh' ) ; } ) ; const isMigrated = jest . fn ( ( ) => Promise . resolve ( true ) ) ; await expect ( coordinateMigration ( { log , runMigration , pollInterval , isMigrated , } ) ) . rejects . toThrow ( /Doh/ ) ; expect ( isMigrated ) . not . toHaveBeenCalled ( ) ; } ) ; } ) ;