Skip to content

Commit c63bba1

Browse files
committed
refactor + tests for getTermsInfo
1 parent b4d97c0 commit c63bba1

File tree

4 files changed

+79
-39
lines changed

4 files changed

+79
-39
lines changed

packages/block-library/src/query/edit/query-toolbar.js

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,30 @@ import { postList } from '@wordpress/icons';
1515
/**
1616
* Internal dependencies
1717
*/
18-
import { getTaxonomyInfo } from '../utils';
18+
import { getTermsInfo } from '../utils';
1919

2020
export default function QueryToolbar( { query, setQuery } ) {
2121
const { categories, tags } = useSelect( ( select ) => {
2222
const { getEntityRecords } = select( 'core' );
2323
const _categories = getEntityRecords( 'taxonomy', 'category' );
2424
const _tags = getEntityRecords( 'taxonomy', 'post_tag' );
2525
return {
26-
categories: getTaxonomyInfo( _categories ),
27-
tags: getTaxonomyInfo( _tags ),
26+
categories: getTermsInfo( _categories ),
27+
tags: getTermsInfo( _tags ),
2828
};
2929
}, [] );
30+
31+
// Handles categories and tags changes.
32+
const onTermsChange = ( terms, queryProperty ) => ( newTermNames ) => {
33+
const termIds = newTermNames.map(
34+
( name ) => terms.mapByName[ name ]?.id
35+
);
36+
if ( termIds.includes( undefined ) ) return;
37+
setQuery( { [ queryProperty ]: termIds } );
38+
};
39+
const onCategoriesChange = onTermsChange( categories, 'categoryIds' );
40+
const onTagsChange = onTermsChange( tags, 'tagIds' );
41+
3042
return (
3143
<Toolbar>
3244
<Dropdown
@@ -77,19 +89,8 @@ export default function QueryToolbar( { query, setQuery } ) {
7789
.name,
7890
} )
7991
) }
80-
suggestions={ categories.terms.map(
81-
( { name } ) => name
82-
) }
83-
onChange={ ( newCategoryNames ) => {
84-
const categoryIds = newCategoryNames.map(
85-
( categoryName ) =>
86-
categories.mapByName[ categoryName ]
87-
?.id
88-
);
89-
if ( categoryIds.includes( undefined ) )
90-
return;
91-
setQuery( { categoryIds } );
92-
} }
92+
suggestions={ categories.names }
93+
onChange={ onCategoriesChange }
9394
/>
9495
) }
9596
{ tags?.terms && (
@@ -101,17 +102,8 @@ export default function QueryToolbar( { query, setQuery } ) {
101102
value: tags.mapById[ tagId ].name,
102103
} )
103104
) }
104-
suggestions={ tags.terms.map(
105-
( { name } ) => name
106-
) }
107-
onChange={ ( newTagNames ) => {
108-
const tagIds = newTagNames.map(
109-
( tagName ) =>
110-
tags.mapByName[ tagName ]?.id
111-
);
112-
if ( tagIds.includes( undefined ) ) return;
113-
setQuery( { tagIds } );
114-
} }
105+
suggestions={ tags.names }
106+
onChange={ onTagsChange }
115107
/>
116108
) }
117109
</>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export const terms = [
2+
{
3+
count: 2,
4+
id: 4,
5+
meta: [],
6+
name: 'nba',
7+
parent: 0,
8+
slug: 'nba',
9+
taxonomy: 'category',
10+
},
11+
{
12+
count: 0,
13+
id: 11,
14+
link: 'http://localhost:8888/?tag=featured',
15+
name: 'featured',
16+
slug: 'featured',
17+
taxonomy: 'post_tag',
18+
},
19+
];
20+
21+
export default { terms };
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Internal dependencies
3+
*/
4+
import { terms } from './fixtures';
5+
import { getTermsInfo } from '../utils';
6+
7+
describe( 'Query block utils', () => {
8+
describe( 'getTermsInfo', () => {
9+
it( 'should return an empty object when no terms provided', () => {
10+
expect( getTermsInfo() ).toEqual( { terms: undefined } );
11+
} );
12+
it( 'should return proper terms info object', () => {
13+
expect( getTermsInfo( terms ) ).toEqual(
14+
expect.objectContaining( {
15+
mapById: expect.objectContaining( {
16+
'4': expect.objectContaining( { name: 'nba' } ),
17+
'11': expect.objectContaining( {
18+
name: 'featured',
19+
} ),
20+
} ),
21+
mapByName: expect.objectContaining( {
22+
nba: expect.objectContaining( { id: 4 } ),
23+
featured: expect.objectContaining( { id: 11 } ),
24+
} ),
25+
names: expect.arrayContaining( [ 'nba', 'featured' ] ),
26+
} )
27+
);
28+
} );
29+
} );
30+
} );
Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
// TODO jsdoc and tests
2-
export const getTaxonomyInfo = ( terms ) => ( {
2+
export const getTermsInfo = ( terms ) => ( {
33
terms,
44
...terms?.reduce(
5-
( acc, term ) => ( {
6-
mapById: {
7-
...acc.mapById,
8-
[ term.id ]: term,
9-
},
10-
mapByName: {
11-
...acc.mapByName,
12-
[ term.name ]: term,
13-
},
14-
} ),
15-
{ mapById: {}, mapByName: {} }
5+
( accumulator, term ) => {
6+
const { mapById, mapByName, names } = accumulator;
7+
mapById[ term.id ] = term;
8+
mapByName[ term.name ] = term;
9+
names.push( term.name );
10+
return accumulator;
11+
},
12+
{ mapById: {}, mapByName: {}, names: [] }
1613
),
1714
} );

0 commit comments

Comments
 (0)