Skip to content

Commit

Permalink
Query Block: Add tests for getValueFromObjectPath() util (#48956)
Browse files Browse the repository at this point in the history
  • Loading branch information
tyxla authored Mar 9, 2023
1 parent c8964e5 commit 9751a79
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
34 changes: 33 additions & 1 deletion packages/block-library/src/query/test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Internal dependencies
*/
import { terms } from './fixtures';
import { getEntitiesInfo } from '../utils';
import { getEntitiesInfo, getValueFromObjectPath } from '../utils';

describe( 'Query block utils', () => {
describe( 'getEntitiesInfo', () => {
Expand All @@ -29,4 +29,36 @@ describe( 'Query block utils', () => {
);
} );
} );

describe( 'getValueFromObjectPath', () => {
it( 'should return undefined when path is empty', () => {
const object = { foo: 'bar' };
const result = getValueFromObjectPath( object, '' );
expect( result ).toBeUndefined();
} );

it( 'should return undefined when path does not exist', () => {
const object = { foo: 'bar' };
const result = getValueFromObjectPath( object, 'baz' );
expect( result ).toBeUndefined();
} );

it( 'should return undefined when a deeper path does not exist', () => {
const object = { foo: { bar: 'baz' } };
const result = getValueFromObjectPath( object, 'foo.test' );
expect( result ).toBeUndefined();
} );

it( 'should return the corresponding value of a single level path', () => {
const object = { foo: 'bar' };
const result = getValueFromObjectPath( object, 'foo' );
expect( result ).toBe( 'bar' );
} );

it( 'should return the value of a deeper path', () => {
const object = { foo: { bar: { baz: 'test' } } };
const result = getValueFromObjectPath( object, 'foo.bar.baz' );
expect( result ).toBe( 'test' );
} );
} );
} );
2 changes: 1 addition & 1 deletion packages/block-library/src/query/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const getEntitiesInfo = ( entities ) => {
* @param {string} path Path to the object property.
* @return {*} Value of the object property at the specified path.
*/
const getValueFromObjectPath = ( object, path ) => {
export const getValueFromObjectPath = ( object, path ) => {
const normalizedPath = path.split( '.' );
let value = object;
normalizedPath.forEach( ( fieldName ) => {
Expand Down

0 comments on commit 9751a79

Please sign in to comment.