@@ -74,4 +74,110 @@ describe('toFieldPathId()', () => {
7474 path : [ ...parentPath ] ,
7575 } ) ;
7676 } ) ;
77+
78+ describe ( 'with nameGenerator' , ( ) => {
79+ const phpNameGenerator = ( path : ( string | number ) [ ] , idPrefix : string ) => {
80+ if ( path . length === 0 ) {
81+ return idPrefix ;
82+ }
83+ const segments = path . map ( ( segment ) => `[${ segment } ]` ) . join ( '' ) ;
84+ return `${ idPrefix } ${ segments } ` ;
85+ } ;
86+
87+ const GLOBAL_FORM_OPTIONS_WITH_NAME_GENERATOR = {
88+ idPrefix : DEFAULT_ID_PREFIX ,
89+ idSeparator : DEFAULT_ID_SEPARATOR ,
90+ nameGenerator : phpNameGenerator ,
91+ } ;
92+
93+ test ( 'generates name for string fieldPath' , ( ) => {
94+ const path = 'firstName' ;
95+ const result = toFieldPathId ( path , GLOBAL_FORM_OPTIONS_WITH_NAME_GENERATOR ) ;
96+ expect ( result ) . toEqual ( {
97+ [ ID_KEY ] : `${ DEFAULT_ID_PREFIX } ${ DEFAULT_ID_SEPARATOR } ${ path } ` ,
98+ path : [ path ] ,
99+ name : 'root[firstName]' ,
100+ } ) ;
101+ } ) ;
102+
103+ test ( 'generates name for nested object path' , ( ) => {
104+ const parentPath = [ 'tasks' , 0 ] ;
105+ const path = 'title' ;
106+ const result = toFieldPathId ( path , GLOBAL_FORM_OPTIONS_WITH_NAME_GENERATOR , parentPath ) ;
107+ expect ( result ) . toEqual ( {
108+ [ ID_KEY ] : `${ DEFAULT_ID_PREFIX } ${ DEFAULT_ID_SEPARATOR } tasks${ DEFAULT_ID_SEPARATOR } 0${ DEFAULT_ID_SEPARATOR } ${ path } ` ,
109+ path : [ ...parentPath , path ] ,
110+ name : 'root[tasks][0][title]' ,
111+ } ) ;
112+ } ) ;
113+
114+ test ( 'generates name for array index' , ( ) => {
115+ const parentPath = [ 'listOfStrings' ] ;
116+ const path = 0 ;
117+ const result = toFieldPathId ( path , GLOBAL_FORM_OPTIONS_WITH_NAME_GENERATOR , parentPath ) ;
118+ expect ( result ) . toEqual ( {
119+ [ ID_KEY ] : `${ DEFAULT_ID_PREFIX } ${ DEFAULT_ID_SEPARATOR } listOfStrings${ DEFAULT_ID_SEPARATOR } ${ path } ` ,
120+ path : [ ...parentPath , path ] ,
121+ name : 'root[listOfStrings][0]' ,
122+ } ) ;
123+ } ) ;
124+
125+ test ( 'does not generate name for empty path' , ( ) => {
126+ const result = toFieldPathId ( '' , GLOBAL_FORM_OPTIONS_WITH_NAME_GENERATOR ) ;
127+ expect ( result ) . toEqual ( {
128+ [ ID_KEY ] : DEFAULT_ID_PREFIX ,
129+ path : [ ] ,
130+ } ) ;
131+ } ) ;
132+
133+ test ( 'generates name with isMultiValue flag for multi-select fields' , ( ) => {
134+ const phpMultiValueNameGenerator = ( path : ( string | number ) [ ] , idPrefix : string , isMultiValue ?: boolean ) => {
135+ if ( path . length === 0 ) {
136+ return idPrefix ;
137+ }
138+ const segments = path . map ( ( segment ) => `[${ segment } ]` ) . join ( '' ) ;
139+ const baseName = `${ idPrefix } ${ segments } ` ;
140+ return isMultiValue ? `${ baseName } []` : baseName ;
141+ } ;
142+
143+ const GLOBAL_FORM_OPTIONS_WITH_MULTI_VALUE = {
144+ idPrefix : DEFAULT_ID_PREFIX ,
145+ idSeparator : DEFAULT_ID_SEPARATOR ,
146+ nameGenerator : phpMultiValueNameGenerator ,
147+ } ;
148+
149+ const parentPath = [ 'hobbies' ] ;
150+ const result = toFieldPathId ( '' , GLOBAL_FORM_OPTIONS_WITH_MULTI_VALUE , parentPath , true ) ;
151+ expect ( result ) . toEqual ( {
152+ [ ID_KEY ] : `${ DEFAULT_ID_PREFIX } ${ DEFAULT_ID_SEPARATOR } hobbies` ,
153+ path : parentPath ,
154+ name : 'root[hobbies][]' ,
155+ } ) ;
156+ } ) ;
157+
158+ test ( 'generates name without brackets when isMultiValue is false' , ( ) => {
159+ const phpMultiValueNameGenerator = ( path : ( string | number ) [ ] , idPrefix : string , isMultiValue ?: boolean ) => {
160+ if ( path . length === 0 ) {
161+ return idPrefix ;
162+ }
163+ const segments = path . map ( ( segment ) => `[${ segment } ]` ) . join ( '' ) ;
164+ const baseName = `${ idPrefix } ${ segments } ` ;
165+ return isMultiValue ? `${ baseName } []` : baseName ;
166+ } ;
167+
168+ const GLOBAL_FORM_OPTIONS_WITH_MULTI_VALUE = {
169+ idPrefix : DEFAULT_ID_PREFIX ,
170+ idSeparator : DEFAULT_ID_SEPARATOR ,
171+ nameGenerator : phpMultiValueNameGenerator ,
172+ } ;
173+
174+ const parentPath = [ 'firstName' ] ;
175+ const result = toFieldPathId ( '' , GLOBAL_FORM_OPTIONS_WITH_MULTI_VALUE , parentPath , false ) ;
176+ expect ( result ) . toEqual ( {
177+ [ ID_KEY ] : `${ DEFAULT_ID_PREFIX } ${ DEFAULT_ID_SEPARATOR } firstName` ,
178+ path : parentPath ,
179+ name : 'root[firstName]' ,
180+ } ) ;
181+ } ) ;
182+ } ) ;
77183} ) ;
0 commit comments