Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Catch failed demo site status check errors #44

Merged
merged 2 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/hooks/tests/use-delete-snapshot.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// To run tests, execute `npm run test -- src/hooks/use-delete-snapshot.test.ts` from the root directory
import * as Sentry from '@sentry/electron/renderer';
import { renderHook, act } from '@testing-library/react';
import { useAuth } from '../../hooks/use-auth';
import { useOffline } from '../../hooks/use-offline';
import { useSiteDetails } from '../../hooks/use-site-details';
import { useDeleteSnapshot } from '../use-delete-snapshot';

jest.mock( '@sentry/electron/renderer' );
jest.mock( '../../hooks/use-site-details' );
jest.mock( '../../hooks/use-auth' );

Expand Down Expand Up @@ -119,4 +122,39 @@ describe( 'useDeleteSnapshot', () => {
} );
} );
} );

it( 'should gracefully handle failed status checks', async () => {
clientReqGet.mockRejectedValue( { message: 'Intentional server failure' } );
( useSiteDetails as jest.Mock ).mockImplementation( () => ( {
snapshots: mockSnapshots.map( ( snapshot ) => ( { ...snapshot, isDeleting: true } ) ),
removeSnapshot: removeSnapshotMock,
updateSnapshot: updateSnapshotMock,
} ) );
renderHook( () => useDeleteSnapshot() );

// Advance the timer to trigger the interval
await act( async () => {
expect( () => {
jest.advanceTimersByTime( 3000 );
} ).not.toThrow();
} );
expect( Sentry.captureException ).toHaveBeenCalledTimes( mockSnapshots.length );
} );

it( 'should not check demo site statuses when lacking an internet connection', () => {
( useOffline as jest.Mock ).mockReturnValue( true );
( useSiteDetails as jest.Mock ).mockImplementation( () => ( {
snapshots: mockSnapshots.map( ( snapshot ) => ( { ...snapshot, isDeleting: true } ) ),
removeSnapshot: removeSnapshotMock,
updateSnapshot: updateSnapshotMock,
} ) );
renderHook( () => useDeleteSnapshot() );

// Advance the timer to trigger the interval
act( () => {
jest.advanceTimersByTime( 3000 );
} );

expect( clientReqGet ).not.toHaveBeenCalled();
} );
} );
24 changes: 16 additions & 8 deletions src/hooks/use-delete-snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as Sentry from '@sentry/electron/renderer';
import { useI18n } from '@wordpress/react-i18n';
import { useCallback, useEffect, useState } from 'react';
import { useAuth } from './use-auth';
import { useOffline } from './use-offline';
import { useSiteDetails } from './use-site-details';

export interface SnapshotStatusResponse {
Expand All @@ -17,8 +18,9 @@ export function useDeleteSnapshot( options: { displayAlert?: boolean } = {} ) {
const { client } = useAuth();
const { removeSnapshot, snapshots, updateSnapshot } = useSiteDetails();
const { __ } = useI18n();
const isOffline = useOffline();
useEffect( () => {
if ( ! client?.req ) {
if ( ! client?.req || isOffline ) {
return;
}
const deletingSnapshots = snapshots.filter( ( snapshot ) => snapshot.isDeleting );
Expand All @@ -29,20 +31,26 @@ export function useDeleteSnapshot( options: { displayAlert?: boolean } = {} ) {
const intervalId = setInterval( async () => {
for ( const snapshot of deletingSnapshots ) {
if ( snapshot.isDeleting ) {
const resp: SnapshotStatusResponse = await client.req.get( '/jurassic-ninja/status', {
apiNamespace: 'wpcom/v2',
site_id: snapshot.atomicSiteId,
} );
if ( parseInt( resp.is_deleted ) === 1 ) {
removeSnapshot( snapshot );
try {
const resp: SnapshotStatusResponse = await client.req.get( '/jurassic-ninja/status', {
apiNamespace: 'wpcom/v2',
site_id: snapshot.atomicSiteId,
} );
if ( parseInt( resp.is_deleted ) === 1 ) {
removeSnapshot( snapshot );
}
} catch ( error ) {
// This error occurs in the background, so we report it but do not
// alert the user.
Sentry.captureException( error );
}
}
}
}, 3000 );
return () => {
clearInterval( intervalId );
};
}, [ client?.req, removeSnapshot, snapshots ] );
}, [ client?.req, isOffline, removeSnapshot, snapshots ] );

const deleteSnapshot = useCallback(
async ( snapshot: Pick< Snapshot, 'atomicSiteId' > ) => {
Expand Down
Loading