From 94fd2067adf58a1b72ef8103401bdb43058984e0 Mon Sep 17 00:00:00 2001 From: gitcliff Date: Tue, 2 Jul 2024 22:26:29 +0300 Subject: [PATCH 1/2] (test):Common utils test coverage --- src/utils/common-utils.test.ts | 173 +++++++++++++++++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 src/utils/common-utils.test.ts diff --git a/src/utils/common-utils.test.ts b/src/utils/common-utils.test.ts new file mode 100644 index 000000000..2dfbd1324 --- /dev/null +++ b/src/utils/common-utils.test.ts @@ -0,0 +1,173 @@ +import { flattenObsList, hasRendering, clearSubmission, gracefullySetSubmission, hasSubmission } from './common-utils'; +import { isEmpty } from '../validators/form-validator'; +import { type FormField, type OpenmrsObs } from '../types'; + +jest.mock('@openmrs/esm-framework', () => ({ + formatDate: jest.fn(), + restBaseUrl: 'http://openmrs.com/rest', +})); + +jest.mock('../validators/form-validator', () => ({ + isEmpty: jest.fn(), +})); + +describe('utils functions', () => { + describe('flattenObsList', () => { + it('should flatten a nested obs list', () => { + const obsList: OpenmrsObs[] = [ + { + concept: '7e43b05b-b6d8-4eb5-8f37-0b14f5347368', + obsDatetime: '2023-07-01', + obsGroup: null, + groupMembers: [ + { + concept: '4c43b05b-b6d8-4eb5-8f37-0b14f5347548', + obsDatetime: '2023-07-02', + obsGroup: null, + groupMembers: [], + comment: '', + location: null, + order: null, + encounter: null, + voided: false, + value: null, + formFieldPath: '', + formFieldNamespace: '', + status: '', + interpretation: '', + uuid: '', + }, + ], + comment: '', + location: null, + order: null, + encounter: null, + voided: false, + value: null, + formFieldPath: '', + formFieldNamespace: '', + status: '', + interpretation: '', + uuid: '', + }, + ]; + + const result = flattenObsList(obsList); + expect(result).toHaveLength(2); + }); + }); + + describe('hasRendering', () => { + it('should return true if the field has the specified rendering', () => { + const field: FormField = { + label: 'Test Field', + type: 'obs', + questionOptions: { rendering: 'text' }, + id: 'testFieldId', + } as FormField; + + expect(hasRendering(field, 'text')).toBe(true); + }); + + it('should return false if the field does not have the specified rendering', () => { + const field: FormField = { + label: 'Test Field', + type: 'obs', + questionOptions: { rendering: 'textarea' }, + id: 'testFieldId', + } as FormField; + + expect(hasRendering(field, 'text')).toBe(false); + }); + }); + + describe('clearSubmission', () => { + it('should initialize the submission object if not present and clear values', () => { + const field: FormField = { + label: 'Test Field', + type: 'obs', + questionOptions: {}, + id: 'testFieldId', + meta: {}, + } as FormField; + + clearSubmission(field); + + expect(field.meta.submission).toEqual({ + voidedValue: null, + newValue: null, + }); + }); + }); + + describe('gracefullySetSubmission', () => { + it('should set the newValue and voidedValue correctly', () => { + const field: FormField = { + label: 'Test Field', + type: 'obs', + questionOptions: {}, + id: 'testFieldId', + meta: {}, + } as FormField; + + (isEmpty as jest.Mock).mockReturnValueOnce(false).mockReturnValueOnce(false); + + const newValue = 'new value'; + const voidedValue = 'voided value'; + + gracefullySetSubmission(field, newValue, voidedValue); + + expect(field.meta.submission).toEqual({ + voidedValue: 'voided value', + newValue: 'new value', + }); + }); + + it('should not set values if they are empty', () => { + const field: FormField = { + label: 'Test Field', + type: 'obs', + questionOptions: {}, + id: 'testFieldId', + meta: {}, + } as FormField; + + (isEmpty as jest.Mock).mockReturnValueOnce(true).mockReturnValueOnce(true); + + gracefullySetSubmission(field, '', ''); + + expect(field.meta.submission).toEqual({}); + }); + }); + + describe('hasSubmission', () => { + it('should return true if there is a newValue or voidedValue', () => { + const field: FormField = { + label: 'Test Field', + type: 'obs', + questionOptions: {}, + id: 'testFieldId', + meta: { + submission: { + newValue: 'new value', + voidedValue: 'voided value', + }, + }, + } as FormField; + + expect(hasSubmission(field)).toBe(true); + }); + + it('should return false if there is no newValue or voidedValue', () => { + const field: FormField = { + label: 'Test Field', + type: 'obs', + questionOptions: {}, + id: 'testFieldId', + meta: {}, + } as FormField; + + expect(hasSubmission(field)).toBe(false); + }); + }); +}); From 6ca14dd99aa9ce4bf2a8236042480152d4b0f940 Mon Sep 17 00:00:00 2001 From: gitcliff Date: Wed, 10 Jul 2024 10:13:08 +0300 Subject: [PATCH 2/2] move obs list to the mocks folder --- __mocks__/forms/rfe-forms/obs-list-data.ts | 37 ++++++++++++++++++++ src/utils/common-utils.test.ts | 39 +--------------------- 2 files changed, 38 insertions(+), 38 deletions(-) create mode 100644 __mocks__/forms/rfe-forms/obs-list-data.ts diff --git a/__mocks__/forms/rfe-forms/obs-list-data.ts b/__mocks__/forms/rfe-forms/obs-list-data.ts new file mode 100644 index 000000000..613e62686 --- /dev/null +++ b/__mocks__/forms/rfe-forms/obs-list-data.ts @@ -0,0 +1,37 @@ +export const obsList = [ + { + concept: '7e43b05b-b6d8-4eb5-8f37-0b14f5347368', + obsDatetime: '2023-07-01', + obsGroup: null, + groupMembers: [ + { + concept: '4c43b05b-b6d8-4eb5-8f37-0b14f5347548', + obsDatetime: '2023-07-02', + obsGroup: null, + groupMembers: [], + comment: '', + location: null, + order: null, + encounter: null, + voided: false, + value: null, + formFieldPath: '', + formFieldNamespace: '', + status: '', + interpretation: '', + uuid: '', + }, + ], + comment: '', + location: null, + order: null, + encounter: null, + voided: false, + value: null, + formFieldPath: '', + formFieldNamespace: '', + status: '', + interpretation: '', + uuid: '', + }, +]; diff --git a/src/utils/common-utils.test.ts b/src/utils/common-utils.test.ts index 2dfbd1324..23b62e995 100644 --- a/src/utils/common-utils.test.ts +++ b/src/utils/common-utils.test.ts @@ -1,6 +1,7 @@ import { flattenObsList, hasRendering, clearSubmission, gracefullySetSubmission, hasSubmission } from './common-utils'; import { isEmpty } from '../validators/form-validator'; import { type FormField, type OpenmrsObs } from '../types'; +import { obsList } from '__mocks__/forms/rfe-forms/obs-list-data'; jest.mock('@openmrs/esm-framework', () => ({ formatDate: jest.fn(), @@ -14,44 +15,6 @@ jest.mock('../validators/form-validator', () => ({ describe('utils functions', () => { describe('flattenObsList', () => { it('should flatten a nested obs list', () => { - const obsList: OpenmrsObs[] = [ - { - concept: '7e43b05b-b6d8-4eb5-8f37-0b14f5347368', - obsDatetime: '2023-07-01', - obsGroup: null, - groupMembers: [ - { - concept: '4c43b05b-b6d8-4eb5-8f37-0b14f5347548', - obsDatetime: '2023-07-02', - obsGroup: null, - groupMembers: [], - comment: '', - location: null, - order: null, - encounter: null, - voided: false, - value: null, - formFieldPath: '', - formFieldNamespace: '', - status: '', - interpretation: '', - uuid: '', - }, - ], - comment: '', - location: null, - order: null, - encounter: null, - voided: false, - value: null, - formFieldPath: '', - formFieldNamespace: '', - status: '', - interpretation: '', - uuid: '', - }, - ]; - const result = flattenObsList(obsList); expect(result).toHaveLength(2); });