From 4fff0fa0e57054be67ce1580a3585fccf6eb972b Mon Sep 17 00:00:00 2001 From: Ed Olivares <34591886+eudoroolivares2016@users.noreply.github.com> Date: Wed, 29 Jan 2025 20:51:45 -0500 Subject: [PATCH] EDSC-2: cleanup and add test --- .../component/AppliedFilters/AppliedFilters.tsx | 13 ++++++------- .../__tests__/AppliedFilters.test.tsx | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/ts/component/AppliedFilters/AppliedFilters.tsx b/src/ts/component/AppliedFilters/AppliedFilters.tsx index ab7fa29..220fa04 100644 --- a/src/ts/component/AppliedFilters/AppliedFilters.tsx +++ b/src/ts/component/AppliedFilters/AppliedFilters.tsx @@ -53,14 +53,13 @@ const temporalToTitle = (temporal: string[] | string) => { return `${start} to ${end}` } -// TODO in earthdata Search we sort by onoing vs end_date the interface says end_date -// Thats what its been currently doing should we change it to be ongoing? -const supportedSortKeys = ['-score', '-usage_score', 'start_date', 'end_date'] +const matchValidSortKey = (inputKey: string): string => { + // Thats what its been currently doing should we change it to be ongoing? + const supportedSortKeys = ['-score', '-usage_score', 'start_date', 'end_date'] -const matchSortKey = (inputKey: string, supportedKeys: string[]): string => { const normalizedInput = inputKey.replace(/^-/, '').toLowerCase() - const matchedKey = supportedKeys.find((supportedKey) => supportedKey.replace(/^-/, '').toLowerCase() === normalizedInput) + const matchedKey = supportedSortKeys.find((supportedKey) => supportedKey.replace(/^-/, '').toLowerCase() === normalizedInput) return matchedKey ?? inputKey } @@ -72,7 +71,6 @@ export const AppliedFilters: React.FC = ({ setQueryString }) => { const { temporal, bounding_box: boundingBox, sort_key: sortKey } = filterValues - console.log('🚀 ~ file: AppliedFilters.tsx:71 ~ sortKey:', sortKey) const formik = useFormikContext() const [applied, setApplied] = useState([]) @@ -123,8 +121,9 @@ export const AppliedFilters: React.FC = ({ }) } + // Normalize the sort key so that the order is consistent if (sortKey) { - const normalizedSortKey = matchSortKey(sortKey, supportedSortKeys) + const normalizedSortKey = matchValidSortKey(sortKey) formik.setFieldValue('sort_key', normalizedSortKey) } diff --git a/src/ts/component/AppliedFilters/__tests__/AppliedFilters.test.tsx b/src/ts/component/AppliedFilters/__tests__/AppliedFilters.test.tsx index 5c43b5d..b69f364 100644 --- a/src/ts/component/AppliedFilters/__tests__/AppliedFilters.test.tsx +++ b/src/ts/component/AppliedFilters/__tests__/AppliedFilters.test.tsx @@ -210,6 +210,21 @@ describe('AppliedFilters', () => { }) }) + describe('when sort key filter has the wrong direction', () => { + test('renders the single date', () => { + const { mockFormik } = setup({ + overrideProps: { + filterValues: { + sort_key: 'usage_score' + } + } + }) + + expect(mockFormik.setFieldValue).toHaveBeenCalledTimes(1) + expect(mockFormik.setFieldValue).toHaveBeenCalledWith('sort_key', '-usage_score') + }) + }) + describe('when removing a filter', () => { test('should remove the selected filter', async () => { const { user, props } = setup({})