Skip to content

Commit

Permalink
EDSC-2: cleanup and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
eudoroolivares2016 committed Jan 30, 2025
1 parent 9e04761 commit 4fff0fa
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/ts/component/AppliedFilters/AppliedFilters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -72,7 +71,6 @@ export const AppliedFilters: React.FC<AppliedFiltersProps> = ({
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<AppliedFilter[]>([])

Expand Down Expand Up @@ -123,8 +121,9 @@ export const AppliedFilters: React.FC<AppliedFiltersProps> = ({
})
}

// 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)
}

Expand Down
15 changes: 15 additions & 0 deletions src/ts/component/AppliedFilters/__tests__/AppliedFilters.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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({})
Expand Down

0 comments on commit 4fff0fa

Please sign in to comment.