diff --git a/lib/VersionHistory/VersionCard/VersionCard.js b/lib/VersionHistory/VersionCard/VersionCard.js index c5a07617..86b718e7 100644 --- a/lib/VersionHistory/VersionCard/VersionCard.js +++ b/lib/VersionHistory/VersionCard/VersionCard.js @@ -20,7 +20,7 @@ const VersionCard = ({ id, isCurrent, isLatest, - isSystemChange = false, + isOriginal, onSelect, source, title, @@ -64,22 +64,18 @@ const VersionCard = ({ ), [id, isCurrent, onSelectVersion, tooltip]); const message = useMemo(() => { - if (isSystemChange) { + if (isOriginal) { return ( - <> - + - + ); } - if (changedFields.length) { + if (changedFields?.length) { return ( <> + <> + - + ); - }, [changedFields, isSystemChange]); + }, [changedFields, isOriginal]); return ( { expect(screen.getByText(defaultProps.title)).toBeInTheDocument(); }); - it('should display version history card with system update', () => { - renderVersionCard({ isSystemChange: true }); - - expect(screen.getByText(/systemChange/)).toBeInTheDocument(); - }); - it('should call \'onSelect\' when \'View this version\' icon button was clicked', async () => { renderVersionCard(); diff --git a/lib/VersionHistory/VersionHistoryPane/VersionHistoryPane.js b/lib/VersionHistory/VersionHistoryPane/VersionHistoryPane.js index 8b77f359..739309ac 100644 --- a/lib/VersionHistory/VersionHistoryPane/VersionHistoryPane.js +++ b/lib/VersionHistory/VersionHistoryPane/VersionHistoryPane.js @@ -59,14 +59,15 @@ const VersionHistoryPane = ({ userId, }, i) => { const user = usersMap[userId]; - const { changedFields, isSystemChange } = getFieldLabels( + const changedFields = getFieldLabels( intl, versionContext?.versionsMap?.[versionId]?.paths, labelsMap, hiddenFields, - systemUpdatedFields, ); + const isOriginal = i === versionsToDisplay.length - 1 && !changedFields?.length; + const source = ( } source={source} changedFields={changedFields} - isSystemChange={isSystemChange} /> ); }) @@ -99,7 +100,6 @@ const VersionHistoryPane = ({ hiddenFields, labelsMap, onSelectVersion, - systemUpdatedFields, usersMap, versionContext?.versionsMap, versionsToDisplay, diff --git a/lib/VersionHistory/getFieldLabels.js b/lib/VersionHistory/getFieldLabels.js index 1342e3dd..ecf68752 100644 --- a/lib/VersionHistory/getFieldLabels.js +++ b/lib/VersionHistory/getFieldLabels.js @@ -1,30 +1,22 @@ import { escapeRegExp, keyBy } from 'lodash'; -export const getFieldLabels = (intl, paths, labelsMap = {}, hiddenFields = [], systemUpdatedFields = []) => { - if (!paths) return { changedFields: [], systemChanges: [] }; +export const getFieldLabels = (intl, paths, labelsMap = {}, hiddenFields = []) => { + if (!paths) return null; const labelsMapEntries = Object.entries(labelsMap); const hiddenFieldsMap = keyBy(hiddenFields); - const { changedFields, systemChanges } = paths.reduce((acc, path) => { + return paths.reduce((acc, path) => { const fieldLabel = labelsMapEntries.find(([fieldPath]) => { - const regex = new RegExp(`^${escapeRegExp(fieldPath).replaceAll('\\\\d', '\\d')}$`); + const regex = new RegExp(`^${escapeRegExp(fieldPath).replace('\\\\d', '\\d')}$`); return regex.test(path); })?.[1] || path; - if (systemUpdatedFields.includes(path.replace(/\[\d+\]/g, ''))) { - acc.systemChanges.push(fieldLabel); - } else if (!hiddenFieldsMap[fieldLabel]) { - acc.changedFields.push(intl.formatMessage({ id: fieldLabel })); + if (!hiddenFieldsMap[fieldLabel]) { + acc.push(intl.formatMessage({ id: fieldLabel })); } return acc; - }, { changedFields: [], systemChanges: [] }); - - return { - changedFields: changedFields.sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase())), - systemChanges, - isSystemChange: !changedFields.length && systemChanges.length, - }; + }, []).sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase())); }; diff --git a/lib/VersionHistory/getFieldLabels.test.js b/lib/VersionHistory/getFieldLabels.test.js index 5295c82e..34d80257 100644 --- a/lib/VersionHistory/getFieldLabels.test.js +++ b/lib/VersionHistory/getFieldLabels.test.js @@ -16,12 +16,8 @@ const paths = [ const intl = { formatMessage: ({ id }) => id }; describe('getFieldLabels', () => { - beforeEach(() => { - jest.clearAllMocks(); - }); - it('should use keys of labelsMap as regexp to parse labels of paths', () => { - expect(getFieldLabels(intl, paths, labelsMap).changedFields).toEqual(expect.arrayContaining([ + expect(getFieldLabels(intl, paths, labelsMap)).toEqual(expect.arrayContaining([ ...Object.values(labelsMap), ])); }); @@ -32,18 +28,9 @@ describe('getFieldLabels', () => { }; const pathsWithHiddenFields = [...paths, hiddenFields.donors]; - const result = getFieldLabels(intl, pathsWithHiddenFields, labelsMap, hiddenFields); - expect(result.changedFields).toEqual(expect.arrayContaining([ + expect(getFieldLabels(intl, pathsWithHiddenFields, labelsMap, hiddenFields)).toEqual(expect.arrayContaining([ ...Object.values(labelsMap), ])); }); - - it('should isSystemChange equal `true` if there are only systemChanges', () => { - const systemUpdatedFields = ['fundDistribution']; - - const result = getFieldLabels(intl, ['fundDistribution'], labelsMap, [], systemUpdatedFields); - - expect(result.isSystemChange).toBeTruthy(); - }); });