Skip to content

Commit

Permalink
Rework tests to use msw and renderInTest
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanfranklin committed Nov 12, 2024
1 parent 0a157c1 commit 5861549
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 42 deletions.
5 changes: 4 additions & 1 deletion react/src/AppRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ function AppRouter() {
</ProtectedRoute>
}
/>
<Route path={ROUTES.PUBLIC_PROJECT} element={<MapProject isPublicView />} />
<Route
path={ROUTES.PUBLIC_PROJECT}
element={<MapProject isPublicView />}
/>
<Route path={ROUTES.CALLBACK} element={<Callback />} />
<Route
path={ROUTES.STREETVIEW_CALLBACK}
Expand Down
69 changes: 30 additions & 39 deletions react/src/components/FeatureFileTree/FeatureFileTree.test.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import React from 'react';
import { render, fireEvent } from '@testing-library/react';
import { MemoryRouter } from 'react-router-dom';
import { fireEvent, waitFor } from '@testing-library/react';
import { http, HttpResponse } from 'msw';
import FeatureFileTree from './FeatureFileTree';
import { server, renderInTest } from '@hazmapper/test/testUtil';
import { featureCollection } from '@hazmapper/__fixtures__/featuresFixture';
import { useDeleteFeature } from '@hazmapper/hooks';

// Mock the hooks
jest.mock('@hazmapper/hooks', () => ({
useDeleteFeature: jest.fn(),
}));
import { testDevConfiguration } from '@hazmapper/__fixtures__/appConfigurationFixture';

jest.mock('react-resize-detector', () => ({
useResizeDetector: () => ({
Expand All @@ -17,64 +13,59 @@ jest.mock('react-resize-detector', () => ({
}),
}));

const renderWithRouter = (ui: React.ReactElement, { route = '/' } = {}) => {
return {
...render(<MemoryRouter initialEntries={[route]}>{ui}</MemoryRouter>),
};
};

describe('FeatureFileTree', () => {
const defaultProps = {
const defaultTreeProps = {
featureCollection: featureCollection,
isPublicView: false,
projectId: 1,
};

beforeEach(() => {
jest.clearAllMocks();

(useDeleteFeature as jest.Mock).mockImplementation(() => ({
mutate: jest.fn(),
isLoading: false,
}));
});

it('renders feature list correctly', () => {
const { getByText } = renderWithRouter(
<FeatureFileTree {...defaultProps} />
const { getByText } = renderInTest(
<FeatureFileTree {...defaultTreeProps} />
);

expect(getByText('foo')).toBeDefined();
expect(getByText('image1.JPG')).toBeDefined();
expect(getByText('image2.JPG')).toBeDefined();
});

it('handles feature deletion for non-public projects', () => {
const deleteFeatureMock = jest.fn();
(useDeleteFeature as jest.Mock).mockImplementation(() => ({
mutate: deleteFeatureMock,
isLoading: false,
}));
it('handles feature deletion for non-public projects', async () => {
const featureId = 1;
let wasDeleted = false;

server.use(
http.delete(
`${testDevConfiguration.geoapiUrl}/projects/${defaultTreeProps.projectId}/features/${featureId}/`,
() => {
wasDeleted = true;
return HttpResponse.json({}, { status: 200 });
}
)
);

const { getByTestId } = renderWithRouter(
<FeatureFileTree {...defaultProps} />,
{ route: '/?selectedFeature=1' }
const { getByTestId } = renderInTest(
<FeatureFileTree {...defaultTreeProps} />,
`/?selectedFeature=${featureId}`
);

// Find and click delete button (as featured is selected)
const deleteButton = getByTestId('delete-feature-button');
fireEvent.click(deleteButton);

expect(deleteFeatureMock).toHaveBeenCalledWith({
projectId: 1,
featureId: 1,
await waitFor(() => {
expect(wasDeleted).toBeTruthy();
});
});

it('does not show delete button for public projects', () => {
const { queryByTestId } = renderWithRouter(
<FeatureFileTree {...defaultProps} isPublicView={true} />,
{ route: '/?selectedFeature=1' }
const { queryByTestId } = renderInTest(
<FeatureFileTree {...defaultTreeProps} isPublicView={true} />,
'/?selectedFeature=1'
);

// Verify delete button is not present
Expand All @@ -83,8 +74,8 @@ describe('FeatureFileTree', () => {
});

it('does not show delete button when no feature is selected', () => {
const { queryByTestId } = renderWithRouter(
<FeatureFileTree {...defaultProps} isPublicView={false} />
const { queryByTestId } = renderInTest(
<FeatureFileTree {...defaultTreeProps} isPublicView={false} />
);

// Verify delete button is not present
Expand Down
4 changes: 3 additions & 1 deletion react/src/components/MapProjectNavBar/MapProjectNavBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ interface NavBarPanelProps {
isPublicView?: boolean;
}

const MapProjectNavBar: React.FC<NavBarPanelProps> = ({ isPublicView = false }) => {
const MapProjectNavBar: React.FC<NavBarPanelProps> = ({
isPublicView = false,
}) => {
const location = useLocation();
const queryParams = new URLSearchParams(location.search);
const activePanel = queryParams.get(queryPanelKey);
Expand Down
7 changes: 6 additions & 1 deletion react/src/pages/MapProject/MapProject.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,12 @@ const MapProject: React.FC<MapProjectProps> = ({ isPublicView = false }) => {
);
}

return <LoadedMapProject isPublicView={isPublicView} activeProject={activeProject} />;
return (
<LoadedMapProject
isPublicView={isPublicView}
activeProject={activeProject}
/>
);
};

interface LoadedMapProject {
Expand Down
3 changes: 3 additions & 0 deletions react/src/test/testUtil.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export const testQueryClient = new QueryClient({
staleTime: 0,
useErrorBoundary: true,
},
mutations: {
retry: false,
},
},
});

Expand Down

0 comments on commit 5861549

Please sign in to comment.