import assert from 'assert'; import sinon from 'sinon'; import { installCalculatedValueRedrawGuard } from './avoidCalculatedValueRedraw'; class TestComponent { public component: { key?: string; calculateValue?: string; calculateValueVariable?: string; redrawOn?: string | string[]; refreshOn?: string | string[]; } = {}; public refreshed = sinon.stub(); public redrawn = sinon.stub(); public checkRefreshOn(changes: unknown[], flags?: unknown): void { this.refreshed(changes, flags); } public redraw(): unknown { this.redrawn(); return Promise.resolve(); } } class TestVisibleComponent extends TestComponent { private _visible = false; public get visible(): boolean { return this._visible; } public set visible(value: boolean) { if (this._visible !== value) { this._visible = value; this.redraw(); } } } class TestDataGrid { public __gdsDataGridRedrawGuardInstalled?: boolean; public rows: Array> = [ { documentNumber: { id: 'calc-id-1', component: { calculateValue: 'value = row.number' } }, age: { id: 'calc-id-var-1', component: { calculateValueVariable: 'age' } }, number: { id: 'plain-id-2', component: {} }, }, ]; public dataValue: unknown[] = [{}]; public component = {}; public setValueCalled = sinon.stub(); public redrawn = sinon.stub(); public setValue(value: unknown, flags?: Record): boolean { this.setValueCalled(value, flags); this.redraw(); return true; } public redraw(): Promise { this.redrawn(); return Promise.resolve(); } } describe('installCalculatedValueRedrawGuard', () => { before(() => { installCalculatedValueRedrawGuard(TestComponent); }); it('skips the redundant data redraw for a calculated field with redrawOn: "data"', () => { const component = new TestComponent(); component.component.calculateValue = 'value = row.number'; component.component.redrawOn = 'data'; component.checkRefreshOn([{ value: 'new value' }], {}); assert.strictEqual(component.refreshed.notCalled, true); }); it('skips the redraw even when redrawOn is not set (any calculated field)', () => { const component = new TestComponent(); component.component.calculateValue = 'value = row.number'; component.checkRefreshOn([{ value: 'new value' }], {}); assert.strictEqual(component.refreshed.notCalled, true); }); it('skips the redraw for a calculateValueVariable field', () => { const component = new TestComponent(); component.component.calculateValueVariable = 'age'; component.checkRefreshOn([{ value: 23 }], {}); assert.strictEqual(component.refreshed.notCalled, true); }); it('skips the redraw in a secondary cycle where changes is empty but flags.changed is set', () => { const component = new TestComponent(); component.component.calculateValue = 'value = row.number'; component.component.redrawOn = 'data'; const flags = { changes: [], changed: { instance: {}, value: 23 } }; component.checkRefreshOn([], flags); assert.strictEqual(component.refreshed.notCalled, true); }); it('preserves redrawOn data for a field without a calculation', () => { const component = new TestComponent(); component.component.redrawOn = 'data'; const changes = [{ value: 'new value' }]; const flags = { changed: true }; component.checkRefreshOn(changes, flags); assert.strictEqual(component.refreshed.calledOnceWithExactly(changes, flags), true); }); it('preserves an explicit refreshOn dependency', () => { const component = new TestComponent(); component.component.calculateValue = 'value = row.number'; component.component.redrawOn = 'data'; component.component.refreshOn = 'number'; component.checkRefreshOn([], {}); assert.strictEqual(component.refreshed.calledOnce, true); }); it('installs only once', () => { const installedMethod = TestComponent.prototype.checkRefreshOn; installCalculatedValueRedrawGuard(TestComponent); assert.strictEqual(TestComponent.prototype.checkRefreshOn, installedMethod); }); describe('redraw()', () => { it('blocks redraw for a calculated field regardless of the call site', () => { const component = new TestComponent(); component.component.calculateValue = 'value = row.number'; component.component.redrawOn = 'data'; const result = component.redraw(); assert.strictEqual(component.redrawn.notCalled, true); assert.ok(result instanceof Promise); }); it('blocks redraw for a calculateValueVariable field', () => { const component = new TestComponent(); component.component.calculateValueVariable = 'age'; const result = component.redraw(); assert.strictEqual(component.redrawn.notCalled, true); assert.ok(result instanceof Promise); }); it('allows redraw for a field without calculateValue', () => { const component = new TestComponent(); component.component.redrawOn = 'data'; component.redraw(); assert.strictEqual(component.redrawn.calledOnce, true); }); it('allows redraw for a calculated field with an explicit refreshOn', () => { const component = new TestComponent(); component.component.calculateValue = 'value = row.number'; component.component.refreshOn = 'number'; component.redraw(); assert.strictEqual(component.redrawn.calledOnce, true); }); }); describe('visibility changes', () => { before(() => { installCalculatedValueRedrawGuard(TestVisibleComponent); }); it('redraws a calculated field when it becomes visible', () => { const component = new TestVisibleComponent(); component.component.calculateValue = 'value = row.number'; component.visible = true; assert.strictEqual(component.redrawn.calledOnce, true); }); it('redraws a calculated field when it becomes hidden', () => { const component = new TestVisibleComponent(); component.component.calculateValue = 'value = row.number'; component.visible = true; component.redrawn.resetHistory(); component.visible = false; assert.strictEqual(component.redrawn.calledOnce, true); }); it('redraws a calculateValueVariable field when it becomes visible', () => { const component = new TestVisibleComponent(); component.component.calculateValueVariable = 'age'; component.visible = true; assert.strictEqual(component.redrawn.calledOnce, true); }); it('still blocks a plain redraw of a calculated field outside a visibility change', () => { const component = new TestVisibleComponent(); component.component.calculateValue = 'value = row.number'; component.visible = true; component.redrawn.resetHistory(); component.redraw(); assert.strictEqual(component.redrawn.notCalled, true); }); it('does not leave the bypass active after the visibility change completes', () => { const component = new TestVisibleComponent(); component.component.calculateValue = 'value = row.number'; component.visible = true; component.redrawn.resetHistory(); component.redraw(); assert.strictEqual(component.redrawn.notCalled, true); }); it('clears the bypass even if the visibility change throws', () => { class ThrowingComponent extends TestVisibleComponent { public set visible(value: boolean) { if (value) { throw new Error('boom'); } } public get visible(): boolean { return false; } } installCalculatedValueRedrawGuard(ThrowingComponent); const component = new ThrowingComponent(); component.component.calculateValue = 'value = row.number'; assert.throws(() => { component.visible = true; }, /boom/); component.redraw(); assert.strictEqual(component.redrawn.notCalled, true); }); it('does not change redraw behaviour for a non-calculated field becoming visible', () => { const component = new TestVisibleComponent(); component.visible = true; assert.strictEqual(component.redrawn.calledOnce, true); }); }); describe('DataGrid.setValue() — redraw suppression', () => { before(() => { const FreshComponent = class extends TestComponent {}; installCalculatedValueRedrawGuard( FreshComponent as unknown as Parameters[0], TestDataGrid as unknown as Parameters[1], ); }); it('suppresses DataGrid.redraw() when triggeredComponentId is a calculated child', () => { const dg = new TestDataGrid(); dg.setValue([{ documentNumber: 'Doc 1', number: '123' }], { triggeredComponentId: 'calc-id-1', }); assert.strictEqual(dg.setValueCalled.calledOnce, true, 'setValue was called'); assert.strictEqual(dg.redrawn.notCalled, true, 'redraw was suppressed'); }); it('allows DataGrid.redraw() when triggeredComponentId is a plain (non-calculated) child', () => { const dg = new TestDataGrid(); dg.setValue([{ documentNumber: 'Doc 1', number: '456' }], { triggeredComponentId: 'plain-id-2', }); assert.strictEqual(dg.setValueCalled.calledOnce, true, 'setValue was called'); assert.strictEqual(dg.redrawn.calledOnce, true, 'redraw was allowed'); }); it('allows DataGrid.redraw() when there is no triggeredComponentId (user add/remove row)', () => { const dg = new TestDataGrid(); dg.setValue([{ documentNumber: 'Doc 1', number: '789' }], {}); assert.strictEqual(dg.redrawn.calledOnce, true, 'redraw was allowed'); }); it('suppresses DataGrid.redraw() when triggeredComponentId is a calculateValueVariable child', () => { const dg = new TestDataGrid(); dg.setValue([{ age: 25, number: '123' }], { triggeredComponentId: 'calc-id-var-1', }); assert.strictEqual(dg.setValueCalled.calledOnce, true, 'setValue was called'); assert.strictEqual(dg.redrawn.notCalled, true, 'redraw was suppressed for calculateValueVariable child'); }); }); describe('NestedComponent.checkData() — row scoping fix', () => { before(() => { const FreshComponent = class extends TestComponent {}; const FreshDataGrid = class extends TestDataGrid {}; class TestNested { public __gdsNestedCheckDataGuardInstalled?: boolean; public builderMode = false; public data: Record = { number: '42' }; public childCheckDataRow: unknown = undefined; public childComp = { data: { number: '42' }, checkData: (_d: unknown, _f: unknown, r: unknown) => { (this as any).childCheckDataRow = r; // eslint-disable-line @typescript-eslint/no-explicit-any }, }; public getComponents() { return [this.childComp]; } public checkData(_data: unknown, _flags: unknown, _row: unknown, _components?: unknown[]) {} } const formioModule = require('@formio/js') as { Components: { components: Record }; }; const origNested = formioModule.Components.components.nested; formioModule.Components.components.nested = TestNested; installCalculatedValueRedrawGuard( FreshComponent as unknown as Parameters[0], FreshDataGrid as unknown as Parameters[1], ); formioModule.Components.components.nested = origNested; (globalThis as Record).__testNested = TestNested; }); it('passes comp.data as row to child checkData instead of the ancestor row', () => { const TestNested = (globalThis as Record).__testNested as { new(): { builderMode: boolean; data: Record; childCheckDataRow: unknown; childComp: { data: Record; checkData: (d: unknown, f: unknown, r: unknown) => void }; getComponents(): Array<{ data: Record; checkData: (d: unknown, f: unknown, r: unknown) => void }>; checkData(d: unknown, f: unknown, r: unknown, c?: unknown[]): void; __gdsNestedCheckDataGuardInstalled?: boolean; }; }; const nested = new TestNested(); assert.strictEqual( (TestNested.prototype as any).__gdsNestedCheckDataGuardInstalled, // eslint-disable-line @typescript-eslint/no-explicit-any true, 'nested guard was installed', ); const wrongRow = { people: [{ ids: [{ number: '42' }] }] }; nested.checkData({}, {}, wrongRow); assert.deepStrictEqual( nested.childCheckDataRow, nested.childComp.data, 'child received comp.data as row, not ancestor row', ); }); it('skips child iteration entirely when builderMode is true', () => { const TestNested = (globalThis as Record).__testNested as { new(): { builderMode: boolean; data: Record; childCheckDataRow: unknown; childComp: { data: Record; checkData: (d: unknown, f: unknown, r: unknown) => void }; getComponents(): Array<{ data: Record; checkData: (d: unknown, f: unknown, r: unknown) => void }>; checkData(d: unknown, f: unknown, r: unknown, c?: unknown[]): void; }; }; const nested = new TestNested(); nested.builderMode = true; nested.checkData({}, {}, {}); assert.strictEqual( nested.childCheckDataRow, undefined, 'child checkData was not called in builderMode', ); }); it('falls back to effectiveRow when comp.data is undefined', () => { const TestNested = (globalThis as Record).__testNested as { new(): { builderMode: boolean; data: Record; childCheckDataRow: unknown; childComp: { data: Record; checkData: (d: unknown, f: unknown, r: unknown) => void }; getComponents(): Array<{ data: Record; checkData: (d: unknown, f: unknown, r: unknown) => void }>; checkData(d: unknown, f: unknown, r: unknown, c?: unknown[]): void; }; }; const nested = new TestNested(); (nested.childComp as Record).data = undefined as unknown as Record; const effectiveRow = { number: 'fallback' }; nested.checkData({}, {}, effectiveRow); assert.deepStrictEqual( nested.childCheckDataRow, effectiveRow, 'child received effectiveRow as fallback when comp.data is undefined', ); }); it('does not re-patch a nested subclass that already has its own guard installed', () => { const formioModule = require('@formio/js') as { Components: { components: Record }; }; class SubNested { public __gdsNestedCheckDataGuardInstalled?: boolean; public builderMode = false; public data: Record = {}; public getComponents() { return []; } public checkData(_d: unknown, _f: unknown, _r: unknown, _c?: unknown[]) {} } const origNested = formioModule.Components.components.nested; formioModule.Components.components.nested = SubNested; const FreshComponent2 = class extends TestComponent {}; const FreshDataGrid2 = class extends TestDataGrid {}; installCalculatedValueRedrawGuard( FreshComponent2 as unknown as Parameters[0], FreshDataGrid2 as unknown as Parameters[1], ); formioModule.Components.components.nested = origNested; assert.strictEqual( Object.prototype.hasOwnProperty.call(SubNested.prototype, '__gdsNestedCheckDataGuardInstalled'), true, 'subclass received its own guard', ); const methodBefore = SubNested.prototype.checkData; formioModule.Components.components.nested = SubNested; installCalculatedValueRedrawGuard( FreshComponent2 as unknown as Parameters[0], FreshDataGrid2 as unknown as Parameters[1], ); formioModule.Components.components.nested = origNested; assert.strictEqual( SubNested.prototype.checkData, methodBefore, 'second install on same subclass did not replace the method', ); }); }); });