Skip to content

Commit 0b0f9fa

Browse files
committed
UIFC-483-refactor-useUpdatedFilter add test, useMemo
1 parent ebcc803 commit 0b0f9fa

4 files changed

Lines changed: 59 additions & 8 deletions

File tree

src/components/Filters/FilterFilters.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import PropTypes from 'prop-types';
2-
import { useState } from 'react';
2+
import { useMemo } from 'react';
33
import { FormattedMessage } from 'react-intl';
44

55
import {
@@ -16,7 +16,7 @@ const FilterFilters = ({
1616
activeFilters = { type: [] },
1717
filterHandlers,
1818
}) => {
19-
const [filterState] = useState(() => buildFilterState(filterConfig));
19+
const filterState = useMemo(() => buildFilterState(filterConfig), []);
2020

2121
const renderCheckboxFilter = (key, props) => {
2222
const groupFilters = activeFilters[key] || [];

src/components/MetadataCollections/CollectionFilters.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import PropTypes from 'prop-types';
2-
import { useState } from 'react';
2+
import { useMemo } from 'react';
33
import { FormattedMessage } from 'react-intl';
44

55
import {
@@ -24,10 +24,11 @@ const CollectionFilters = ({
2424
filterData,
2525
...props
2626
}) => {
27-
const [filterState] = useState(() => buildFilterState(
27+
const filterState = useMemo(
2828
// skip for mdSource filter as it is dynamic and handled separately
29-
filterConfig.filter(f => f.name !== 'mdSource')
30-
));
29+
() => buildFilterState(filterConfig.filter(f => f.name !== 'mdSource')),
30+
[]
31+
);
3132

3233
const renderCheckboxFilter = (key) => {
3334
const groupFilters = activeFilters[key] || [];

src/components/MetadataSources/SourceFilters.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import PropTypes from 'prop-types';
2-
import { useState } from 'react';
2+
import { useMemo } from 'react';
33
import { FormattedMessage } from 'react-intl';
44

55
import {
@@ -20,7 +20,7 @@ const SourceFilters = ({
2020
filterHandlers,
2121
...props
2222
}) => {
23-
const [filterState] = useState(() => buildFilterState(filterConfig));
23+
const filterState = useMemo(() => buildFilterState(filterConfig), []);
2424

2525
const renderCheckboxFilter = (key) => {
2626
const groupFilters = activeFilters[key] || [];

src/util/filterUtils.test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { buildFilterState } from './filterUtils';
2+
3+
describe('buildFilterState', () => {
4+
it('should build filter state from configs', () => {
5+
const configs = [
6+
{
7+
name: 'filter1',
8+
values: [
9+
{ cql: 'cql1', name: 'name1' },
10+
{ cql: 'cql2', name: 'name2' },
11+
],
12+
},
13+
{
14+
name: 'filter2',
15+
values: [{ cql: 'cql3', name: 'name3' }],
16+
},
17+
];
18+
19+
const expected = {
20+
filter1: [
21+
{ value: 'cql1', label: 'name1' },
22+
{ value: 'cql2', label: 'name2' },
23+
],
24+
filter2: [{ value: 'cql3', label: 'name3' }],
25+
};
26+
27+
expect(buildFilterState(configs)).toEqual(expected);
28+
});
29+
30+
it('should handle empty configs', () => {
31+
const configs = [];
32+
const expected = {};
33+
expect(buildFilterState(configs)).toEqual(expected);
34+
});
35+
36+
it('should handle configs with empty values', () => {
37+
const configs = [
38+
{
39+
name: 'filter1',
40+
values: [],
41+
},
42+
];
43+
44+
const expected = {
45+
filter1: [],
46+
};
47+
48+
expect(buildFilterState(configs)).toEqual(expected);
49+
});
50+
});

0 commit comments

Comments
 (0)