Skip to content

Commit 679148a

Browse files
committed
More testing coverage to replace large field wrapper test
1 parent 2b8df14 commit 679148a

15 files changed

+1773
-233
lines changed

ui/js/blocks/src/blocks/components/test/FieldInspectorControls.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,6 @@ describe( 'FieldInspectorControls', () => {
5858
/>
5959
);
6060

61-
expect( screen.getByTestId( 'pods-inspector' ).innerHTML ).toMatchSnapshot();
61+
expect( screen.getByTestId( 'pods-inspector' ).tagName ).toEqual( 'DIV' );
6262
} );
6363
} );

ui/js/blocks/src/blocks/components/test/__snapshots__/FieldInspectorControls.test.js.snap

Lines changed: 0 additions & 3 deletions
This file was deleted.

ui/js/dfv/src/components/field-wrapper/div-field-layout.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const DivFieldLayout = ( {
1313
} ) => {
1414
const dfvContainerClass = classnames(
1515
'pods-dfv-container',
16-
`pods-dfv-container-${ fieldType }`
16+
`pods-dfv-container-${ fieldType }`,
1717
);
1818

1919
return (

ui/js/dfv/src/components/field-wrapper/field-error-boundary.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class FieldErrorBoundary extends React.Component {
2626
console.warn(
2727
'There was an error rendering this field.',
2828
error,
29-
errorInfo
29+
errorInfo,
3030
);
3131
}
3232

ui/js/dfv/src/components/field-wrapper/index.js

Lines changed: 85 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,24 @@ export const FieldWrapper = ( props ) => {
8282
// Sort out different shapes that we could get the help text in.
8383
// It's possible to get an array of strings for the help text, but it
8484
// will usually be a string.
85-
const shouldShowHelpText = helpText && ( 'help' !== helpText );
85+
const shouldShowHelpText = helpText && (
86+
'help' !== helpText
87+
);
8688

8789
const helpTextString = Array.isArray( helpText ) ? helpText[ 0 ] : helpText;
88-
const helpLink = ( Array.isArray( helpText ) && !! helpText[ 1 ] )
90+
const helpLink = (
91+
Array.isArray( helpText ) && !! helpText[ 1 ]
92+
)
8993
? helpText[ 1 ]
9094
: undefined;
9195

9296
// Some fields show a label, and some don't.
9397
// Others get a description and others don't.
9498
const showLabel = (
9599
'heading' !== fieldType &&
96-
( 'html' !== fieldType || ! htmlNoLabel ) &&
100+
(
101+
'html' !== fieldType || ! htmlNoLabel
102+
) &&
97103
! fieldEmbed
98104
);
99105

@@ -110,7 +116,9 @@ export const FieldWrapper = ( props ) => {
110116
const passAllPodFieldsMap = isBooleanGroupField;
111117

112118
// Make all values into an array, to make handling repeatable fields easier.
113-
const valuesArray = ( isRepeatable && Array.isArray( value ) )
119+
const valuesArray = (
120+
isRepeatable && Array.isArray( value )
121+
)
114122
? value
115123
: [ value ];
116124

@@ -136,16 +144,16 @@ export const FieldWrapper = ( props ) => {
136144
&& field?.required
137145
) {
138146
// Check if we have a field value.
139-
let fieldValue = undefined;
140-
let fieldVariation = undefined;
147+
let fieldValue;
148+
let fieldVariation;
141149

142-
let variations = [
150+
const variations = [
143151
field.name,
144152
'pods_meta_' + field.name,
145153
'pods_field_' + field.name,
146154
];
147155

148-
variations.every( variation => {
156+
variations.every( ( variation ) => {
149157
// Stop the loop if we found the value we were looking for.
150158
if ( 'undefined' !== typeof allPodValues[ variation ] ) {
151159
fieldValue = allPodValues[ variation ];
@@ -161,8 +169,8 @@ export const FieldWrapper = ( props ) => {
161169
if (
162170
'' === fieldValue
163171
|| (
164-
'undefined' !== typeof field?.data['']
165-
&& field?.data[''] === fieldValue
172+
'undefined' !== typeof field?.data[ '' ]
173+
&& field?.data[ '' ] === fieldValue
166174
)
167175
) {
168176
setValue( field?.default ?? '' );
@@ -190,7 +198,7 @@ export const FieldWrapper = ( props ) => {
190198
condition: () => true === toBool( required ),
191199
},
192200
],
193-
value
201+
value,
194202
);
195203

196204
// Handle Block Editor save lock.
@@ -230,71 +238,75 @@ export const FieldWrapper = ( props ) => {
230238
const inputComponents = !! FieldComponent ? (
231239
<FieldErrorBoundary>
232240
<div className="pods-field-wrapper">
233-
{ ( () => {
234-
if ( true === isBooleanGroupField ) {
241+
{ (
242+
() => {
243+
if ( true === isBooleanGroupField ) {
244+
return (
245+
<FieldComponent
246+
storeKey={ storeKey }
247+
values={ values }
248+
podName={ podName }
249+
podType={ podType }
250+
allPodValues={ passAllPodValues ? allPodValues : undefined }
251+
allPodFieldsMap={ passAllPodFieldsMap ? allPodFieldsMap : undefined }
252+
setOptionValue={ setOptionValue }
253+
isValid={ ! validationMessages.length }
254+
addValidationRules={ addValidationRules }
255+
setHasBlurred={ () => setHasBlurred( true ) }
256+
fieldConfig={ field }
257+
/>
258+
);
259+
}
260+
261+
if ( true === isRepeatable ) {
262+
return (
263+
<RepeatableFieldList
264+
storeKey={ storeKey }
265+
fieldConfig={ processedFieldConfig }
266+
valuesArray={ valuesArray }
267+
FieldComponent={ FieldComponent }
268+
podType={ podType }
269+
podName={ podName }
270+
allPodValues={ passAllPodValues ? allPodValues : undefined }
271+
allPodFieldsMap={ passAllPodFieldsMap ? allPodFieldsMap : undefined }
272+
setFullValue={ setValue }
273+
setHasBlurred={ () => setHasBlurred( true ) }
274+
/>
275+
);
276+
}
277+
235278
return (
236279
<FieldComponent
237280
storeKey={ storeKey }
238-
values={ values }
281+
value={ value }
239282
podName={ podName }
240283
podType={ podType }
241-
allPodValues={ passAllPodValues ? allPodValues : undefined }
242-
allPodFieldsMap={ passAllPodFieldsMap ? allPodFieldsMap : undefined }
243-
setOptionValue={ setOptionValue }
284+
allPodValues={ allPodValues }
285+
allPodFieldsMap={ allPodFieldsMap }
286+
setValue={ setValue }
244287
isValid={ ! validationMessages.length }
245288
addValidationRules={ addValidationRules }
246289
setHasBlurred={ () => setHasBlurred( true ) }
247-
fieldConfig={ field }
248-
/>
249-
);
250-
}
251-
252-
if ( true === isRepeatable ) {
253-
return (
254-
<RepeatableFieldList
255-
storeKey={ storeKey }
256290
fieldConfig={ processedFieldConfig }
257-
valuesArray={ valuesArray }
258-
FieldComponent={ FieldComponent }
259-
podType={ podType }
260-
podName={ podName }
261-
allPodValues={ passAllPodValues ? allPodValues : undefined }
262-
allPodFieldsMap={ passAllPodFieldsMap ? allPodFieldsMap : undefined }
263-
setFullValue={ setValue }
264-
setHasBlurred={ () => setHasBlurred( true ) }
265291
/>
266292
);
267293
}
268-
269-
return (
270-
<FieldComponent
271-
storeKey={ storeKey }
272-
value={ value }
273-
podName={ podName }
274-
podType={ podType }
275-
allPodValues={ allPodValues }
276-
allPodFieldsMap={ allPodFieldsMap }
277-
setValue={ setValue }
278-
isValid={ ! validationMessages.length }
279-
addValidationRules={ addValidationRules }
280-
setHasBlurred={ () => setHasBlurred( true ) }
281-
fieldConfig={ processedFieldConfig }
282-
/>
283-
);
284-
} )() }
294+
)() }
285295
</div>
286296
</FieldErrorBoundary>
287297
) : (
288298
<span className="pods-field-option__invalid-field">
289299
{ sprintf(
290300
// translators: %s is the field type.
291301
__( 'The field type \'%s\' was invalid.', 'pods' ),
292-
fieldType
302+
fieldType,
293303
) }
294304
</span>
295305
);
296306

297-
const validationMessagesComponent = ( hasBlurred && validationMessages.length ) ? (
307+
const validationMessagesComponent = (
308+
hasBlurred && validationMessages.length
309+
) ? (
298310
<ValidationMessages messages={ validationMessages } />
299311
) : undefined;
300312

@@ -406,8 +418,10 @@ const MemoizedFieldWrapper = React.memo(
406418

407419
// Look up the dependencies, we may need to re-render if any of the
408420
// values have changed.
409-
const allDependencyFieldSlugs = ( nextProps.field?.conditional_logic?.rules || [] ).map(
410-
( rule ) => rule.field
421+
const allDependencyFieldSlugs = (
422+
nextProps.field?.conditional_logic?.rules || []
423+
).map(
424+
( rule ) => rule.field,
411425
);
412426

413427
// If it's a boolean group, there are also subfields to check.
@@ -416,9 +430,13 @@ const MemoizedFieldWrapper = React.memo(
416430

417431
subfields.forEach( ( subfield ) => {
418432
allDependencyFieldSlugs.push(
419-
...( ( subfield?.conditional_logic?.rules || [] ).map(
420-
( rule ) => rule.field
421-
) )
433+
...(
434+
(
435+
subfield?.conditional_logic?.rules || []
436+
).map(
437+
( rule ) => rule.field,
438+
)
439+
),
422440
);
423441
} );
424442
}
@@ -444,9 +462,13 @@ const MemoizedFieldWrapper = React.memo(
444462
}
445463

446464
parentDependencySlugs.push(
447-
...( ( parentField?.conditional_logic?.rules || [] ).map(
448-
( rule ) => rule.field
449-
) )
465+
...(
466+
(
467+
parentField?.conditional_logic?.rules || []
468+
).map(
469+
( rule ) => rule.field,
470+
)
471+
),
450472
);
451473
} );
452474

@@ -458,7 +480,7 @@ const MemoizedFieldWrapper = React.memo(
458480
...dependencyFieldSlugs,
459481
...parentDependencySlugs,
460482
...nextLevelSlugs,
461-
]
483+
],
462484
);
463485
};
464486

@@ -474,7 +496,7 @@ const MemoizedFieldWrapper = React.memo(
474496
} );
475497

476498
return ! haveAnyDependenciesChanged;
477-
}
499+
},
478500
);
479501

480502
export default MemoizedFieldWrapper;

ui/js/dfv/src/components/field-wrapper/repeatable-field-list.js

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,12 @@ const RepeatableFieldList = ( {
6969

7070
const deleteValueAtIndex = ( index ) => {
7171
const newValues = [
72-
...( valuesArray || [] ).slice( 0, index ),
73-
...( valuesArray || [] ).slice( index + 1 ),
72+
...(
73+
valuesArray || []
74+
).slice( 0, index ),
75+
...(
76+
valuesArray || []
77+
).slice( index + 1 ),
7478
];
7579

7680
setFullValue( newValues );
@@ -112,7 +116,10 @@ const RepeatableFieldList = ( {
112116
);
113117

114118
const handleDragEnd = ( event ) => {
115-
const { active, over } = event;
119+
const {
120+
active,
121+
over,
122+
} = event;
116123

117124
if ( ! over?.id || active.id === over.id ) {
118125
return;
@@ -122,7 +129,7 @@ const RepeatableFieldList = ( {
122129
const newIndex = parseInt( over.id, 10 );
123130

124131
setFullValue(
125-
arrayMove( valuesArray, oldIndex, newIndex )
132+
arrayMove( valuesArray, oldIndex, newIndex ),
126133
);
127134

128135
setHasBlurred( true );
@@ -161,7 +168,9 @@ const RepeatableFieldList = ( {
161168
setValue={ createSetValueAtIndex( index ) }
162169
setHasBlurred={ setHasBlurred }
163170
isDraggable={ valuesArray.length > 1 }
164-
moveControls={ ( valuesArray.length > 1 ) ? (
171+
moveControls={ (
172+
valuesArray.length > 1
173+
) ? (
165174
<ToolbarGroup className="pods-field-wrapper__movers">
166175
<ToolbarButton
167176
disabled={ index === 0 }
@@ -173,7 +182,9 @@ const RepeatableFieldList = ( {
173182
/>
174183

175184
<ToolbarButton
176-
disabled={ index === ( valuesArray.length - 1 ) }
185+
disabled={ index === (
186+
valuesArray.length - 1
187+
) }
177188
onClick={ () => swapValues( index, index + 1 ) }
178189
icon={ chevronDown }
179190
label={ __( 'Move down', 'pods' ) }
@@ -182,15 +193,17 @@ const RepeatableFieldList = ( {
182193
/>
183194
</ToolbarGroup>
184195
) : null }
185-
deleteControl={ ( valuesArray.length > 1 ) ? (
196+
deleteControl={ (
197+
valuesArray.length > 1
198+
) ? (
186199
<ToolbarButton
187200
onClick={ ( event ) => {
188201
event.stopPropagation();
189202

190203
// eslint-disable-next-line no-alert
191204
const confirmation = confirm(
192205
// eslint-disable-next-line @wordpress/i18n-no-collapsible-whitespace
193-
__( 'Are you sure you want to delete this value?', 'pods' )
206+
__( 'Are you sure you want to delete this value?', 'pods' ),
194207
);
195208

196209
if ( confirmation ) {
@@ -237,8 +250,8 @@ RepeatableFieldList.propTypes = {
237250
PropTypes.bool,
238251
PropTypes.number,
239252
PropTypes.array,
240-
]
241-
)
253+
],
254+
),
242255
),
243256

244257
/**

0 commit comments

Comments
 (0)