Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix: Prevent unnecessary demo site status request while offline
These requests will always fail, producing unnecessary noise.
  • Loading branch information
dcalhoun committed Apr 26, 2024
commit 0dad90e50c55cfbd6696286bc7a460bd8d30462e
18 changes: 18 additions & 0 deletions src/hooks/tests/use-delete-snapshot.test.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 { 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';

Expand Down Expand Up @@ -139,4 +140,21 @@ describe( 'useDeleteSnapshot', () => {
} );
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();
} );
} );
6 changes: 4 additions & 2 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 Down Expand Up @@ -48,7 +50,7 @@ export function useDeleteSnapshot( options: { displayAlert?: boolean } = {} ) {
return () => {
clearInterval( intervalId );
};
}, [ client?.req, removeSnapshot, snapshots ] );
}, [ client?.req, isOffline, removeSnapshot, snapshots ] );

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