Skip to content

Commit f445361

Browse files
authored
Merge pull request #1165 from ral-facilities/modify-catalogue-item-edit-error-messages-when-has-children-#1136
Modify dialogue error messages when editing a catalogue item that has child elements #1136
2 parents e153a36 + 5c5a543 commit f445361

File tree

4 files changed

+81
-5
lines changed

4 files changed

+81
-5
lines changed

cypress/e2e/with_mock_data/catalogueItems.cy.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,15 +603,32 @@ describe('Catalogue Items', () => {
603603

604604
cy.findByLabelText('Name *').clear();
605605
cy.findByLabelText('Name *').type('test_has_children_elements');
606+
607+
cy.findByLabelText('Manufacturer *').type('Man{downArrow}{enter}');
608+
cy.findByRole('button', { name: 'Next' }).click();
609+
610+
cy.findByRole('button', { name: 'Finish'}).click();
611+
cy.findByRole('dialog')
612+
.should('be.visible')
613+
.within(() => {
614+
cy.contains('Unable to update catalogue item properties and manufacturer '
615+
+ '(Manufacturer C), as the catalogue item has associated items.')
616+
});
617+
cy.findByRole('button', { name: 'Finish' }).should('be.disabled');
618+
619+
cy.findByRole('button', { name: 'Back' }).click();
620+
cy.findByLabelText('Manufacturer *').type('Man{upArrow}{enter}');
606621
cy.findByRole('button', { name: 'Next' }).click();
622+
cy.findByRole('button', { name: 'Finish' }).should('be.enabled');
607623

608624
cy.findByLabelText('Measurement Range (Joules) *').type('0');
609625

610626
cy.findByRole('button', { name: 'Finish' }).click();
611627
cy.findByRole('dialog')
612628
.should('be.visible')
613629
.within(() => {
614-
cy.contains('Catalogue item has child elements and cannot be edited');
630+
cy.contains('Unable to update catalogue item properties and manufacturer '
631+
+ '(Manufacturer C), as the catalogue item has associated items.')
615632
});
616633
cy.findByRole('button', { name: 'Finish' }).should('be.disabled');
617634
});

src/catalogue/items/catalogueItemsDialog.component.test.tsx

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,39 @@ describe('Catalogue Items Dialog', () => {
10841084
});
10851085
});
10861086

1087-
it('displays error message if catalogue item has children elements', async () => {
1087+
it('displays error message when editing manufacturer_id if catalogue item has child elements', async () => {
1088+
props = {
1089+
...props,
1090+
parentInfo: getCatalogueCategoryById('4'),
1091+
selectedCatalogueItem: getCatalogueItemById('1'),
1092+
};
1093+
1094+
createView();
1095+
1096+
await modifyValues({
1097+
name: 'test_has_children_elements',
1098+
manufacturer: 'Man{arrowdown}{arrowdown}{enter}',
1099+
});
1100+
1101+
await user.click(screen.getByRole('button', { name: 'Next' }));
1102+
await user.click(screen.getByRole('button', { name: 'Finish' }));
1103+
1104+
expect(axiosPatchSpy).toHaveBeenCalledWith('/v1/catalogue-items/1', {
1105+
name: 'test_has_children_elements',
1106+
manufacturer_id: '3',
1107+
});
1108+
1109+
await waitFor(() => {
1110+
expect(
1111+
screen.getByText(
1112+
'Unable to update catalogue item properties and manufacturer '
1113+
+ '(Manufacturer A), as the catalogue item has associated items.'
1114+
)
1115+
).toBeInTheDocument();
1116+
});
1117+
});
1118+
1119+
it('displays error message when editing properties if catalogue item has child elements', async () => {
10881120
props = {
10891121
...props,
10901122
parentInfo: getCatalogueCategoryById('4'),
@@ -1098,16 +1130,30 @@ describe('Catalogue Items Dialog', () => {
10981130
});
10991131

11001132
await user.click(screen.getByRole('button', { name: 'Next' }));
1133+
1134+
await modifyValues({
1135+
resolution: '24',
1136+
});
1137+
11011138
await user.click(screen.getByRole('button', { name: 'Finish' }));
11021139

11031140
expect(axiosPatchSpy).toHaveBeenCalledWith('/v1/catalogue-items/1', {
11041141
name: 'test_has_children_elements',
1142+
properties: [
1143+
{ id: '1', value: 24 },
1144+
{ id: '2', value: 30 },
1145+
{ id: '3', value: 'CMOS' },
1146+
{ id: '4', value: null },
1147+
{ id: '5', value: true },
1148+
{ id: '6', value: false },
1149+
],
11051150
});
11061151

11071152
await waitFor(() => {
11081153
expect(
11091154
screen.getByText(
1110-
'Catalogue item has child elements and cannot be edited'
1155+
'Unable to update catalogue item properties and manufacturer '
1156+
+ '(Manufacturer A), as the catalogue item has associated items.'
11111157
)
11121158
).toBeInTheDocument();
11131159
});

src/catalogue/items/catalogueItemsDialog.component.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,15 @@ function CatalogueItemsDialog(props: CatalogueItemsDialogProps) {
386386

387387
if (response && error.response?.status === 409) {
388388
if (response.detail.includes('child elements')) {
389+
// find the name of the manufacturer, so it can be used in the error message
390+
const manufacturerName = manufacturerList?.find(
391+
(manufacturer) => manufacturer.id === selectedCatalogueItem?.manufacturer_id
392+
) || null;
393+
// add the manufacturer name into the error message
394+
const childElementsMessage = "Unable to update catalogue item properties and manufacturer ("
395+
+ manufacturerName?.name + "), as the catalogue item has associated items.";
389396
setErrorPropertiesStep('root.formError', {
390-
message: response.detail,
397+
message: childElementsMessage,
391398
});
392399
}
393400
return;
@@ -407,6 +414,7 @@ function CatalogueItemsDialog(props: CatalogueItemsDialogProps) {
407414
patchCatalogueItem,
408415
handleClose,
409416
setErrorPropertiesStep,
417+
manufacturerList,
410418
]
411419
);
412420

src/mocks/handlers.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,9 +386,14 @@ export const handlers = [
386386
};
387387

388388
if (body.name === 'test_has_children_elements') {
389+
// find the name of the manufacturer, so it can be used in the error message
390+
const manufacturerName = ManufacturersJSON?.find(
391+
(manufacturer) => manufacturer.id === validCatalogueItem?.manufacturer_id
392+
) as Manufacturer;
389393
return HttpResponse.json(
390394
{
391-
detail: 'Catalogue item has child elements and cannot be edited',
395+
detail: 'Unable to update catalogue item properties and manufacturer ('
396+
+ manufacturerName?.name + '), as the catalogue item has child elements.'
392397
},
393398
{ status: 409 }
394399
);

0 commit comments

Comments
 (0)