Skip to content

Commit

Permalink
Merge branch 'main' into task/WG-401-Add-Asset-to-Feature
Browse files Browse the repository at this point in the history
  • Loading branch information
sophia-massie committed Jan 27, 2025
2 parents 4813e9e + 8e8c6de commit 3b67fc8
Show file tree
Hide file tree
Showing 37 changed files with 1,318 additions and 434 deletions.
10 changes: 9 additions & 1 deletion react/jest.setup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { testDevConfiguration } from '@hazmapper/__fixtures__/appConfigurationFixture';
import { server } from '@hazmapper/test/testUtil';
import { server, testQueryClient } from '@hazmapper/test/testUtil';

/***** A) Setup the configuration used for unit testing *****/
jest.mock('@hazmapper/hooks/environment/getLocalAppConfiguration', () => ({
Expand Down Expand Up @@ -56,6 +56,9 @@ export function shouldIgnoreError(args: any[]): boolean {
/***** C) Setup testing and also ensure that we are mocking things in tests *****/

beforeAll(() => {
// Uncomment next line to see all handlers
// console.log('Registered handlers:', server.listHandlers());

// Establish mocking of APIs before all tests
server.listen({
onUnhandledRequest: 'error',
Expand All @@ -81,6 +84,11 @@ beforeAll(() => {
});
});

// Clear the React Query cache before each test
beforeEach(() => {
testQueryClient.clear();
});

// Reset any runtime request handlers we may add during the tests
afterEach(() => server.resetHandlers());

Expand Down
297 changes: 147 additions & 150 deletions react/package-lock.json

Large diffs are not rendered by default.

130 changes: 64 additions & 66 deletions react/src/__fixtures__/projectFixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,82 +5,80 @@ import {
ProjectRequest,
} from '../types';

export const designSafeProjectMock: DesignSafeProject = {
uuid: 'proj-uuid',
name: 'designsafe.project',
created: '2019-10-31T16:04:26.327Z',
lastUpdated: '2019-10-31T16:04:30.163Z',
associationIds: [],
value: {
dois: [],
coPis: [],
title: 'Sample DesignSafe Project',
users: [
{
inst: 'University of Texas at Austin (utexas.edu)',
role: 'pi',
email: '[email protected]',
fname: 'Fixture First Name',
lname: 'Fixture Last Name',
username: 'fixture1Username',
},
{
inst: 'University of Texas at Austin (utexas.edu)',
role: 'co_pi',
email: '[email protected]',
fname: 'Tester',
lname: 'Test',
username: 'fixture2Username',
},
],
authors: [],
frTypes: [],
nhEvent: '',
nhTypes: [],
fileObjs: [],
fileTags: [],
keywords: [],
nhEvents: [],
dataTypes: [],
projectId: 'proj-id',
tombstone: false,
facilities: [],
nhLatitude: '',
nhLocation: '',
description: 'Map Test description required.',
nhLongitude: '',
projectType: 'None',
teamMembers: [],
awardNumbers: [],
guestMembers: [],
hazmapperMaps: [
{
name: 'Hazmapper_TestProject',
path: '/',
uuid: '620aeaf4-f813-4b90-ba52-bc87cfa7b07b',
deployment: 'production',
},
],
referencedData: [],
associatedProjects: [],
},
};

export const projectMock: Project = {
id: 1,
uuid: 'abc123',
name: 'Sample Project',
description: 'A sample project for testing purposes.',
public: true,
system_file: 'sample-file',
system_id: 'sample-id',
system_id:
'project-1234' /* 'project-' prefix implies its a DesignSafe project system */,
system_path: '/path/to/sample',
deletable: true,
streetview_instances: null,
ds_project: {
uuid: 'proj-uuid',
projectId: 'proj-id',
title: 'Sample DesignSafe Project',
value: {
dois: [],
coPis: [],
title: 'Hazmapper V3 PROD Map Test 2024.08.07',
users: [
{
inst: 'University of Texas at Austin (utexas.edu)',
role: 'pi',
email: '[email protected]',
fname: 'Fixture First Name',
lname: 'Fixture Last Name',
username: 'fixture1Username',
},
{
inst: 'University of Texas at Austin (utexas.edu)',
role: 'co_pi',
email: '[email protected]',
fname: 'Tester',
lname: 'Test',
username: 'fixture2Username',
},
],
authors: [],
frTypes: [],
nhEvent: '',
nhTypes: [],
fileObjs: [],
fileTags: [],
keywords: [],
nhEvents: [],
dataTypes: [],
projectId: 'PRJ-5566',
tombstone: false,
facilities: [],
nhLatitude: '',
nhLocation: '',
description: 'Map Test description required.',
nhLongitude: '',
projectType: 'None',
teamMembers: [],
awardNumbers: [],
guestMembers: [],
hazmapperMaps: [
{
name: 'Hazmapper_TestProject',
path: '/',
uuid: '620aeaf4-f813-4b90-ba52-bc87cfa7b07b',
deployment: 'production',
},
],
referencedData: [],
associatedProjects: [],
},
},
};

export const designSafeProjectMock: DesignSafeProject = {
uuid: 'proj-uuid',
projectId: 'proj-id',
title: 'Sample DesignSafe Project',
value: {},
ds_project: designSafeProjectMock,
};

export const designSafeProjectCollectionMock: DesignSafeProjectCollection = {
Expand Down
1 change: 1 addition & 0 deletions react/src/__fixtures__/usersFixtures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const users = [{ id: 1, username: 'user' }];
45 changes: 43 additions & 2 deletions react/src/components/AssetDetail/AssetDetail.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import React from 'react';
import { render, screen, act } from '@testing-library/react';
import AssetDetail from './AssetDetail';
import { mockImgFeature } from '@hazmapper/__fixtures__/featuresFixture';
import {
mockImgFeature,
mockPointFeature,
} from '@hazmapper/__fixtures__/featuresFixture';
import AssetGeometry from './AssetGeometry';

jest.mock('@hazmapper/hooks', () => ({
Expand All @@ -25,7 +28,7 @@ describe('AssetDetail', () => {
onQuestionnaireClick: jest.fn(),
};

it('renders all main components', async () => {
it('renders all main components for image feature', async () => {
const { getByText } = render(<AssetDetail {...AssetModalProps} />);
const assetGeometry = screen.getByTestId('asset-geometry');
await act(async () => {
Expand All @@ -37,4 +40,42 @@ describe('AssetDetail', () => {
expect(getByText('Metadata')).toBeDefined();
expect(assetGeometry).toBeDefined();
});

it('renders all main components for point feature', async () => {
const { getByText } = render(
<AssetDetail {...AssetModalProps} selectedFeature={mockPointFeature} />
);
const assetGeometry = screen.getByTestId('asset-geometry');
await act(async () => {
render(<AssetGeometry selectedFeature={mockPointFeature} />);
});

// Check for standard components
expect(getByText('Metadata')).toBeDefined();
expect(assetGeometry).toBeDefined();

// Check for DesignSafe button presence when not in public view
expect(getByText('Add Asset from DesignSafe')).toBeDefined();
});

it('renders all main components for point feature public view', async () => {
const { getByText, queryByText } = render(
<AssetDetail
{...AssetModalProps}
selectedFeature={mockPointFeature}
isPublicView={true}
/>
);
const assetGeometry = screen.getByTestId('asset-geometry');
await act(async () => {
render(<AssetGeometry selectedFeature={mockPointFeature} />);
});

// Check for standard components
expect(getByText('Metadata')).toBeDefined();
expect(assetGeometry).toBeDefined();

// Verify some comopnents are not present in public view
expect(queryByText('Add Asset from DesignSafe')).toBeNull();
});
});
54 changes: 35 additions & 19 deletions react/src/components/FeatureFileTree/FeatureFileTree.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { fireEvent, waitFor } from '@testing-library/react';
import { fireEvent, waitFor, act } from '@testing-library/react';
import { http, HttpResponse } from 'msw';
import FeatureFileTree from './FeatureFileTree';
import { server, renderInTest } from '@hazmapper/test/testUtil';
Expand All @@ -24,11 +24,13 @@ describe('FeatureFileTree', () => {
jest.clearAllMocks();
});

it('renders feature list correctly', () => {
const { getByText } = renderInTest(
<FeatureFileTree {...defaultTreeProps} />
);
it('renders feature list correctly', async () => {
let rendered;
await act(async () => {
rendered = renderInTest(<FeatureFileTree {...defaultTreeProps} />);
});

const { getByText } = rendered;
expect(getByText('foo')).toBeDefined();
expect(getByText('image1.JPG')).toBeDefined();
expect(getByText('image2.JPG')).toBeDefined();
Expand All @@ -48,37 +50,51 @@ describe('FeatureFileTree', () => {
)
);

const { getByTestId } = renderInTest(
<FeatureFileTree {...defaultTreeProps} />,
`/?selectedFeature=${featureId}`
);
let rendered;
await act(async () => {
rendered = renderInTest(
<FeatureFileTree {...defaultTreeProps} />,
`/?selectedFeature=${featureId}`
);
});

// Find and click delete button (as featured is selected)
const { getByTestId } = rendered;
const deleteButton = getByTestId('delete-feature-button');
fireEvent.click(deleteButton);
await act(async () => {
fireEvent.click(deleteButton);
});

await waitFor(() => {
expect(wasDeleted).toBeTruthy();
});
});

it('does not show delete button for public projects', () => {
const { queryByTestId } = renderInTest(
<FeatureFileTree {...defaultTreeProps} isPublicView={true} />,
'/?selectedFeature=1'
);
it('does not show delete button for public projects', async () => {
let rendered;
await act(async () => {
rendered = renderInTest(
<FeatureFileTree {...defaultTreeProps} isPublicView={true} />,
'/?selectedFeature=1'
);
});

// Verify delete button is not present
const { queryByTestId } = rendered;
const deleteButton = queryByTestId('delete-feature-button');
expect(deleteButton).toBeNull();
});

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

// Verify delete button is not present
const { queryByTestId } = rendered;
const deleteButton = queryByTestId('delete-feature-button');
expect(deleteButton).toBeNull();
});
Expand Down
14 changes: 7 additions & 7 deletions react/src/components/Files/FileListing.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { FileListing } from './FileListing';
import {
useFiles,
useAuthenticatedUser,
useDsProjects,
useDesignSafeProjects,
useSystems,
} from '../../hooks';
import { serializeToChonkyFile } from '../../utils/fileUtils';
Expand All @@ -16,7 +16,7 @@ jest.mock('../../hooks', () => ({
data: [],
refetch: jest.fn(),
})),
useDsProjects: jest.fn(() => ({ result: [] })),
useDesignSafeProjects: jest.fn(() => ({ result: [] })),
useAuthenticatedUser: jest.fn(() => ({ data: { username: 'test-user' } })),
useSystems: jest.fn(() => ({ data: [] })),
}));
Expand All @@ -43,7 +43,7 @@ describe('FileListing', () => {

it('renders without crashing and displays "No systems available" if no systems are returned', () => {
(useSystems as jest.Mock).mockReturnValue({ data: [], myDataSystem: null });
(useDsProjects as jest.Mock).mockReturnValue({
(useDesignSafeProjects as jest.Mock).mockReturnValue({
result: [],
});
(useAuthenticatedUser as jest.Mock).mockReturnValue({
Expand All @@ -66,7 +66,7 @@ describe('FileListing', () => {
data: [],
refetch: jest.fn(),
});
(useDsProjects as jest.Mock).mockReturnValue({
(useDesignSafeProjects as jest.Mock).mockReturnValue({
data: [],
});
(useAuthenticatedUser as jest.Mock).mockReturnValue({
Expand All @@ -90,7 +90,7 @@ describe('FileListing', () => {
data: [],
refetch: jest.fn(),
});
(useDsProjects as jest.Mock).mockReturnValue({
(useDesignSafeProjects as jest.Mock).mockReturnValue({
data: [],
});
(useAuthenticatedUser as jest.Mock).mockReturnValue({
Expand Down Expand Up @@ -131,7 +131,7 @@ describe('FileListing', () => {
refetch: jest.fn(),
});

(useDsProjects as jest.Mock).mockReturnValue({
(useDesignSafeProjects as jest.Mock).mockReturnValue({
data: [],
});

Expand Down Expand Up @@ -175,7 +175,7 @@ describe('FileListing', () => {
refetch: jest.fn(),
});

(useDsProjects as jest.Mock).mockReturnValue({
(useDesignSafeProjects as jest.Mock).mockReturnValue({
data: [],
});

Expand Down
Loading

0 comments on commit 3b67fc8

Please sign in to comment.