import { act } from 'react-dom/test-utils' ; import { OverviewTestBed , setupOverviewPage } from '../overview.helpers' ; import { setupEnvironment , advanceTime } from '../../helpers' ; import { SYSTEM_INDICES_MIGRATION_POLL_INTERVAL_MS } from '../../../../common/constants' ; describe ( 'Overview - Migrate system indices - Step completion' , ( ) => { let testBed ; const { server , httpRequestsMockHelpers } = setupEnvironment ( ) ; afterAll ( ( ) => { server . restore ( ) ; } ) ; test ( `It's complete when no upgrade is needed` , async ( ) => { httpRequestsMockHelpers . setLoadSystemIndicesMigrationStatus ( { migration_status : 'NO_MIGRATION_NEEDED' , } ) ; await act ( async ( ) => { testBed = await setupOverviewPage ( ) ; } ) ; const { exists , component } = testBed ; component . update ( ) ; expect ( exists ( `migrateSystemIndicesStep-complete` ) ) . toBe ( true ) ; } ) ; test ( `It's incomplete when migration is needed` , async ( ) => { httpRequestsMockHelpers . setLoadSystemIndicesMigrationStatus ( { migration_status : 'MIGRATION_NEEDED' , } ) ; await act ( async ( ) => { testBed = await setupOverviewPage ( ) ; } ) ; const { exists , component } = testBed ; component . update ( ) ; expect ( exists ( `migrateSystemIndicesStep-incomplete` ) ) . toBe ( true ) ; } ) ; describe ( 'Poll for new status' , ( ) => { beforeEach ( async ( ) => { jest . useFakeTimers ( ) ; httpRequestsMockHelpers . setLoadSystemIndicesMigrationStatus ( { migration_status : 'IN_PROGRESS' , } ) ; testBed = await setupOverviewPage ( ) ; } ) ; afterEach ( ( ) => { jest . useRealTimers ( ) ; } ) ; test ( 'renders step as complete when a upgraded needed status is followed by a no upgrade needed' , async ( ) => { const { exists } = testBed ; expect ( exists ( 'migrateSystemIndicesStep-incomplete' ) ) . toBe ( true ) ; httpRequestsMockHelpers . setLoadSystemIndicesMigrationStatus ( { migration_status : 'NO_MIGRATION_NEEDED' , } ) ; await advanceTime ( SYSTEM_INDICES_MIGRATION_POLL_INTERVAL_MS ) ; testBed . component . update ( ) ; expect ( exists ( 'migrateSystemIndicesStep-complete' ) ) . toBe ( true ) ; } ) ; } ) ; } ) ;