4087 refactor object metadata item hooks and utils (#4861)
- Extracted each exported element from useObjectMetadataItem into its own hook.
This commit is contained in:
@ -1,21 +0,0 @@
|
||||
import { renderHook } from '@testing-library/react';
|
||||
|
||||
import { useGetObjectOrderByField } from '@/object-metadata/hooks/useGetObjectOrderByField';
|
||||
import { getObjectMetadataItemsMock } from '@/object-metadata/utils/getObjectMetadataItemsMock';
|
||||
|
||||
const mockObjectMetadataItems = getObjectMetadataItemsMock();
|
||||
|
||||
describe('useGetObjectOrderByField', () => {
|
||||
it('should work as expected', () => {
|
||||
const objectMetadataItem = mockObjectMetadataItems.find(
|
||||
(item) => item.nameSingular === 'person',
|
||||
)!;
|
||||
|
||||
const { result } = renderHook(() =>
|
||||
useGetObjectOrderByField({ objectMetadataItem })('AscNullsLast'),
|
||||
);
|
||||
expect(result.current).toEqual({
|
||||
name: { firstName: 'AscNullsLast', lastName: 'AscNullsLast' },
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,33 @@
|
||||
import { ReactNode } from 'react';
|
||||
import { MockedProvider } from '@apollo/client/testing';
|
||||
import { renderHook } from '@testing-library/react';
|
||||
import { RecoilRoot } from 'recoil';
|
||||
|
||||
import { useGetObjectOrderByField } from '@/object-metadata/hooks/useGetObjectOrderByField';
|
||||
|
||||
const Wrapper = ({ children }: { children: ReactNode }) => (
|
||||
<RecoilRoot>
|
||||
<MockedProvider addTypename={false}>{children}</MockedProvider>
|
||||
</RecoilRoot>
|
||||
);
|
||||
|
||||
describe('useGetObjectOrderByField', () => {
|
||||
it('should work as expected', () => {
|
||||
const { result } = renderHook(
|
||||
() => {
|
||||
const { getObjectOrderByField } = useGetObjectOrderByField({
|
||||
objectNameSingular: 'person',
|
||||
});
|
||||
|
||||
return getObjectOrderByField('AscNullsLast');
|
||||
},
|
||||
{
|
||||
wrapper: Wrapper,
|
||||
},
|
||||
);
|
||||
|
||||
expect(result.current).toEqual({
|
||||
name: { firstName: 'AscNullsLast', lastName: 'AscNullsLast' },
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -2,21 +2,19 @@ import { renderHook } from '@testing-library/react';
|
||||
import { RecoilRoot } from 'recoil';
|
||||
|
||||
import { useMapToObjectRecordIdentifier } from '@/object-metadata/hooks/useMapToObjectRecordIdentifier';
|
||||
import { getObjectMetadataItemsMock } from '@/object-metadata/utils/getObjectMetadataItemsMock';
|
||||
|
||||
const mockObjectMetadataItems = getObjectMetadataItemsMock();
|
||||
|
||||
describe('useMapToObjectRecordIdentifier', () => {
|
||||
it('should work as expected', async () => {
|
||||
const { result } = renderHook(
|
||||
() => {
|
||||
const objectMetadataItem = mockObjectMetadataItems.find(
|
||||
(item) => item.nameSingular === 'person',
|
||||
)!;
|
||||
const { mapToObjectRecordIdentifier } = useMapToObjectRecordIdentifier({
|
||||
objectNameSingular: 'person',
|
||||
});
|
||||
|
||||
return useMapToObjectRecordIdentifier({
|
||||
objectMetadataItem,
|
||||
})({ id: 'id', name: { firstName: 'Sheldon', lastName: 'Cooper' } });
|
||||
return mapToObjectRecordIdentifier({
|
||||
id: 'id',
|
||||
name: { firstName: 'Sheldon', lastName: 'Cooper' },
|
||||
});
|
||||
},
|
||||
{
|
||||
wrapper: RecoilRoot,
|
||||
|
||||
@ -11,6 +11,7 @@ const Wrapper = ({ children }: { children: ReactNode }) => (
|
||||
</RecoilRoot>
|
||||
);
|
||||
|
||||
// Split into tests for each new hook
|
||||
describe('useObjectMetadataItem', () => {
|
||||
it('should return correct properties', async () => {
|
||||
const { result } = renderHook(
|
||||
@ -20,39 +21,8 @@ describe('useObjectMetadataItem', () => {
|
||||
},
|
||||
);
|
||||
|
||||
const {
|
||||
basePathToShowPage,
|
||||
objectMetadataItem,
|
||||
labelIdentifierFieldMetadata,
|
||||
getRecordFromCache,
|
||||
findManyRecordsQuery,
|
||||
findOneRecordQuery,
|
||||
createOneRecordMutation,
|
||||
updateOneRecordMutation,
|
||||
deleteOneRecordMutation,
|
||||
executeQuickActionOnOneRecordMutation,
|
||||
createManyRecordsMutation,
|
||||
deleteManyRecordsMutation,
|
||||
mapToObjectRecordIdentifier,
|
||||
getObjectOrderByField,
|
||||
} = result.current;
|
||||
const { objectMetadataItem } = result.current;
|
||||
|
||||
expect(labelIdentifierFieldMetadata).toBeUndefined();
|
||||
expect(basePathToShowPage).toBe('/object/opportunity/');
|
||||
expect(objectMetadataItem.id).toBe('20202020-cae9-4ff4-9579-f7d9fe44c937');
|
||||
expect(typeof getRecordFromCache).toBe('function');
|
||||
expect(typeof mapToObjectRecordIdentifier).toBe('function');
|
||||
expect(typeof getObjectOrderByField).toBe('function');
|
||||
expect(findManyRecordsQuery).toHaveProperty('kind', 'Document');
|
||||
expect(findOneRecordQuery).toHaveProperty('kind', 'Document');
|
||||
expect(createOneRecordMutation).toHaveProperty('kind', 'Document');
|
||||
expect(updateOneRecordMutation).toHaveProperty('kind', 'Document');
|
||||
expect(deleteOneRecordMutation).toHaveProperty('kind', 'Document');
|
||||
expect(executeQuickActionOnOneRecordMutation).toHaveProperty(
|
||||
'kind',
|
||||
'Document',
|
||||
);
|
||||
expect(createManyRecordsMutation).toHaveProperty('kind', 'Document');
|
||||
expect(deleteManyRecordsMutation).toHaveProperty('kind', 'Document');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user