/* eslint-disable @typescript-eslint/no-explicit-any */ import assert from 'assert'; import { renderForm } from '../../test-utils/renderForm'; const settle = (ms = 400): Promise => new Promise((resolve) => setTimeout(resolve, ms)); const textField = (key: string, extra: Record = {}) => ({ key, type: 'textfield', input: true, label: key, clearOnHide: true, persistent: true, defaultValue: '', ...extra, }); const yesNo = (key: string, extra: Record = {}) => ({ key, type: 'radio', input: true, label: key, clearOnHide: true, persistent: true, defaultValue: null, values: [{ label: 'Yes', value: 'yes' }, { label: 'No', value: 'no' }], ...extra, }); const party = (key: string, knownKey: string) => ({ key, type: 'container', input: true, tree: true, label: key, clearOnHide: false, defaultValue: null, persistent: true, customConditional: `show = (row.${knownKey} === "yes") ? true : false`, components: [textField('name'), textField('line1')], }); const tabsSchema = { display: 'form', components: [{ key: 'rows', type: 'datagrid', input: true, tree: true, label: 'Rows', components: [{ key: 'group', type: 'container', input: true, tree: true, label: 'Group', clearOnHide: true, persistent: true, components: [{ key: 'tabs2', type: 'tabs', input: false, components: [ { key: 'tabA', label: 'A', components: [yesNo('partyAKnown'), party('partyA', 'partyAKnown')] }, { key: 'tabB', label: 'B', components: [yesNo('partyBKnown'), party('partyB', 'partyBKnown')] }, ], }], }], }], }; const findComponent = (form: any, key: string): any => { let found: any; form.everyComponent((component: any) => { if (!found && component.component.key === key) found = component; }); return found; }; const typeInto = (input: HTMLInputElement, value: string): void => { input.value = value; input.dispatchEvent(new window.Event('input', { bubbles: true })); input.dispatchEvent(new window.Event('change', { bubbles: true })); }; const clickRadio = (container: HTMLElement, key: string, value: string): void => { const radio = Array.from(container.querySelectorAll('input[type=radio]')) .find((input) => input.name.includes(`[${key}]`) && input.value === value); assert.ok(radio, `radio ${key}=${value} not rendered`); radio.click(); radio.dispatchEvent(new window.Event('change', { bubbles: true })); }; const switchTabsAndBack = async (container: HTMLElement): Promise => { const links = Array.from(container.querySelectorAll('[ref="tabLink-tabs2"]')); links[1].click(); await settle(); links[0].click(); await settle(); }; describe('real form: a visible container whose data is detached from its row', () => { it('reattaches the container so typed values reach the form data and survive tab switches', async function () { this.timeout(20000); const { form, container, destroy } = await renderForm(tabsSchema); try { clickRadio(container, 'partyAKnown', 'yes'); await settle(); const group = () => form.data.rows[0].group; delete group().partyA; const input = (name: string) => container.querySelector( `input[name="data[rows][0][group][partyA][${name}]"]`, ) as HTMLInputElement; typeInto(input('name'), 'Example'); typeInto(input('line1'), 'Line one'); await settle(); assert.deepStrictEqual( group().partyA, { name: 'Example', line1: 'Line one' }, 'typed values are missing from the form data', ); for (let i = 1; i <= 3; i++) { await switchTabsAndBack(container); assert.deepStrictEqual( group().partyA, { name: 'Example', line1: 'Line one' }, `container data lost on tab switch ${i}`, ); assert.strictEqual(input('name').value, 'Example', `typed value cleared on screen on tab switch ${i}`); } } finally { destroy(); } }); it('reattaches a detached container in a datagrid row when the root has the same key', async function () { this.timeout(20000); const schema = { display: 'form', components: [ { key: 'party', type: 'container', input: true, tree: true, label: 'Root party', components: [textField('name')] }, { key: 'rows', type: 'datagrid', input: true, tree: true, label: 'Rows', components: [{ key: 'party', type: 'container', input: true, tree: true, label: 'Row party', clearOnHide: false, components: [textField('name')] }], }, ], }; const { form, container, destroy } = await renderForm(schema, { data: { party: { name: 'ROOT' }, rows: [{ party: { name: 'ROW' } }] } }); try { delete form.data.rows[0].party; typeInto(container.querySelector('input[name="data[rows][0][party][name]"]') as HTMLInputElement, 'TYPED'); await settle(); const grid = findComponent(form, 'rows'); grid.setValue(grid.dataValue); await settle(); assert.deepStrictEqual(form.data.rows[0].party, { name: 'TYPED' }); assert.deepStrictEqual(form.data.party, { name: 'ROOT' }); } finally { destroy(); } }); it('keeps the empty shape of a hidden clearOnHide: false container stable across tab switches', async function () { this.timeout(20000); const { form, container, destroy } = await renderForm(tabsSchema); try { clickRadio(container, 'partyAKnown', 'no'); await settle(); const before = JSON.stringify(form.data.rows[0].group.partyA); await switchTabsAndBack(container); assert.strictEqual(findComponent(form, 'partyA').visible, false); assert.strictEqual(JSON.stringify(form.data.rows[0].group.partyA), before); } finally { destroy(); } }); }); describe('real form: hidden fields are not seeded with empty values (Form.io v4 parity)', () => { const mode = textField('mode'); const commentedOut = yesNo('commentedOut', { hidden: true, defaultValue: '', redrawOn: 'data', customConditional: "// show = (data.mode === 'a') ? true : false;", }); const staticHidden = textField('staticHidden', { hidden: true }); const conditional = textField('conditionalField', { customConditional: "show = data.mode === 'a';" }); const keptWhenHidden = textField('keptWhenHidden', { hidden: true, clearOnHide: false }); const schema = { display: 'form', components: [mode, commentedOut, staticHidden, conditional, keptWhenHidden] }; it('leaves hidden clear-on-hide fields out of the data and the submission', async function () { this.timeout(20000); const { form, destroy } = await renderForm(schema); try { for (const key of ['commentedOut', 'staticHidden', 'conditionalField']) { assert.ok(!(key in form.data), `${key} is in the data as ${JSON.stringify(form.data[key])}`); assert.ok(!(key in form.submission.data), `${key} is in the submission`); } assert.strictEqual(form.data.keptWhenHidden, '', 'a clearOnHide: false field still gets its default'); } finally { destroy(); } }); it('treats a customConditional that is only a comment as hidden, as v4 did', async function () { this.timeout(20000); const { form, container, destroy } = await renderForm(schema); try { assert.strictEqual(findComponent(form, 'commentedOut').visible, false); assert.ok(!container.querySelector('input[name^="data[commentedOut]"]'), 'commentedOut is rendered'); } finally { destroy(); } }); it('still seeds a conditionally shown field once it is shown, and drops it again when hidden', async function () { this.timeout(20000); const { form, destroy } = await renderForm(schema); try { form.getComponent('mode').setValue('a', { modified: true }); await settle(); assert.strictEqual(form.data.conditionalField, '', 'shown field was not given its default'); form.getComponent('conditionalField').setValue('typed', { modified: true }); await settle(); form.getComponent('mode').setValue('b', { modified: true }); await settle(); assert.ok(!('conditionalField' in form.data), 'hidden field kept its value'); assert.ok(!('commentedOut' in form.data), 'commentedOut appeared after a data change'); } finally { destroy(); } }); it('keeps data loaded for a hidden field (a submission value is not a seeded default)', async function () { this.timeout(20000); const { form, destroy } = await renderForm(schema, { data: { staticHidden: 'loaded', mode: '' } }); try { assert.strictEqual(form.data.staticHidden, 'loaded'); } finally { destroy(); } }); it('applies to every value type, including after a tab switch', async function () { this.timeout(20000); const hidden = { input: true, hidden: true, clearOnHide: true }; const fields = [ { key: 'selectboxes', type: 'selectboxes', label: 'S', values: [{ label: 'A', value: 'a' }], ...hidden }, { key: 'checkbox', type: 'checkbox', label: 'C', ...hidden }, { key: 'datetime', type: 'datetime', label: 'D', ...hidden }, { key: 'select', type: 'select', label: 'X', dataSrc: 'values', data: { values: [{ label: 'A', value: 'a' }] }, ...hidden }, { key: 'radio', type: 'radio', label: 'R', values: [{ label: 'A', value: 'a' }], ...hidden }, { key: 'number', type: 'number', label: 'N', ...hidden }, { key: 'hiddenType', type: 'hidden', input: true, defaultValue: 'D' }, ]; const typesSchema = { display: 'form', components: [{ key: 'tabs2', type: 'tabs', input: false, components: [{ key: 'a', label: 'A', components: [mode, ...fields] }, { key: 'b', label: 'B', components: [] }], }], }; const { form, container, destroy } = await renderForm(typesSchema); try { form.getComponent('mode').setValue('x', { modified: true }); await settle(); await switchTabsAndBack(container); assert.deepStrictEqual(form.data, { mode: 'x', hiddenType: 'D' }); } finally { destroy(); } }); it('still calculates a hidden field that keeps its value, and a hidden-type field', async function () { this.timeout(20000); const calcSchema = { display: 'form', components: [ mode, textField('keptCalculated', { hidden: true, clearOnHide: false, calculateValue: "value = data.mode + '?'" }), { key: 'hiddenType', type: 'hidden', input: true, calculateValue: "value = data.mode + '#'" }, ], }; const { form, destroy } = await renderForm(calcSchema); try { form.getComponent('mode').setValue('x', { modified: true }); await settle(); assert.strictEqual(form.data.keptCalculated, 'x?'); assert.strictEqual(form.data.hiddenType, 'x#'); } finally { destroy(); } }); it('seeds hidden fields when the form shows hidden fields', async function () { this.timeout(20000); const { form, destroy } = await renderForm( { display: 'form', components: [textField('shown', { hidden: true, defaultValue: 'x' })] }, { formOptions: { showHiddenFields: true } }, ); try { assert.strictEqual(form.data.shown, 'x'); } finally { destroy(); } }); }); describe('real form: reattaching keeps values in layout and nested children', () => { it('carries over values typed into panels, columns and a nested container', async function () { this.timeout(20000); const partyWithLayout = { key: 'party', type: 'container', input: true, tree: true, label: 'Party', clearOnHide: false, components: [ { key: 'panel1', type: 'panel', input: false, components: [textField('inPanel')] }, { key: 'cols', type: 'columns', input: false, columns: [{ width: 6, components: [textField('inColumn')] }, { width: 6, components: [] }] }, { key: 'inner', type: 'container', input: true, tree: true, label: 'Inner', components: [textField('deep')] }, ], }; const layoutSchema = { display: 'form', components: [{ key: 'tabs2', type: 'tabs', input: false, components: [{ key: 'a', label: 'A', components: [partyWithLayout] }, { key: 'b', label: 'B', components: [textField('other')] }], }], }; const { form, container, destroy } = await renderForm(layoutSchema); try { delete form.data.party; const input = (name: string) => container.querySelector(`input[name="${name}"]`) as HTMLInputElement; typeInto(input('data[party][inPanel]'), 'P'); typeInto(input('data[party][inColumn]'), 'C'); typeInto(input('data[party][inner][deep]'), 'D'); await settle(); await switchTabsAndBack(container); assert.deepStrictEqual(form.data.party, { inPanel: 'P', inColumn: 'C', inner: { deep: 'D' } }); } finally { destroy(); } }); });