From 44b32a92709ed30f53180cd18da9fa704c0d4aa3 Mon Sep 17 00:00:00 2001 From: Jules HABLOT Date: Mon, 8 Apr 2024 15:55:38 +0200 Subject: [PATCH] fix(@leav/ui): avoid interface error on empty metadata --- .../useCreateCancelConfirm.tsx | 3 + .../useGetRecordForm.test.tsx | 156 +++++++++++++++++- .../useGetRecordForm/useGetRecordForm.ts | 32 ++-- 3 files changed, 172 insertions(+), 19 deletions(-) diff --git a/libs/ui/src/components/RecordEdition/hooks/useCreateCancelConfirm/useCreateCancelConfirm.tsx b/libs/ui/src/components/RecordEdition/hooks/useCreateCancelConfirm/useCreateCancelConfirm.tsx index e9fe90cdc..1e6cbf1bc 100644 --- a/libs/ui/src/components/RecordEdition/hooks/useCreateCancelConfirm/useCreateCancelConfirm.tsx +++ b/libs/ui/src/components/RecordEdition/hooks/useCreateCancelConfirm/useCreateCancelConfirm.tsx @@ -1,3 +1,6 @@ +// Copyright LEAV Solutions 2017 +// This file is released under LGPL V3 +// License text available at https://www.gnu.org/licenses/lgpl-3.0.txt import {KitModal, KitSpace, KitTypography} from 'aristid-ds'; import {useSharedTranslation} from '_ui/hooks/useSharedTranslation'; export type UseCreateCancelConfirmHook = (onConfirm: () => void) => () => void; diff --git a/libs/ui/src/hooks/useGetRecordForm/useGetRecordForm.test.tsx b/libs/ui/src/hooks/useGetRecordForm/useGetRecordForm.test.tsx index d637fa108..941a54033 100644 --- a/libs/ui/src/hooks/useGetRecordForm/useGetRecordForm.test.tsx +++ b/libs/ui/src/hooks/useGetRecordForm/useGetRecordForm.test.tsx @@ -104,7 +104,7 @@ describe('useGetRecordForm', () => { version: {tree_1: {id: '1337', label: 'Some tree element'}} }), { - wrapper: ({children}) => {children as JSX.Element} + wrapper: ({children}) => {children} } ); @@ -156,4 +156,158 @@ describe('useGetRecordForm', () => { ] }); }); + + test('Run query and format result, with empty metadata', async () => { + const mocks = [ + { + request: { + query: RecordFormDocument, + variables: { + libraryId: 'test_lib', + recordId: '987654', + formId: 'edition', + version: [ + { + treeId: 'tree_1', + treeNodeId: '1337' + } + ] + } + }, + result: { + data: { + recordForm: { + id: 'edition', + recordId: '987654', + library: { + id: 'test_lib' + }, + elements: [ + { + id: '123456789', + containerId: '_root', + uiElementType: 'text_input', + attribute: { + ...mockFormAttribute, + values_list: null, + __typename: 'StandardAttribute' + }, + type: FormElementTypes.field, + valueError: null, + values: [ + { + __typename: 'Value', + id_value: '987654321', + created_at: 1234567890, + modified_at: 1234567890, + created_by: { + __typename: 'User', + id: '1', + whoAmI: mockRecord + }, + modified_by: { + __typename: 'User', + id: '1', + whoAmI: mockRecord + }, + metadata: [{value: null}], + version: [ + { + __typename: 'ValueVersion', + treeId: 'tree_1', + treeNode: { + id: '1337', + record: { + id: '1337', + whoAmI: { + id: '1337', + label: 'Some tree element', + library: { + id: 'test_lib' + } + } + } + } + } + ], + value: 'some value', + raw_value: 'some value' + } + ], + settings: [] + } + ] + } + } + } + } + ]; + + const {result} = renderHook( + () => + useGetRecordForm({ + libraryId: 'test_lib', + recordId: '987654', + formId: 'edition', + version: {tree_1: {id: '1337', label: 'Some tree element'}} + }), + { + wrapper: ({children}) => {children} + } + ); + + expect(result.current.loading).toBe(true); + + await waitFor(() => expect(result.current.loading).toBe(false)); + + expect(result.current.error).toBeUndefined(); + expect(result.current.recordForm).toMatchObject({ + id: 'edition', + recordId: '987654', + library: { + id: 'test_lib' + }, + elements: [ + { + id: '123456789', + containerId: '_root', + uiElementType: 'text_input', + type: FormElementTypes.field, + valueError: null, + attribute: {...mockFormAttribute, values_list: null}, + values: [ + { + id_value: '987654321', + created_at: 1234567890, + modified_at: 1234567890, + created_by: { + id: '1', + whoAmI: mockRecord + }, + modified_by: { + id: '1', + whoAmI: mockRecord + }, + metadata: [ + { + value: { + version: {} + } + } + ], + version: { + tree_1: { + id: '1337', + label: 'Some tree element' + } + }, + value: 'some value', + raw_value: 'some value' + } + ], + settings: [] + } + ] + }); + }); }); diff --git a/libs/ui/src/hooks/useGetRecordForm/useGetRecordForm.ts b/libs/ui/src/hooks/useGetRecordForm/useGetRecordForm.ts index 386da4563..0707d4950 100644 --- a/libs/ui/src/hooks/useGetRecordForm/useGetRecordForm.ts +++ b/libs/ui/src/hooks/useGetRecordForm/useGetRecordForm.ts @@ -111,24 +111,20 @@ const useGetRecordForm = ({ const recordFormFormatted: IRecordForm = { ...data.recordForm, elements: data.recordForm.elements.map( - (element): RecordFormElement => { - return { - ...element, - values: (element?.values ?? []).map(value => { - return { - ...value, - version: arrayValueVersionToObject(value.version ?? []), - metadata: (value.metadata ?? []).map(metadata => ({ - ...metadata, - value: { - ...metadata.value, - version: arrayValueVersionToObject(metadata.value.version ?? []) - } - })) - }; - }) - }; - } + (element): RecordFormElement => ({ + ...element, + values: (element?.values ?? []).map(value => ({ + ...value, + version: arrayValueVersionToObject(value.version ?? []), + metadata: (value.metadata ?? []).map(metadata => ({ + ...metadata, + value: { + ...metadata.value, + version: arrayValueVersionToObject(metadata.value?.version ?? []) + } + })) + })) + }) ) };