From 98f1549237fe69957826fc505792f73463c37bcc Mon Sep 17 00:00:00 2001 From: Joel Wetzell Date: Fri, 3 Apr 2026 19:12:00 -0500 Subject: [PATCH] add tests for params utils --- src/app/utils/params.spec.ts | 127 +++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 src/app/utils/params.spec.ts diff --git a/src/app/utils/params.spec.ts b/src/app/utils/params.spec.ts new file mode 100644 index 0000000..5b76548 --- /dev/null +++ b/src/app/utils/params.spec.ts @@ -0,0 +1,127 @@ +import { SomeJSONSchema } from 'ajv/dist/types/json-schema'; +import { ParamsFormInfo } from '../models/form.model'; +import { FormGroup, FormControl } from '@angular/forms'; +import { schemaToParamsFormInfo } from './params'; + +describe('params utils - schema to form', () => { + const tests: { name: string; schema: SomeJSONSchema; formInfo: ParamsFormInfo }[] = [ + { + name: 'basic string property', + schema: { + type: 'object', + properties: { + name: { + title: 'Name', + type: 'string', + }, + }, + required: [], + }, + formInfo: { + formGroup: new FormGroup({ + name: new FormControl(''), + }), + paramsInfo: { + name: { + key: 'name', + display: 'Name', + type: 'string', + hint: undefined, + isConst: false, + schema: { + title: 'Name', + type: 'string', + }, + placeholder: '', + default: undefined, + }, + }, + }, + }, + { + name: 'basic string property w/ default', + schema: { + type: 'object', + properties: { + name: { + title: 'Name', + type: 'string', + default: 'John Doe', + }, + }, + required: [], + }, + formInfo: { + formGroup: new FormGroup({ + name: new FormControl('John Doe'), + }), + paramsInfo: { + name: { + key: 'name', + display: 'Name', + type: 'string', + hint: undefined, + isConst: false, + schema: { + title: 'Name', + type: 'string', + default: 'John Doe', + }, + placeholder: '', + default: 'John Doe', + }, + }, + }, + }, + { + name: 'basic string property w/ description', + schema: { + type: 'object', + properties: { + name: { + title: 'Name', + description: 'Name of the thing', + type: 'string', + }, + }, + required: [], + }, + formInfo: { + formGroup: new FormGroup({ + name: new FormControl(''), + }), + paramsInfo: { + name: { + key: 'name', + display: 'Name', + type: 'string', + hint: 'Name of the thing', + isConst: false, + schema: { + title: 'Name', + description: 'Name of the thing', + type: 'string', + }, + placeholder: '', + default: undefined, + }, + }, + }, + }, + ]; + + tests.forEach((testCase) => { + test(test.name, () => { + const result = schemaToParamsFormInfo(testCase.schema); + expect(result.paramsInfo).toEqual(testCase.formInfo.paramsInfo); + expect(Object.keys(result.formGroup.controls)).toEqual( + Object.keys(testCase.formInfo.formGroup.controls), + ); + Object.entries(result.formGroup.controls).forEach(([key, control]) => { + const expectedControl = testCase.formInfo.formGroup.controls[key]; + expect(expectedControl).toBeDefined(); + expect(control.value).toEqual(expectedControl?.value); + }); + }); + }); +});