Skip to content

Commit

Permalink
Added test plus jest env configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
arodidev committed Nov 16, 2023
1 parent 5ebcbfc commit e299564
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const config = {
'react-markdown': '<rootDir>/__mocks__/react-markdown.tsx',
'^dexie$': require.resolve('dexie'),
},
testEnvironment: 'jsdom',
};

module.exports = config;
11 changes: 11 additions & 0 deletions packages/esm-form-render-app/src/form-validator.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/** @jest-environment jsdom */

import { handleFormValidation } from './form-validator';

describe('handleFormValidation', () => {
it('should fail when no schema is passed to the function', async () => {
await expect(() => handleFormValidation(null, null)).rejects.toThrow(
'Invalid argument: "schema" cannot be null, undefined or an empty object. Please provide a valid object.',
);
});
});
13 changes: 11 additions & 2 deletions packages/esm-form-render-app/src/form-validator.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import { openmrsFetch } from '@openmrs/esm-framework';
import { OHRIFormSchema } from '@openmrs/openmrs-form-engine-lib';
import { async } from 'rxjs';

export const handleFormValidation = async (schema, configObject) => {
const errors = [];
const warnings = [];

if (!schema) {
throw new Error('Invalid argument: "schema" cannot be null or undefined. Please provide a valid object.');
if (!schema || !Object.keys(schema)?.length) {
throw new Error(
'Invalid argument: "schema" cannot be null, undefined or an empty object. Please provide a valid object.',
);
}

if (!configObject || !Object.keys(configObject)?.length) {
throw new Error(
'Invalid argument: "configObject" cannot be null, undefined or an empty object. Please provide a valid object.',
);
}

const parsedForm = typeof schema == 'string' ? JSON.parse(schema) : schema;
Expand Down

0 comments on commit e299564

Please sign in to comment.