Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Parent Check Condition in buildTermsTree #66006

Merged
merged 4 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/editor/src/utils/terms.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ export function buildTermsTree( flatTerms ) {
const flatTermsWithParentAndChildren = flatTerms.map( ( term ) => {
return {
children: [],
parent: null,
parent: undefined,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Terms fetched by getEntityRecords use parent: undefined, so we match them.

...term,
};
} );

// All terms should have a `parent` because we're about to index them by it.
if (
flatTermsWithParentAndChildren.some( ( { parent } ) => parent === null )
flatTermsWithParentAndChildren.some(
( { parent } ) => parent === undefined
)
) {
return flatTermsWithParentAndChildren;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/editor/src/utils/test/terms.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
import { buildTermsTree } from '../terms';

describe( 'buildTermsTree()', () => {
it( 'Should return same array as input with null parent and empty children added if parent is never specified.', () => {
it( 'Should return same array as input with undefined parent and empty children added if parent is never specified.', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we test both cases of null and undefined?

const input = Object.freeze( [
{ id: 2232, dummy: true },
{ id: 2245, dummy: true },
] );
const output = Object.freeze( [
{ id: 2232, parent: null, children: [], dummy: true },
{ id: 2245, parent: null, children: [], dummy: true },
{ id: 2232, parent: undefined, children: [], dummy: true },
{ id: 2245, parent: undefined, children: [], dummy: true },
] );
const termsTreem = buildTermsTree( input );
expect( termsTreem ).toEqual( output );
Expand Down
Loading