Skip to content

Commit

Permalink
refactor + tests for getTermsInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
ntsekouras committed Sep 3, 2020
1 parent b4d97c0 commit c63bba1
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 39 deletions.
46 changes: 19 additions & 27 deletions packages/block-library/src/query/edit/query-toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,30 @@ import { postList } from '@wordpress/icons';
/**
* Internal dependencies
*/
import { getTaxonomyInfo } from '../utils';
import { getTermsInfo } from '../utils';

export default function QueryToolbar( { query, setQuery } ) {
const { categories, tags } = useSelect( ( select ) => {
const { getEntityRecords } = select( 'core' );
const _categories = getEntityRecords( 'taxonomy', 'category' );
const _tags = getEntityRecords( 'taxonomy', 'post_tag' );
return {
categories: getTaxonomyInfo( _categories ),
tags: getTaxonomyInfo( _tags ),
categories: getTermsInfo( _categories ),
tags: getTermsInfo( _tags ),
};
}, [] );

// Handles categories and tags changes.
const onTermsChange = ( terms, queryProperty ) => ( newTermNames ) => {
const termIds = newTermNames.map(
( name ) => terms.mapByName[ name ]?.id
);
if ( termIds.includes( undefined ) ) return;
setQuery( { [ queryProperty ]: termIds } );
};
const onCategoriesChange = onTermsChange( categories, 'categoryIds' );
const onTagsChange = onTermsChange( tags, 'tagIds' );

return (
<Toolbar>
<Dropdown
Expand Down Expand Up @@ -77,19 +89,8 @@ export default function QueryToolbar( { query, setQuery } ) {
.name,
} )
) }
suggestions={ categories.terms.map(
( { name } ) => name
) }
onChange={ ( newCategoryNames ) => {
const categoryIds = newCategoryNames.map(
( categoryName ) =>
categories.mapByName[ categoryName ]
?.id
);
if ( categoryIds.includes( undefined ) )
return;
setQuery( { categoryIds } );
} }
suggestions={ categories.names }
onChange={ onCategoriesChange }
/>
) }
{ tags?.terms && (
Expand All @@ -101,17 +102,8 @@ export default function QueryToolbar( { query, setQuery } ) {
value: tags.mapById[ tagId ].name,
} )
) }
suggestions={ tags.terms.map(
( { name } ) => name
) }
onChange={ ( newTagNames ) => {
const tagIds = newTagNames.map(
( tagName ) =>
tags.mapByName[ tagName ]?.id
);
if ( tagIds.includes( undefined ) ) return;
setQuery( { tagIds } );
} }
suggestions={ tags.names }
onChange={ onTagsChange }
/>
) }
</>
Expand Down
21 changes: 21 additions & 0 deletions packages/block-library/src/query/test/fixtures/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export const terms = [
{
count: 2,
id: 4,
meta: [],
name: 'nba',
parent: 0,
slug: 'nba',
taxonomy: 'category',
},
{
count: 0,
id: 11,
link: 'http://localhost:8888/?tag=featured',
name: 'featured',
slug: 'featured',
taxonomy: 'post_tag',
},
];

export default { terms };
30 changes: 30 additions & 0 deletions packages/block-library/src/query/test/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Internal dependencies
*/
import { terms } from './fixtures';
import { getTermsInfo } from '../utils';

describe( 'Query block utils', () => {
describe( 'getTermsInfo', () => {
it( 'should return an empty object when no terms provided', () => {
expect( getTermsInfo() ).toEqual( { terms: undefined } );
} );
it( 'should return proper terms info object', () => {
expect( getTermsInfo( terms ) ).toEqual(
expect.objectContaining( {
mapById: expect.objectContaining( {
'4': expect.objectContaining( { name: 'nba' } ),
'11': expect.objectContaining( {
name: 'featured',
} ),
} ),
mapByName: expect.objectContaining( {
nba: expect.objectContaining( { id: 4 } ),
featured: expect.objectContaining( { id: 11 } ),
} ),
names: expect.arrayContaining( [ 'nba', 'featured' ] ),
} )
);
} );
} );
} );
21 changes: 9 additions & 12 deletions packages/block-library/src/query/utils.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
// TODO jsdoc and tests
export const getTaxonomyInfo = ( terms ) => ( {
export const getTermsInfo = ( terms ) => ( {
terms,
...terms?.reduce(
( acc, term ) => ( {
mapById: {
...acc.mapById,
[ term.id ]: term,
},
mapByName: {
...acc.mapByName,
[ term.name ]: term,
},
} ),
{ mapById: {}, mapByName: {} }
( accumulator, term ) => {
const { mapById, mapByName, names } = accumulator;
mapById[ term.id ] = term;
mapByName[ term.name ] = term;
names.push( term.name );
return accumulator;
},
{ mapById: {}, mapByName: {}, names: [] }
),
} );

0 comments on commit c63bba1

Please sign in to comment.