import { CLOUD_BACKUP_STATUS_POLL_INTERVAL_MS , CLOUD_SNAPSHOT_REPOSITORY , } from '../../../../common/constants' ; import { setupEnvironment , advanceTime } from '../../helpers' ; import { OverviewTestBed , setupOverviewPage } from '../overview.helpers' ; describe ( 'Overview - Backup Step' , ( ) => { let testBed ; let server ; let setServerAsync ; let httpRequestsMockHelpers ; beforeEach ( ( ) => { ( { server , setServerAsync , httpRequestsMockHelpers } = setupEnvironment ( ) ) ; } ) ; afterEach ( ( ) => { server . restore ( ) ; } ) ; describe ( 'On-prem' , ( ) => { beforeEach ( async ( ) => { testBed = await setupOverviewPage ( ) ; } ) ; test ( 'Shows link to Snapshot and Restore' , ( ) => { const { exists , find } = testBed ; expect ( exists ( 'snapshotRestoreLink' ) ) . toBe ( true ) ; expect ( find ( 'snapshotRestoreLink' ) . props ( ) . href ) . toBe ( 'snapshotAndRestoreUrl' ) ; } ) ; test ( 'renders step as incomplete ' , ( ) => { const { exists } = testBed ; expect ( exists ( 'backupStep-incomplete' ) ) . toBe ( true ) ; } ) ; } ) ; describe ( 'On Cloud' , ( ) => { const setupCloudOverviewPage = async ( ) => setupOverviewPage ( { plugins : { cloud : { isCloudEnabled : true , deploymentUrl : 'deploymentUrl' , } , } , } ) ; describe ( 'initial loading state' , ( ) => { beforeEach ( async ( ) => { setServerAsync ( true ) ; testBed = await setupCloudOverviewPage ( ) ; } ) ; afterEach ( ( ) => { setServerAsync ( false ) ; } ) ; test ( 'is rendered' , ( ) => { const { exists } = testBed ; expect ( exists ( 'cloudBackupLoading' ) ) . toBe ( true ) ; } ) ; } ) ; describe ( 'error state' , ( ) => { beforeEach ( async ( ) => { httpRequestsMockHelpers . setLoadCloudBackupStatusResponse ( undefined , { statusCode : 400 , message : 'error' , } ) ; testBed = await setupCloudOverviewPage ( ) ; } ) ; test ( 'is rendered' , ( ) => { const { exists } = testBed ; testBed . component . update ( ) ; expect ( exists ( 'cloudBackupErrorCallout' ) ) . toBe ( true ) ; } ) ; test ( 'lets the user attempt to reload backup status' , ( ) => { const { exists } = testBed ; testBed . component . update ( ) ; expect ( exists ( 'cloudBackupRetryButton' ) ) . toBe ( true ) ; } ) ; test ( 'loads on prem if missing found-snapshots repository' , async ( ) => { httpRequestsMockHelpers . setLoadCloudBackupStatusResponse ( undefined , { statusCode : 404 , message : `[${ CLOUD_SNAPSHOT_REPOSITORY }] missing` , } ) ; testBed = await setupCloudOverviewPage ( ) ; const { exists } = testBed ; testBed . component . update ( ) ; expect ( exists ( 'snapshotRestoreLink' ) ) . toBe ( true ) ; expect ( exists ( 'cloudBackupErrorCallout' ) ) . toBe ( false ) ; } ) ; } ) ; describe ( 'success state' , ( ) => { describe ( 'when data is backed up' , ( ) => { beforeEach ( async ( ) => { httpRequestsMockHelpers . setLoadCloudBackupStatusResponse ( { isBackedUp : true , lastBackupTime : '2021-08-25T19:59:59.863Z' , } ) ; testBed = await setupCloudOverviewPage ( ) ; } ) ; test ( 'renders link to Cloud backups and last backup time ' , ( ) => { const { exists , find } = testBed ; expect ( exists ( 'dataBackedUpStatus' ) ) . toBe ( true ) ; expect ( exists ( 'cloudSnapshotsLink' ) ) . toBe ( true ) ; expect ( find ( 'dataBackedUpStatus' ) . text ( ) ) . toContain ( 'Last snapshot created on' ) ; } ) ; test ( 'renders step as complete ' , ( ) => { const { exists } = testBed ; expect ( exists ( 'backupStep-complete' ) ) . toBe ( true ) ; } ) ; } ) ; describe ( `when data isn't backed up` , ( ) => { beforeEach ( async ( ) => { httpRequestsMockHelpers . setLoadCloudBackupStatusResponse ( { isBackedUp : false , lastBackupTime : undefined , } ) ; testBed = await setupCloudOverviewPage ( ) ; } ) ; test ( 'renders link to Cloud backups and "not backed up" status' , ( ) => { const { exists } = testBed ; expect ( exists ( 'dataNotBackedUpStatus' ) ) . toBe ( true ) ; expect ( exists ( 'cloudSnapshotsLink' ) ) . toBe ( true ) ; } ) ; test ( 'renders step as incomplete ' , ( ) => { const { exists } = testBed ; expect ( exists ( 'backupStep-incomplete' ) ) . toBe ( true ) ; } ) ; } ) ; } ) ; describe ( 'poll for new status' , ( ) => { beforeEach ( async ( ) => { jest . useFakeTimers ( ) ; httpRequestsMockHelpers . setLoadCloudBackupStatusResponse ( { isBackedUp : true , lastBackupTime : '2021-08-25T19:59:59.863Z' , } ) ; testBed = await setupCloudOverviewPage ( ) ; } ) ; afterEach ( ( ) => { jest . useRealTimers ( ) ; } ) ; test ( 'renders step as incomplete when a success state is followed by an error state' , async ( ) => { const { exists } = testBed ; expect ( exists ( 'backupStep-complete' ) ) . toBe ( true ) ; httpRequestsMockHelpers . setLoadCloudBackupStatusResponse ( undefined , { statusCode : 400 , message : 'error' , } ) ; await advanceTime ( CLOUD_BACKUP_STATUS_POLL_INTERVAL_MS ) ; testBed . component . update ( ) ; expect ( exists ( 'backupStep-incomplete' ) ) . toBe ( true ) ; } ) ; } ) ; } ) ; } ) ;