refactor: refactor from another PR from main

This commit is contained in:
Josh 2022-11-14 12:37:08 +00:00
parent 8c83e9d508
commit 8f708964ea
7 changed files with 6 additions and 72 deletions

View File

@ -1,19 +0,0 @@
import { getDisplayedValue } from '../useMainValue';
describe('getDisplayedValue', () => {
it('returns the mainField value', () => {
const modifiedData = {
DeepComplex: [
{
Title: 'File',
},
],
};
const componentFieldPath = ['DeepComplex', 0];
const mainField = 'Title';
const normalizedContent = getDisplayedValue(modifiedData, componentFieldPath, mainField);
expect(normalizedContent).toEqual('File');
});
});

View File

@ -1,21 +0,0 @@
import { useMemo } from 'react';
import get from 'lodash/get';
import toString from 'lodash/toString';
import { useCMEditViewDataManager } from '@strapi/helper-plugin';
export function getDisplayedValue(modifiedData, componentFieldPath, mainField) {
return toString(get(modifiedData, [...componentFieldPath, mainField], ''));
}
function useMainValue(schema, componentFieldPath) {
const { modifiedData } = useCMEditViewDataManager();
const mainField = useMemo(() => get(schema, ['settings', 'mainField'], 'id'), [schema]);
const displayedValue =
mainField === 'id' ? '' : getDisplayedValue(modifiedData, componentFieldPath, mainField);
return displayedValue.trim().length < 1 ? '' : ` - ${displayedValue}`;
}
export default useMainValue;

View File

@ -1,11 +0,0 @@
import React from 'react';
function connect(WrappedComponent, select) {
return (props) => {
const selectors = select(props);
return <WrappedComponent {...props} {...selectors} />;
};
}
export default connect;

View File

@ -1,2 +0,0 @@
export { default as connect } from './connect';
export { default as select } from './select';

View File

@ -1,19 +0,0 @@
import { useMemo } from 'react';
import useMainValue from '../hooks/useMainValue';
import { useContentTypeLayout } from '../../../../../hooks';
function useSelect({ componentUid, name, index }) {
const { getComponentLayout } = useContentTypeLayout();
const componentLayoutData = useMemo(() => {
const layout = getComponentLayout(componentUid);
return layout;
}, [componentUid, getComponentLayout]);
const mainValue = useMainValue(componentLayoutData, [name, index]);
return {
mainValue,
};
}
export default useSelect;

View File

@ -17,6 +17,11 @@ jest.mock('../../../../hooks', () => ({
}),
}));
jest.mock('@strapi/helper-plugin', () => ({
...jest.requireActual('@strapi/helper-plugin'),
useCMEditViewDataManager: jest.fn().mockImplementation(() => ({ modifiedData: {} })),
}));
/**
* We _could_ unmock this and use it, but it requires more
* harnessing then is necessary and it's not worth it for these

View File

@ -15,6 +15,7 @@ const toggleNotification = jest.fn();
jest.mock('@strapi/helper-plugin', () => ({
...jest.requireActual('@strapi/helper-plugin'),
useCMEditViewDataManager: jest.fn().mockImplementation(() => ({ modifiedData: {} })),
useNotification: jest.fn().mockImplementation(() => toggleNotification),
NotAllowedInput: () => 'This field is not allowed',
}));