import assert from 'assert'; import sinon from 'sinon'; import { installDeferredPristineErrors } from './deferPristineErrors'; class TestComponent { public __gdsValidationAttempted?: boolean; public component: { clearOnHide?: boolean } = {}; public pristine = true; public root?: TestWizard; public setCustomValidity = sinon.stub().returns([]); public visible = true; public nextVisible?: boolean; public checkComponentConditions(): boolean { if (this.nextVisible !== undefined) { this.visible = this.nextVisible; } return this.visible; } public setPristine(pristine: boolean): void { this.pristine = pristine; } public setComponentValidity( errors: unknown[], dirty: boolean, silentCheck: boolean, ): unknown { return { errors, dirty, silentCheck }; } } class TestWizard { public __gdsValidationAttemptInProgress?: boolean; public component?: TestComponent; public submitting = false; public nextPage(): unknown { return this.component?.setComponentValidity( [{ level: 'error', message: 'Required' }], true, false, ); } } describe('installDeferredPristineErrors', () => { before(() => { installDeferredPristineErrors(TestComponent, TestWizard); }); it('does not present errors for a pristine component during an unrelated change', () => { const component = new TestComponent(); const result = component.setComponentValidity( [{ level: 'error', message: 'Required' }], false, false, ); assert.deepStrictEqual(result, []); assert.strictEqual(component.setCustomValidity.calledOnceWithExactly(''), true); }); it('delegates dirty validation used by Next', () => { const component = new TestComponent(); const wizard = new TestWizard(); component.root = wizard; wizard.component = component; const errors = [{ level: 'error', message: 'Required' }]; const result = wizard.nextPage(); assert.deepStrictEqual(result, { errors, dirty: true, silentCheck: false }); assert.strictEqual(component.setCustomValidity.notCalled, true); assert.strictEqual(component.__gdsValidationAttempted, true); }); it('delegates dirty validation while a form is submitting', () => { const component = new TestComponent(); const wizard = new TestWizard(); wizard.submitting = true; component.root = wizard; const errors = [{ level: 'error', message: 'Required' }]; const result = component.setComponentValidity(errors, true, false); assert.deepStrictEqual(result, { errors, dirty: true, silentCheck: false }); assert.strictEqual(component.__gdsValidationAttempted, true); }); it('does not apply an earlier submission attempt to a newly revealed component', () => { const component = new TestComponent(); component.root = new TestWizard(); const result = component.setComponentValidity( [{ level: 'error', message: 'Required' }], true, false, ); assert.deepStrictEqual(result, []); assert.strictEqual(component.setCustomValidity.calledOnceWithExactly(''), true); assert.strictEqual(component.__gdsValidationAttempted, undefined); }); it('does not mark a conditionally hidden field during Next before it is revealed', () => { const component = new TestComponent(); const wizard = new TestWizard(); component.root = wizard; component.visible = false; wizard.component = component; const hiddenResult = wizard.nextPage(); assert.deepStrictEqual(hiddenResult, []); assert.strictEqual(component.__gdsValidationAttempted, undefined); component.visible = true; component.setCustomValidity.resetHistory(); const revealedResult = component.setComponentValidity( [{ level: 'error', message: 'Mode of transport is required' }], true, false, ); assert.deepStrictEqual(revealedResult, []); assert.strictEqual(component.setCustomValidity.calledOnceWithExactly(''), true); assert.strictEqual(component.__gdsValidationAttempted, undefined); }); it('resets a clear-on-hide field when it is hidden and revealed again', () => { const component = new TestComponent(); component.pristine = false; component.__gdsValidationAttempted = true; component.nextVisible = false; const hiddenResult = component.checkComponentConditions(); assert.strictEqual(hiddenResult, false); assert.strictEqual(component.pristine, true); assert.strictEqual(component.__gdsValidationAttempted, undefined); assert.strictEqual(component.setCustomValidity.calledOnceWithExactly(''), true); component.visible = true; component.setCustomValidity.resetHistory(); const revealedResult = component.setComponentValidity( [{ level: 'error', message: 'Required' }], false, false, ); assert.deepStrictEqual(revealedResult, []); assert.strictEqual(component.setCustomValidity.calledOnceWithExactly(''), true); }); it('preserves validation state when clearOnHide is disabled', () => { const component = new TestComponent(); component.component.clearOnHide = false; component.pristine = false; component.__gdsValidationAttempted = true; component.nextVisible = false; component.checkComponentConditions(); assert.strictEqual(component.pristine, false); assert.strictEqual(component.__gdsValidationAttempted, true); assert.strictEqual(component.setCustomValidity.notCalled, true); }); it('delegates validation after a component has changed', () => { const component = new TestComponent(); component.pristine = false; const errors = [{ level: 'error', message: 'Required' }]; const result = component.setComponentValidity(errors, false, false); assert.deepStrictEqual(result, { errors, dirty: false, silentCheck: false }); assert.strictEqual(component.setCustomValidity.notCalled, true); }); it('installs only once', () => { const installedMethod = TestComponent.prototype.setComponentValidity; installDeferredPristineErrors(TestComponent, TestWizard); assert.strictEqual(TestComponent.prototype.setComponentValidity, installedMethod); }); });