-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b4d97c0
commit c63bba1
Showing
4 changed files
with
79 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ] ), | ||
} ) | ||
); | ||
} ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: [] } | ||
), | ||
} ); |