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

Add unit tests to cover changing PHP version functionality #233

Merged
merged 5 commits into from
Jun 12, 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
16 changes: 16 additions & 0 deletions jest-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ import nock from 'nock';
if ( typeof window !== 'undefined' ) {
// The ipcListener global is usually defined in preload.ts
window.ipcListener = { subscribe: jest.fn() };

// Mock `matchMedia` as it's not implemented in JSDOM
// Reference: https://jestjs.io/docs/manual-mocks#mocking-methods-which-are-not-implemented-in-jsdom
Object.defineProperty( window, 'matchMedia', {
writable: true,
value: jest.fn().mockImplementation( ( query ) => ( {
matches: false,
media: query,
onchange: null,
addListener: jest.fn(), // deprecated
removeListener: jest.fn(), // deprecated
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
} ) ),
} );
}

nock.disableNetConnect();
Expand Down
84 changes: 83 additions & 1 deletion src/components/tests/content-tab-settings.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// To run tests, execute `npm run test -- src/components/content-tab-settings.test.tsx` from the root directory
import { fireEvent, render, screen } from '@testing-library/react';
import { fireEvent, render, screen, within } from '@testing-library/react';
import { userEvent } from '@testing-library/user-event';
import { useGetPhpVersion } from '../../hooks/use-get-php-version';
import { useGetWpVersion } from '../../hooks/use-get-wp-version';
Expand Down Expand Up @@ -152,4 +152,86 @@ describe( 'ContentTabSettings', () => {
expect( copyText ).toHaveBeenCalledWith( 'password' );
} );
} );

describe( 'PHP version', () => {
it( 'changes PHP version when site is not running', async () => {
const user = userEvent.setup();

const updateSite = jest.fn();
const startServer = jest.fn();
const stopServer = jest.fn();
// Mock snapshots to include a snapshot for the selected site
( useSiteDetails as jest.Mock ).mockReturnValue( {
selectedSite: { ...selectedSite, running: false } as SiteDetails,
snapshots: [ { localSiteId: selectedSite.id } ],
updateSite,
startServer,
stopServer,
} );
( useGetPhpVersion as jest.Mock ).mockReturnValue( '8.0' );

const { rerender } = render( <ContentTabSettings selectedSite={ selectedSite } /> );
await user.click( screen.getByRole( 'button', { name: 'Edit PHP version' } ) );
const dialog = screen.getByRole( 'dialog' );
expect( dialog ).toBeVisible();
await user.selectOptions(
within( dialog ).getByRole( 'combobox', {
name: 'PHP version',
} ),
'8.2'
);
await user.click(
within( dialog ).getByRole( 'button', {
name: 'Save',
} )
);
expect( updateSite ).toHaveBeenCalledWith( expect.objectContaining( { phpVersion: '8.2' } ) );
expect( stopServer ).not.toHaveBeenCalled();
expect( startServer ).not.toHaveBeenCalled();

( useGetPhpVersion as jest.Mock ).mockReturnValue( '8.2' );
rerender( <ContentTabSettings selectedSite={ selectedSite } /> );
expect( screen.getByText( '8.2' ) ).toBeVisible();
} );

it( 'changes PHP version and restarts site when site is running', async () => {
const user = userEvent.setup();

const updateSite = jest.fn();
const startServer = jest.fn();
const stopServer = jest.fn();
// Mock snapshots to include a snapshot for the selected site
( useSiteDetails as jest.Mock ).mockReturnValue( {
selectedSite: { ...selectedSite, running: true } as SiteDetails,
snapshots: [ { localSiteId: selectedSite.id } ],
updateSite,
startServer,
stopServer,
} );
( useGetPhpVersion as jest.Mock ).mockReturnValue( '8.0' );

const { rerender } = render( <ContentTabSettings selectedSite={ selectedSite } /> );
await user.click( screen.getByRole( 'button', { name: 'Edit PHP version' } ) );
const dialog = screen.getByRole( 'dialog' );
expect( dialog ).toBeVisible();
await user.selectOptions(
within( dialog ).getByRole( 'combobox', {
name: 'PHP version',
} ),
'8.2'
);
await user.click(
within( dialog ).getByRole( 'button', {
name: 'Save',
} )
);
expect( updateSite ).toHaveBeenCalledWith( expect.objectContaining( { phpVersion: '8.2' } ) );
expect( stopServer ).toHaveBeenCalled();
expect( startServer ).toHaveBeenCalled();

( useGetPhpVersion as jest.Mock ).mockReturnValue( '8.2' );
rerender( <ContentTabSettings selectedSite={ selectedSite } /> );
expect( screen.getByText( '8.2' ) ).toBeVisible();
} );
} );
} );
Loading