Skip to content
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
2 changes: 1 addition & 1 deletion js/formidable_admin.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions js/src/admin/duplicateFieldSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ export function restoreFieldSettingValues( snapshot, settings ) {
const preserved = new Set( [ 'field_key', 'field_order', 'id', 'form_id' ].map(
key => `field_options[${ key }_${ toId }]`
) );
if ( settings.querySelector( '.frm_repeat_field' )?.value === '1' ) {
// PHP created a separate child form for this repeater's copied fields.
preserved.add( `field_options[form_select_${ toId }]` );
}
const changed = [];
snapshot.controls.forEach( control => {
const name = copyControlName( control.name, fromId, toId );
Expand Down
46 changes: 46 additions & 0 deletions tests/js/duplicateFieldSettings.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* oxlint-disable import/no-nodejs-modules -- Tests run with the Node test runner. */
import assert from 'node:assert/strict';
import { test } from 'node:test';
import { restoreFieldSettingValues } from '../../js/src/admin/duplicateFieldSettings.js';

function restoreSettings( repeat ) {
const controls = [ 'form_select', 'name', 'field_key', 'field_order', 'form_id' ];
const targets = controls.map( key => ( {
name: `field_options[${ key }_202]`,
value: `new-${ key }`,
checked: false,
} ) );
const snapshot = {
fieldId: '101',
controls: controls.map( key => ( {
name: `field_options[${ key }_101]`,
value: `original-${ key }`,
type: 'hidden',
checked: false,
disabled: false,
select: null,
} ) ),
};
const settings = {
dataset: { fid: '202' },
querySelectorAll: () => targets,
querySelector: () => repeat === null ? null : { value: repeat },

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Non-blocking: this stub returns the same object for any selector, so the test would still pass if the source queried a different class or element. The live run above covered the real selector, but the unit test cannot catch a rename. Make the stub answer only for the selector the code uses (I ran this: 2/2 still pass):

Suggested change
querySelector: () => repeat === null ? null : { value: repeat },
querySelector: selector => selector === '.frm_repeat_field' && repeat !== null ? { value: repeat } : null,

};

restoreFieldSettingValues( snapshot, settings );
return targets.map( target => target.value );
}

test( 'duplicated repeaters keep their new child form and generated identities', () => {
assert.deepEqual( restoreSettings( '1' ), [
'new-form_select', 'original-name', 'new-field_key', 'new-field_order', 'new-form_id',
] );
} );

test( 'non-repeating fields still copy an unsaved form or field selection', () => {
for ( const repeat of [ '0', '', null ] ) {
assert.deepEqual( restoreSettings( repeat ), [
'original-form_select', 'original-name', 'new-field_key', 'new-field_order', 'new-form_id',
] );
}
} );
Loading