From d104259e635cee9bb82ebf3611a0c4ae57e149b9 Mon Sep 17 00:00:00 2001 From: Grace Nakiyemba Date: Wed, 11 Nov 2026 18:17:36 +0300 Subject: [PATCH] fix(formulus): load authored form schema version --- formulus/src/services/FormService.ts | 14 ++++- .../services/__tests__/FormService.test.ts | 62 +++++++++++++++++-- 2 files changed, 69 insertions(+), 7 deletions(-) diff --git a/formulus/src/services/FormService.ts b/formulus/src/services/FormService.ts index 80bf4a9ef..0dacafca6 100644 --- a/formulus/src/services/FormService.ts +++ b/formulus/src/services/FormService.ts @@ -31,6 +31,18 @@ export interface FormSpec { uiSchema: unknown; } +function resolveSchemaVersion(schema: unknown): string { + if (schema && typeof schema === 'object') { + const { schemaVersion, version } = schema as Record; + for (const candidate of [schemaVersion, version]) { + if (typeof candidate === 'string' && candidate.trim().length > 0) { + return candidate; + } + } + } + return '1.0'; +} + /** * Service for managing form-related operations */ @@ -172,7 +184,7 @@ export class FormService { id: formDir.name, name: formDir.name, description: 'Form for collecting ' + formDir.name + ' observations', - schemaVersion: '1.0', //TODO: Fix this + schemaVersion: resolveSchemaVersion(schema), schema: schema, uiSchema: uiSchema, }; diff --git a/formulus/src/services/__tests__/FormService.test.ts b/formulus/src/services/__tests__/FormService.test.ts index c3c327cc4..db7671ed7 100644 --- a/formulus/src/services/__tests__/FormService.test.ts +++ b/formulus/src/services/__tests__/FormService.test.ts @@ -26,7 +26,13 @@ jest.mock('react-native-fs', () => ({ readDir: jest.fn(), })); -function setupFormSpecsFromFsMocks() { +function setupFormSpecsFromFsMocks( + schema: Record = { + type: 'object', + properties: { name: { type: 'string' }, age: { type: 'number' } }, + required: ['name'], + }, +) { const RNFS = require('react-native-fs'); const formsPath = '/test/path/forms'; RNFS.exists.mockImplementation(async (p: string) => p === formsPath); @@ -44,11 +50,7 @@ function setupFormSpecsFromFsMocks() { }); RNFS.readFile.mockImplementation(async (path: string) => { if (path.endsWith('/schema.json')) { - return JSON.stringify({ - type: 'object', - properties: { name: { type: 'string' }, age: { type: 'number' } }, - required: ['name'], - }); + return JSON.stringify(schema); } if (path.endsWith('/ui.json')) { return JSON.stringify({ @@ -180,6 +182,54 @@ describe('FormService', () => { }); }); + describe('loaded schema version', () => { + test.each([ + ['schemaVersion', { schemaVersion: '2.0' }, '2.0'], + ['version', { version: '3.0' }, '3.0'], + [ + 'schemaVersion precedence', + { schemaVersion: '2.0', version: '3.0' }, + '2.0', + ], + ['missing metadata', {}, '1.0'], + ['blank schemaVersion', { schemaVersion: '', version: '3.0' }, '3.0'], + [ + 'whitespace schemaVersion', + { schemaVersion: ' \t\n', version: '3.0' }, + '3.0', + ], + ...[null, 2, false, {}, []].map(schemaVersion => [ + `non-string schemaVersion ${JSON.stringify(schemaVersion)}`, + { schemaVersion, version: '3.0' }, + '3.0', + ]), + ...['', ' \t\n', null, 2, false, {}, []].map(version => [ + `invalid version ${JSON.stringify(version)}`, + { version }, + '1.0', + ]), + ['both whitespace-only', { schemaVersion: ' ', version: '\t\n' }, '1.0'], + ['authored whitespace preserved', { schemaVersion: ' 2.0 ' }, ' 2.0 '], + ['nested metadata ignored', { properties: { version: '3.0' } }, '1.0'], + ])( + 'uses the expected version for %s', + async (_label, metadata, expected) => { + const schema = { + type: 'object', + ...(metadata as Record), + }; + setupFormSpecsFromFsMocks(schema); + + await formServiceInstance.invalidateCache(); + + const formSpec = formServiceInstance.getFormSpecById('person'); + expect(formSpec).toBeDefined(); + expect(formSpec?.schemaVersion).toBe(expected); + expect(formSpec?.schema).toEqual(schema); + }, + ); + }); + describe('getFormSpecById', () => { test('should return the correct form type for a valid ID', () => { formServiceInstance.addFormSpec({