Skip to content

Commit

Permalink
[O3-4096]-Add tests for deleting configurations in a package (#17)
Browse files Browse the repository at this point in the history
* Add tests for deleting configurations in a package

* test successful deletion of configurations

* Fix Schema Update on Configuration Deletion
  • Loading branch information
TrevorAntony authored Nov 7, 2024
1 parent fa2b1c8 commit a624b0a
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import '@testing-library/jest-dom';
import DeleteConfigDetailModal from './delete-config-detail-modal.component';
import { WidgetTypes } from '../../types';
import userEvent from '@testing-library/user-event';
import { showSnackbar } from '@openmrs/esm-framework';

jest.mock('@openmrs/esm-framework', () => ({
showSnackbar: jest.fn(),
Expand Down Expand Up @@ -93,4 +94,134 @@ describe('DeleteConfigDetailModal', () => {
await user.click(screen.getByText('cancel'));
expect(mockCloseModal).toHaveBeenCalled();
});

it('successfully deletes configuration and updates schema when confirm button is clicked', async () => {
const user = userEvent.setup();
render(<DeleteConfigDetailModal {...mockProps} />);

await user.click(screen.getByText('deleteConfiguration'));
expect(mockOnSchemaChange).toHaveBeenCalledTimes(1);
expect(mockOnSchemaChange).toHaveBeenCalledWith({
'@openmrs/esm-patient-chart-app': {
extensionSlots: {
'patient-chart-dashboard-slot': {
configure: {
configKey1: {
title: 'Sample Title',
slotName: 'sample-slot',
isExpanded: true,
tabDefinitions: [
{
id: 'tab1',
tabName: 'tab1',
headerTitle: 'Header 1',
displayText: 'Tab 1',
encounterType: 'encounter1',
columns: [],
launchOptions: { displayText: 'Launch' },
formList: [],
},
],
},
},
},
slot1: {
configure: {
configKey1: {
'encounter-list-table-tabs': undefined,
slotName: 'slot1',
title: 'Another Title',
widgetType1: [
{
tabName: 'tab1',
},
],
},
},
},
},
},
});
});

it('displays correct configuration details before deletion', () => {
render(<DeleteConfigDetailModal {...mockProps} />);

expect(screen.getByText('menuSlot : slot1')).toBeInTheDocument();
expect(screen.getByText('tabName : tab1')).toBeInTheDocument();
expect(screen.getByText('headerTitle : Header 1')).toBeInTheDocument();
expect(screen.getByText('deleteConfiguration')).toBeEnabled();
});

it('shows error snackbar when deletion fails', async () => {
const user = userEvent.setup();
const errorProps = {
...mockProps,
onSchemaChange: jest.fn().mockImplementation(() => {
throw new Error('Failed to delete configuration');
}),
};

render(<DeleteConfigDetailModal {...errorProps} />);

await user.click(screen.getByText('deleteConfiguration'));
expect(showSnackbar).toHaveBeenCalledWith({
title: 'errorDeletingConfiguration',
kind: 'error',
subtitle: 'Failed to delete configuration',
});
expect(mockCloseModal).toHaveBeenCalled();
});

it('successfully deletes configuration and updates schema when confirm button is clicked', async () => {
const user = userEvent.setup();
render(<DeleteConfigDetailModal {...mockProps} />);

await user.click(screen.getByText('deleteConfiguration'));

const expectedSchema = {
'@openmrs/esm-patient-chart-app': {
extensionSlots: {
'patient-chart-dashboard-slot': {
configure: {
configKey1: {
title: 'Sample Title',
slotName: 'sample-slot',
isExpanded: true,
tabDefinitions: [
{
id: 'tab1',
tabName: 'tab1',
headerTitle: 'Header 1',
displayText: 'Tab 1',
encounterType: 'encounter1',
columns: [],
launchOptions: { displayText: 'Launch' },
formList: [],
},
],
},
},
},
slot1: {
configure: {
configKey1: {
title: 'Another Title',
slotName: 'slot1',
widgetType1: [{ tabName: 'tab1' }],
},
},
},
},
},
};
expect(mockOnSchemaChange).toHaveBeenCalledWith(expectedSchema);
expect(showSnackbar).toHaveBeenCalledWith({
title: 'success',
kind: 'success',
isLowContrast: true,
subtitle: 'tabConfigurationDeleted'
});
expect(mockCloseModal).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,16 @@ const DeleteConfigDetailModal: React.FC<DeleteConfigDetailModalProps> = ({
...schemaBasePath.extensionSlots[slotDetails?.slot]?.configure,
[configurationKey]: {
...schemaBasePath.extensionSlots[slotDetails?.slot]?.configure[configurationKey],
[widgetType]: schemaBasePath.extensionSlots[slotDetails?.slot]?.configure[configurationKey]?.[
widgetType
]?.filter((def) => {
const retainedConfigs = def.tabName !== tabDefinition.tabName;
return retainedConfigs;
}),
},
},
} as ExtensionSlot,
},
},
};

delete updatedSchema['@openmrs/esm-patient-chart-app'].extensionSlots[slotDetails?.slot]
.configure[configurationKey][widgetType];

onSchemaChange(updatedSchema);
showSnackbar({
title: t('success', 'Success!'),
Expand Down

0 comments on commit a624b0a

Please sign in to comment.