Skip to content
Prev Previous commit
Fix unit tests
  • Loading branch information
fluiddot committed Oct 2, 2024
commit b7fff68e0177a01fd7726c19e11961cc971c829d
16 changes: 8 additions & 8 deletions src/components/tests/assistant-anchor.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe( 'Anchor', () => {
( useSiteDetails as jest.Mock ).mockReturnValue( {} );
( getIpcApi as jest.Mock ).mockReturnValue( {
openURL: jest.fn( () => Promise.resolve() ),
showMessageBox: jest.fn(),
showErrorMessageBox: jest.fn(),
} );
} );

Expand Down Expand Up @@ -79,9 +79,10 @@ describe( 'Anchor', () => {
} );

it( 'should gracefully handle link open failures', async () => {
const error = new Error( 'Failed to open link' );
( getIpcApi as jest.Mock ).mockReturnValue( {
openURL: jest.fn( () => Promise.reject( new Error( 'Failed to open link' ) ) ),
showMessageBox: jest.fn(),
openURL: jest.fn( () => Promise.reject( error ) ),
showErrorMessageBox: jest.fn(),
} );
render( <Anchor href="https://example.com" children="Example link" /> );

Expand All @@ -90,11 +91,10 @@ describe( 'Anchor', () => {
// Await asynchronous openURL execution
await new Promise( process.nextTick );

expect( getIpcApi().showMessageBox ).toHaveBeenCalledWith( {
type: 'error',
message: 'Failed to open link',
detail: 'We were unable to open the link. Please try again.',
buttons: [ 'OK' ],
expect( getIpcApi().showErrorMessageBox ).toHaveBeenCalledWith( {
title: 'Failed to open link',
message: 'We were unable to open the link. Please try again.',
error,
} );
expect( Sentry.captureException ).toHaveBeenCalled();
} );
Expand Down
15 changes: 8 additions & 7 deletions src/components/tests/header.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function mockGetIpcApi( mocks: Record< string, jest.Mock > ) {
getSnapshots: jest.fn( () => Promise.resolve( [] ) ),
saveSnapshotsToStorage: jest.fn( () => Promise.resolve() ),
startServer: jest.fn( () => Promise.resolve( { running: true } ) ),
showMessageBox: jest.fn(),
showErrorMessageBox: jest.fn(),
...mocks,
} );
}
Expand Down Expand Up @@ -54,9 +54,10 @@ describe( 'Header', () => {
describe( 'when starting a server fails', () => {
it( 'should display an error message', async () => {
const user = userEvent.setup();
const error = new Error( 'Failed to start the server' );
mockGetIpcApi( {
startServer: jest.fn( () => {
throw new Error( 'Failed to start the server' );
throw error;
} ),
stopServer: jest.fn( () => Promise.resolve( { running: false } ) ),
} );
Expand All @@ -72,12 +73,12 @@ describe( 'Header', () => {

expect( mockedGetIpcApi().startServer ).toHaveBeenCalledTimes( 1 );
expect( screen.getByText( 'Start' ) ).toBeVisible();
expect( mockedGetIpcApi().showMessageBox ).toHaveBeenCalledTimes( 1 );
expect( mockedGetIpcApi().showMessageBox ).toHaveBeenCalledWith( {
type: 'error',
message: 'Failed to start the site server',
detail:
expect( mockedGetIpcApi().showErrorMessageBox ).toHaveBeenCalledTimes( 1 );
expect( mockedGetIpcApi().showErrorMessageBox ).toHaveBeenCalledWith( {
title: 'Failed to start the site server',
message:
"Please verify your site's local path directory contains the standard WordPress installation files and try again. If this problem persists, please contact support.",
error,
} );
} );
} );
Expand Down
20 changes: 13 additions & 7 deletions src/hooks/tests/use-import-export.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ beforeEach( () => {
jest.clearAllMocks();
( getIpcApi as jest.Mock ).mockReturnValue( {
showSaveAsDialog: jest.fn(),
showMessageBox: jest.fn().mockResolvedValue( { response: 0, checkboxChecked: false } ),
showErrorMessageBox: jest.fn(),
showNotification: jest.fn(),
exportSite: jest.fn().mockReturnValue( true ),
importSite: jest.fn().mockReturnValue( selectedSite ),
Expand Down Expand Up @@ -84,9 +84,12 @@ describe( 'useImportExport hook', () => {
},
SITE_ID
);
expect( getIpcApi().showMessageBox ).toHaveBeenCalledWith(
expect.objectContaining( { type: 'error', message: 'Failed exporting site' } )
);
expect( getIpcApi().showErrorMessageBox ).toHaveBeenCalledWith( {
title: 'Failed exporting site',
message:
'An error occurred while exporting the site. If this problem persists, please contact support.',
error: 'error',
} );
} );

it( 'exports database', async () => {
Expand Down Expand Up @@ -233,9 +236,12 @@ describe( 'useImportExport hook', () => {
path: 'backup.zip',
},
} );
expect( getIpcApi().showMessageBox ).toHaveBeenCalledWith(
expect.objectContaining( { type: 'error', message: 'Failed importing site' } )
);
expect( getIpcApi().showErrorMessageBox ).toHaveBeenCalledWith( {
title: 'Failed importing site',
message:
'An error occurred while importing the site. Verify the file is a valid Jetpack backup, Local, Playground or .sql database file and try again. If this problem persists, please contact support.',
error: 'error',
} );
} );

it( 'does not import if another import is running', async () => {
Expand Down
15 changes: 8 additions & 7 deletions src/hooks/tests/use-update-demo-site.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jest.mock( '../../hooks/use-auth' );
jest.mock( '../../lib/get-ipc-api', () => ( {
getIpcApi: jest.fn().mockReturnValue( {
archiveSite: jest.fn().mockResolvedValue( { zipContent: new Blob( [ 'zipContent' ] ) } ),
showMessageBox: jest.fn().mockResolvedValue( { response: 1 } ), // Assuming '1' is the cancel button
showErrorMessageBox: jest.fn().mockResolvedValue( { response: 1 } ), // Assuming '1' is the cancel button
getWpVersion: jest.fn().mockResolvedValue( '6.5' ),
} ),
} ) );
Expand Down Expand Up @@ -108,7 +108,8 @@ describe( 'useUpdateDemoSite', () => {
} );

it( 'when an update fails, ensure an alert is triggered', async () => {
clientReqPost.mockRejectedValue( new Error( 'Update failed' ) );
const error = new Error( 'Update failed' );
clientReqPost.mockRejectedValue( error );

const { result } = renderHook( () => useUpdateDemoSite(), { wrapper } );

Expand All @@ -118,11 +119,11 @@ describe( 'useUpdateDemoSite', () => {
} );

// Assert that an alert is displayed to inform users of the failure
expect( getIpcApi().showMessageBox ).toHaveBeenCalledWith(
expect.objectContaining( {
type: 'warning',
} )
);
expect( getIpcApi().showErrorMessageBox ).toHaveBeenCalledWith( {
title: 'Update failed',
message: "We couldn't update the Test Site demo site. Please try again.",
error,
} );

// Assert that 'isDemoSiteUpdating' is set back to false
expect( result.current.isDemoSiteUpdating( mockLocalSite.id ) ).toBe( false );
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/use-update-demo-site.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const DemoSiteUpdateProvider: React.FC< DemoSiteUpdateProviderProps > = (
getIpcApi().showErrorMessageBox( {
title: __( 'Update failed' ),
message: sprintf(
__( "We couldn't update the %s demo site. Please try again" ),
__( "We couldn't update the %s demo site. Please try again." ),
localSite.name
),
error,
Expand Down