Skip to content

Commit

Permalink
Merge pull request #1007 from InseeFr/fix/sims
Browse files Browse the repository at this point in the history
fix: add unit test
  • Loading branch information
PierreVasseur authored Oct 8, 2024
2 parents 27e7dda + ae562c4 commit 47fa2e7
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 13 deletions.
43 changes: 30 additions & 13 deletions src/packages/modules-operations/msd/pages/sims-creation/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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');
});
});

0 comments on commit 47fa2e7

Please sign in to comment.