Skip to content

Commit

Permalink
fix(@leav/ui): avoid interface error on empty metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
P0ppoff committed Apr 12, 2024
1 parent 9c250ca commit 44b32a9
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
156 changes: 155 additions & 1 deletion libs/ui/src/hooks/useGetRecordForm/useGetRecordForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ describe('useGetRecordForm', () => {
version: {tree_1: {id: '1337', label: 'Some tree element'}}
}),
{
wrapper: ({children}) => <MockedProvider mocks={mocks}>{children as JSX.Element}</MockedProvider>
wrapper: ({children}) => <MockedProvider mocks={mocks}>{children}</MockedProvider>
}
);

Expand Down Expand Up @@ -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}) => <MockedProvider mocks={mocks}>{children}</MockedProvider>
}
);

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: []
}
]
});
});
});
32 changes: 14 additions & 18 deletions libs/ui/src/hooks/useGetRecordForm/useGetRecordForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? [])
}
}))
}))
})
)
};

Expand Down

0 comments on commit 44b32a9

Please sign in to comment.