Skip to content

Commit

Permalink
refactored unit tests, removed prop from golden recordlist
Browse files Browse the repository at this point in the history
  • Loading branch information
NyashaMuusha committed Jul 2, 2024
1 parent 26abc8f commit b4035cb
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 35 deletions.
1 change: 0 additions & 1 deletion JeMPI_Apps/JeMPI_UI/src/pages/settings/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ const Settings = () => {
Setup properties for Golden record lists
</Typography>
<GoldenRecordLists
goldenRecordList={configurationData?.additionalNodes || []}
/>
</CustomTabPanel>
<CustomTabPanel value={value} index={4}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState, FC } from 'react';
import { useEffect, useState} from 'react';
import Box from '@mui/material/Box';
import {
DataGrid,
Expand All @@ -16,7 +16,8 @@ import SaveIcon from '@mui/icons-material/Save';
import CancelIcon from '@mui/icons-material/Close';
import { EditToolbar } from 'components/shared/EditToolBar';
import { formatNodeName, toSnakeCase, toUpperCase } from 'utils/helpers';
import { Configuration } from 'types/Configuration';
import { Configuration, CustomNode } from 'types/Configuration';
import { useConfiguration } from 'hooks/useUIConfiguration';

interface RowData {
id: string;
Expand All @@ -28,33 +29,28 @@ interface RowData {
fieldIndex: number;
}

interface GoldenRecordListsProps {
goldenRecordList: any;
}

const GoldenRecordLists: FC<GoldenRecordListsProps> = ({ goldenRecordList }) => {
const GoldenRecordLists = () => {
const [rows, setRows] = useState<RowData[]>([]);
const [rowModesModel, setRowModesModel] = useState<GridRowModesModel>({});
const [, setConfiguration] = useState<Configuration>();
const { configuration, setConfiguration } = useConfiguration();

useEffect(() => {
if (goldenRecordList) {
const rowsWithIds = goldenRecordList.flatMap((node: { fields: any[]; nodeName: string }, nodeIndex: number) => {
return node.fields
? node.fields.map((field, fieldIndex) => ({
id: `${node.nodeName}_${nodeIndex}_${fieldIndex}`,
nodeName: node.nodeName,
fieldName: field.fieldName,
fieldType: field.fieldType,
csvCol: field.csvCol,
nodeIndex,
fieldIndex,
}))
: [];
if (configuration && configuration.additionalNodes) {
const rowsWithIds = configuration.additionalNodes.flatMap((node: CustomNode, nodeIndex: number) => {
return node.fields.map((field, fieldIndex) => ({
id: `${node.name}_${nodeIndex}_${fieldIndex}`,
nodeName: node.name,
fieldName: field.fieldName,
fieldType: field.fieldType,
csvCol: field.source?.csvCol ?? 0,
nodeIndex,
fieldIndex,
}));
});
setRows(rowsWithIds);
}
}, [goldenRecordList]);
}, [configuration]);

const handleEditClick = (id: GridRowId) => () => {
setRowModesModel((prevModel) => ({ ...prevModel, [id]: { mode: GridRowModes.Edit } }));
Expand All @@ -67,16 +63,16 @@ const GoldenRecordLists: FC<GoldenRecordListsProps> = ({ goldenRecordList }) =>
}
};



const handleUpdateConfiguration = (updatedRow: RowData, rowIndex: number) => {
const storedConfiguration = localStorage.getItem('configuration');
const currentConfiguration = storedConfiguration ? JSON.parse(storedConfiguration) : {};
const updatedConfiguration = getUpdatedConfiguration(updatedRow, rowIndex, currentConfiguration);
localStorage.setItem(
'configuration',
JSON.stringify(updatedConfiguration)
)
localStorage.setItem('configuration', JSON.stringify(updatedConfiguration));
setConfiguration(updatedConfiguration);
}
};

const getUpdatedConfiguration = (updatedRow: RowData, fieldIndex: number, currentConfig: Configuration): Configuration => {
const nodeIndex = updatedRow.nodeIndex;
const fieldName = toSnakeCase(updatedRow.fieldName);
Expand Down Expand Up @@ -125,6 +121,7 @@ const GoldenRecordLists: FC<GoldenRecordListsProps> = ({ goldenRecordList }) =>
return { ...updatedRow, id } as RowData;
};


const handleRowModesModelChange = (newRowModesModel: GridRowModesModel) => {
setRowModesModel(newRowModesModel);
};
Expand All @@ -135,6 +132,10 @@ const GoldenRecordLists: FC<GoldenRecordListsProps> = ({ goldenRecordList }) =>
}
};

const handleProcessRowUpdateError = (error: any) => {
console.error('Error during row update:', error);
};

const columns: GridColDef[] = [
{
field: 'Name',
Expand Down Expand Up @@ -235,7 +236,7 @@ const GoldenRecordLists: FC<GoldenRecordListsProps> = ({ goldenRecordList }) =>
},
}}
>
{goldenRecordList && (
{configuration && (
<DataGrid
rows={rows}
columns={columns}
Expand All @@ -244,6 +245,7 @@ const GoldenRecordLists: FC<GoldenRecordListsProps> = ({ goldenRecordList }) =>
onRowModesModelChange={handleRowModesModelChange}
onRowEditStop={handleRowEditStop}
processRowUpdate={processRowUpdate}
onProcessRowUpdateError={handleProcessRowUpdateError}
getRowId={(row) => row.id}
slots={{
toolbar: EditToolbar,
Expand Down
20 changes: 14 additions & 6 deletions JeMPI_Apps/JeMPI_UI/src/test/settings/GoldenRecordLists.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,27 @@ import userEvent from '@testing-library/user-event';
import mockData from 'services/mockData';
import '@testing-library/jest-dom';
import GoldenRecordLists from 'pages/settings/goldenRecordLists/GoldenRecordLists';
import { useConfiguration } from 'hooks/useUIConfiguration';

jest.mock('hooks/useUIConfiguration', () => ({
useConfiguration: jest.fn(),
}));

describe('GoldenRecordLists', () => {
const goldenRecordListsWithIds = mockData.configuration.auxGoldenRecordFields.map((row, index) => ({
...row,
id: `row_${index}`,
}));
const mockConfiguration = mockData.configuration.additionalNodes;

beforeEach(() => {
(useConfiguration as jest.Mock).mockReturnValue({
configuration: mockConfiguration,
setConfiguration: jest.fn(),
});
});
it('renders without crashing', () => {
render(<GoldenRecordLists goldenRecordList={goldenRecordListsWithIds} />);
render(<GoldenRecordLists />);
});

it('handles edit mode', async () => {
render(<GoldenRecordLists goldenRecordList={goldenRecordListsWithIds} />);
render(<GoldenRecordLists />);

const editIcon = await waitFor(() => document.getElementById('edit-button'));
const saveButton = await waitFor(() => document.getElementById('save-button'));
Expand Down
2 changes: 1 addition & 1 deletion JeMPI_Apps/JeMPI_UI/src/types/Configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export interface LinkMetaData {
m: number;
u: number;
}
export type FieldType = 'String' | 'DateTime' | 'Bool' | 'Number';

export type FieldType = 'String' | 'DateTime' | 'Bool' | 'Number';

export interface Field {
id?: string;
Expand Down

0 comments on commit b4035cb

Please sign in to comment.