/* eslint-disable @typescript-eslint/no-explicit-any */ import assert from 'assert'; (global as any).matchMedia = (global as any).matchMedia || (() => ({ matches: false, addListener: () => {}, removeListener: () => {}, })); const mockDoc: Record = { createElement: (): Record => ({ setAttribute: () => {}, style: {}, classList: { add: () => {}, remove: () => {} }, addEventListener: () => {}, removeEventListener: () => {}, querySelector: () => null, querySelectorAll: () => [], }), documentElement: { style: {} }, head: { appendChild: () => {} }, body: { appendChild: () => null, querySelector: () => null, querySelectorAll: () => [] }, getElementsByTagName: (): any[] => [], addEventListener: () => {}, removeEventListener: () => {}, cookie: '', }; (global as any).document = mockDoc; const mockWindow: Record = { addEventListener: () => {}, removeEventListener: () => {}, document: mockDoc, navigator: { userAgent: 'node', cookieEnabled: true }, localStorage: { getItem: () => null, setItem: () => {}, removeItem: () => {} }, sessionStorage: { getItem: () => null, setItem: () => {}, removeItem: () => {} }, location: { href: 'http://localhost', origin: 'http://localhost', protocol: 'http:', host: 'localhost', hostname: 'localhost', port: '', pathname: '/', search: '', hash: '', }, }; (global as any).window = mockWindow; Object.defineProperty(global, 'navigator', { value: mockWindow.navigator, writable: true, configurable: true, }); import { Components } from '@formio/js'; import { installCalculatedValueRedrawGuard } from './avoidCalculatedValueRedraw'; import { installDeferredPristineErrors } from './deferPristineErrors'; const { components } = Components as any; function stubRender(instance: any): void { const stub = (comp: any) => { comp.element = comp.element || { parentNode: { children: [] } }; comp.render = () => '
'; comp.sanitize = (html: string) => html; comp.setElement = () => {}; comp.attach = () => Promise.resolve(); comp.detach = () => {}; }; stub(instance); const recurse = (comp: any) => { if (typeof comp.getComponents === 'function') { comp.getComponents().forEach((child: any) => { stub(child); recurse(child); }); } if (Array.isArray(comp.tabs)) { comp.tabs.forEach((tab: any[]) => tab.forEach((child: any) => { stub(child); recurse(child); })); } }; recurse(instance); } // Tab 0: radio plus a container shown when `row.consigneeKnown === 'yes'`; tab 1: a textfield const CONTAINER_SCHEMA = { key: 'consignee', type: 'container', input: true, label: 'Consignee', tree: true, clearOnHide: true, customConditional: 'show = (row.consigneeKnown === "yes") ? true : false', components: [ { key: 'companyName', type: 'textfield', input: true, label: 'Company name', clearOnHide: true, persistent: true, }, ], }; const TABS_SCHEMA = { key: 'tabs', type: 'tabs', input: false, persistent: false, components: [ { key: 'tab0', label: 'Tab 0', components: [ { key: 'consigneeKnown', type: 'radio', input: true, label: 'Do you know the consignee?', values: [ { label: 'Yes', value: 'yes' }, { label: 'No', value: 'no' }, ], clearOnHide: true, persistent: true, }, CONTAINER_SCHEMA, ], }, { key: 'tab1', label: 'Tab 1', components: [ { key: 'otherField', type: 'textfield', input: true, label: 'Other field', clearOnHide: true, persistent: true, }, ], }, ], }; function buildTabs(data: Record): any { const TabsCtor = components.tabs; const instance = new TabsCtor(TABS_SCHEMA, {}, data); instance.root = instance; instance.data = data; stubRender(instance); return instance; } function simulateTabSwitch(tabs: any, index: number): void { tabs.currentTab = index; tabs.setValue(tabs.data); } function findByKey(tabs: any, key: string): any | undefined { for (const tab of tabs.tabs) { for (const comp of tab) { if (comp.key === key) return comp; if (typeof comp.getComponents === 'function') { for (const child of comp.getComponents()) { if (child.key === key) return child; } } } } return undefined; } describe('tab-switch data-clearing bug', () => { before(() => { installCalculatedValueRedrawGuard(); installDeferredPristineErrors(); }); // A tree container evaluates `customConditional` against its own data, not the // parent row, so the first tab switch wiped it it('container data survives the first tab switch away and back — tree container (BUG 1 regression)', () => { const data: Record = { consigneeKnown: 'yes', consignee: { companyName: 'ACME Ltd' }, }; const tabs = buildTabs(data); const container = findByKey(tabs, 'consignee'); assert.ok(container, 'container component not found in tree'); // A tree container's data is its own sub-object, which exposes the bug container.data = data.consignee; simulateTabSwitch(tabs, 1); simulateTabSwitch(tabs, 0); assert.ok( data.consignee !== undefined && data.consignee !== null, 'container data was deleted on first tab switch away-and-back', ); assert.deepStrictEqual( data.consignee, { companyName: 'ACME Ltd' }, 'container data was wiped on first tab switch away-and-back', ); }); it('radio value (consigneeKnown) is not cleared by tab switch', () => { const data: Record = { consigneeKnown: 'yes', consignee: { companyName: 'ACME Ltd' }, }; const tabs = buildTabs(data); simulateTabSwitch(tabs, 1); simulateTabSwitch(tabs, 0); assert.strictEqual( data.consigneeKnown, 'yes', 'radio value should not be cleared by tab switch', ); }); it('container data survives multiple tab switches', () => { const data: Record = { consigneeKnown: 'yes', consignee: { companyName: 'ACME Ltd' }, }; const tabs = buildTabs(data); const container = findByKey(tabs, 'consignee'); assert.ok(container, 'container not found'); container.data = data.consignee; for (let i = 0; i < 4; i++) { simulateTabSwitch(tabs, 1); simulateTabSwitch(tabs, 0); assert.ok( data.consignee !== undefined && data.consignee !== null, `container data was deleted on tab switch iteration ${i}`, ); } }); it('does not nest a copy inside a visible tree container that already has data', () => { const data: Record = { known: 'yes', group: { name: 'Example' } }; const PanelCtor = components.panel; const root = new PanelCtor({ key: 'root', type: 'panel', input: false, components: [ { key: 'known', type: 'radio', input: true, label: 'Known?', values: [{ label: 'Yes', value: 'yes' }] }, { key: 'group', type: 'container', input: true, tree: true, label: 'Group', clearOnHide: true, customConditional: 'show = (row.known === "yes") ? true : false', components: [{ key: 'name', type: 'textfield', input: true, label: 'Name' }], }, ], }, {}, data); root.root = root; root.data = data; stubRender(root); const container = root.getComponents()[1]; container.data = data.group; container.clearComponentOnHide(); assert.deepStrictEqual(data.group, { name: 'Example' }, 'a copy of the container was nested inside its own data'); }); }); describe('NestedComponent.checkData double-iteration (BUG 2 — input lag)', () => { before(() => { installCalculatedValueRedrawGuard(); }); it('calls child checkData exactly once per checkData call on the nested parent', () => { const ContainerCtor = components.container; const childCallCount: number[] = []; const childSchema = { key: 'field1', type: 'textfield', input: true, }; const containerData: Record = { field1: 'hello' }; const container = new ContainerCtor( { key: 'wrapper', type: 'container', components: [childSchema] }, {}, containerData, ); container.root = container; container.data = containerData; stubRender(container); for (const child of container.getComponents()) { const orig = child.checkData.bind(child); child.checkData = (...args: any[]) => { childCallCount.push(1); return orig(...args); }; } container.checkData(containerData, {}, containerData); assert.strictEqual( childCallCount.length, 1, `child checkData was called ${childCallCount.length} times instead of 1 — double-iteration bug still present`, ); }); }); const SECTION_CONDITIONAL = 'show = data.options.map(function (o) { return o.option.code; }).includes("SHOW");'; function sectionSchema(overrides: Record = {}): Record { return { key: 'section', type: 'container', input: true, label: 'Section', tree: true, clearOnHide: true, defaultValue: null, customConditional: SECTION_CONDITIONAL, components: [ { key: 'answer', type: 'radio', input: true, label: 'Answer?', values: [ { label: 'Yes', value: 'yes' }, { label: 'No', value: 'no' }, ], clearOnHide: true, persistent: true, }, ], ...overrides, }; } function buildSectionRoot(data: Record, overrides: Record = {}): any { const PanelCtor = components.panel; const root = new PanelCtor( { key: 'root', type: 'panel', input: false, components: [sectionSchema(overrides)] }, {}, data, ); root.root = root; root.data = data; stubRender(root); return root; } function revealSection(root: any, data: Record): void { data.options = [{ option: { code: 'SHOW' } }]; root.checkConditions(data, {}, data); } describe('conditionally revealed container gets its data attached to the root data', () => { before(() => { installCalculatedValueRedrawGuard(); installDeferredPristineErrors(); }); it('creates data.section when the container is revealed and has no value', () => { const data: Record = { options: [] }; const root = buildSectionRoot(data); const container = root.getComponents()[0]; assert.ok(!('section' in data), 'precondition: section must be absent while hidden'); revealSection(root, data); assert.ok('section' in data, 'data.section was not created when the container was revealed'); assert.strictEqual(container.dataValue, data.section, 'container data is detached from root data'); }); it('writes child values into the root data after the container is revealed', () => { const data: Record = { options: [] }; const root = buildSectionRoot(data); const container = root.getComponents()[0]; revealSection(root, data); container.getComponents()[0].setValue('no'); assert.deepStrictEqual(data.section, { answer: 'no' }); }); it('leaves existing container data untouched when revealed', () => { const data: Record = { options: [], section: { answer: 'yes' } }; const root = buildSectionRoot(data); revealSection(root, data); assert.deepStrictEqual(data.section, { answer: 'yes' }); }); it('attaches a revealed clearOnHide: false container', () => { const data: Record = { options: [] }; const root = buildSectionRoot(data, { clearOnHide: false }); delete data.section; revealSection(root, data); assert.ok(data.section && typeof data.section === 'object', 'the revealed container was not attached'); root.getComponents()[0].getComponents()[0].setValue('no'); assert.deepStrictEqual(data.section, { answer: 'no' }, 'child values do not reach the root data'); }); it('leaves a hidden clearOnHide: false container detached', () => { const data: Record = { options: [] }; const root = buildSectionRoot(data, { clearOnHide: false }); delete data.section; root.checkConditions(data, {}, data); assert.ok(!('section' in data), 'a hidden container was given data'); }); it('finds existing data under a dotted key instead of resetting it', () => { const data: Record = { options: [], details: { section: { answer: 'yes' } } }; const root = buildSectionRoot(data, { key: 'details.section' }); revealSection(root, data); assert.deepStrictEqual(data.details.section, { answer: 'yes' }); }); }); describe('a conditional field inside a revealed container keeps its value', () => { before(() => { installCalculatedValueRedrawGuard(); installDeferredPristineErrors(); }); it('does not reset a filled child with its own customConditional', () => { const data: Record = { outer: { inner: 'yes', name: 'Example' } }; const PanelCtor = components.panel; const root = new PanelCtor({ key: 'root', type: 'panel', input: false, components: [{ key: 'outer', type: 'container', input: true, tree: true, label: 'Outer', clearOnHide: false, components: [ { key: 'inner', type: 'radio', input: true, label: 'Inner', values: [{ label: 'Yes', value: 'yes' }] }, { key: 'name', type: 'textfield', input: true, label: 'Name', clearOnHide: true, defaultValue: 'DEFAULT', customConditional: 'show = row.inner === "yes";' }, ], }], }, {}, data); root.root = root; root.data = data; stubRender(root); const name = root.getComponents()[0].getComponents()[1]; name.clearComponentOnHide(); assert.deepStrictEqual(data.outer, { inner: 'yes', name: 'Example' }); }); });