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

Component: Extract getNodeText to its own file and add unit tests #69135

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
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
30 changes: 1 addition & 29 deletions packages/components/src/autocomplete/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,35 +40,7 @@ import type {
UseAutocompleteProps,
WPCompleter,
} from './types';

const getNodeText = ( node: React.ReactNode ): string => {
if ( node === null ) {
return '';
}

switch ( typeof node ) {
case 'string':
case 'number':
return node.toString();
break;
case 'boolean':
return '';
break;
case 'object': {
if ( node instanceof Array ) {
return node.map( getNodeText ).join( '' );
}
if ( 'props' in node ) {
return getNodeText( node.props.children );
}
break;
}
default:
return '';
}

return '';
};
import getNodeText from '../utils/get-node-text';

const EMPTY_FILTERED_OPTIONS: KeyedOption[] = [];

Expand Down
24 changes: 24 additions & 0 deletions packages/components/src/utils/get-node-text.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const getNodeText = ( node: React.ReactNode ): string => {
if ( node === null ) {
return '';
}

switch ( typeof node ) {
case 'string':
case 'number':
return node.toString();
case 'object': {
if ( node instanceof Array ) {
return node.map( getNodeText ).join( '' );
}
if ( 'props' in node ) {
return getNodeText( node.props.children );
}
return '';
}
default:
return '';
}
};

export default getNodeText;
37 changes: 37 additions & 0 deletions packages/components/src/utils/test/get-node-text.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Internal dependencies
*/
import getNodeText from '../get-node-text';

describe( 'getNodeText', () => {
it( 'should return an empty string for null', () => {
expect( getNodeText( null ) ).toBe( '' );
} );

it( 'should return the string representation of a string node', () => {
expect( getNodeText( 'Hello' ) ).toBe( 'Hello' );
} );

it( 'should return the string representation of a number node', () => {
expect( getNodeText( 123 ) ).toBe( '123' );
} );

it( 'should return an empty string for a boolean node', () => {
expect( getNodeText( true ) ).toBe( '' );
} );

it( 'should concatenate text from an array of nodes', () => {
expect( getNodeText( [ 'Hello', ' ', 'World' ] ) ).toBe(
'Hello World'
);
} );

it( 'should return text from a React element with children', () => {
const element = (
<div>
Hello <span>World</span>
</div>
);
expect( getNodeText( element ) ).toBe( 'Hello World' );
} );
} );
Loading