diff --git a/src/packages/modules-operations/msd/pages/sims-creation/index.jsx b/src/packages/modules-operations/msd/pages/sims-creation/index.jsx index 925be1674..523a1d78a 100644 --- a/src/packages/modules-operations/msd/pages/sims-creation/index.jsx +++ b/src/packages/modules-operations/msd/pages/sims-creation/index.jsx @@ -33,6 +33,23 @@ import { Button } from '../../../../components/buttons/button'; const { RICH_TEXT } = rangeType; +export const generateSimsBeforeSubmit = ( + mode, + simsProp, + parentType, + idParent, + rubrics, +) => { + return { + id: mode !== DUPLICATE ? simsProp.id : '', + labelLg1: mode !== DUPLICATE ? simsProp.labelLg1 : '', + labelLg2: mode !== DUPLICATE ? simsProp.labelLg2 : '', + [getParentIdName(parentType)]: idParent, + created: mode !== DUPLICATE ? simsProp.created : '', + rubrics, + }; +}; + const convertRubric = (rubric) => { if (rubric.rangeType === 'RICH_TEXT') { return { @@ -96,21 +113,21 @@ const SimsCreation = ({ setSaving(true); const idParentToSave = idParent || idParentProp; - const rubrics = Object.values(sims).map(convertRubric); - const simsToSave = { - id: mode !== DUPLICATE ? simsProp.id : '', - labelLg1: mode !== DUPLICATE ? simsProp.labelLg1 : '', - labelLg2: mode !== DUPLICATE ? simsProp.labelLg2 : '', - [getParentIdName(parentType)]: idParentToSave, - created: mode !== DUPLICATE ? simsProp.created : '', - rubrics, - }; - onSubmit(simsToSave, (id) => { - setSaving(false); - goBack(`/operations/sims/${id}`); - }); + onSubmit( + generateSimsBeforeSubmit( + mode, + simsProp, + parentType, + idParentToSave, + rubrics, + ), + (id) => { + setSaving(false); + goBack(`/operations/sims/${id}`, true); + }, + ); }; const goBackUrl = sims.id diff --git a/src/packages/modules-operations/msd/pages/sims-creation/index.spec.tsx b/src/packages/modules-operations/msd/pages/sims-creation/index.spec.tsx new file mode 100644 index 000000000..0b55f31d0 --- /dev/null +++ b/src/packages/modules-operations/msd/pages/sims-creation/index.spec.tsx @@ -0,0 +1,83 @@ +import { vi, expect, describe, it, beforeEach, Mock } from 'vitest'; +import { generateSimsBeforeSubmit } from './index'; +import * as utils from '../../utils'; + +describe('generateSimsBeforeSubmit', () => { + beforeEach(() => { + vi.mock('../../utils', () => ({ + getParentIdName: vi.fn(), + })); + }); + + it('should generate the correct payload for CREATE mode', () => { + (utils.getParentIdName as Mock).mockReturnValue('parentId'); + + const result = generateSimsBeforeSubmit( + 'CREATE', + { + id: '123', + labelLg1: 'Label 1', + labelLg2: 'Label 2', + created: '2023-01-01', + }, + 'operation', + 'parent123', + { rubric1: 'value1' }, + ); + + expect(result).toEqual({ + id: '123', + labelLg1: 'Label 1', + labelLg2: 'Label 2', + parentId: 'parent123', + created: '2023-01-01', + rubrics: { rubric1: 'value1' }, + }); + }); + + it('should generate the correct payload for DUPLICATE mode', () => { + (utils.getParentIdName as Mock).mockReturnValue('parentId'); + + const result = generateSimsBeforeSubmit( + 'DUPLICATE', + { + id: '123', + labelLg1: 'Label 1', + labelLg2: 'Label 2', + created: '2023-01-01', + }, + 'series', + 'parent456', + { rubric2: 'value2' }, + ); + + expect(result).toEqual({ + id: '', + labelLg1: '', + labelLg2: '', + parentId: 'parent456', + created: '', + rubrics: { rubric2: 'value2' }, + }); + }); + + it('should use the correct parent ID name based on parent type', () => { + (utils.getParentIdName as Mock).mockReturnValue('idOperation'); + + const result = generateSimsBeforeSubmit( + 'UPDATE', + { + id: '123', + labelLg1: 'Label 1', + labelLg2: 'Label 2', + created: '2023-01-01', + }, + 'indicator', + 'parent789', + { rubric3: 'value3' }, + ); + + expect(result.idOperation).toBe('parent789'); + expect(utils.getParentIdName).toHaveBeenCalledWith('indicator'); + }); +});