Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion formulus/src/services/FormService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>;
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
*/
Expand Down Expand Up @@ -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,
};
Expand Down
62 changes: 56 additions & 6 deletions formulus/src/services/__tests__/FormService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ jest.mock('react-native-fs', () => ({
readDir: jest.fn(),
}));

function setupFormSpecsFromFsMocks() {
function setupFormSpecsFromFsMocks(
schema: Record<string, unknown> = {
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);
Expand All @@ -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({
Expand Down Expand Up @@ -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<string, unknown>),
};
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({
Expand Down
Loading