|
| 1 | +import { PersonAttributeAdapter } from './person-attribute-adapter'; |
| 2 | +import { type FormField, type FormProcessorContextProps } from '../types'; |
| 3 | +import { type FormContextProps } from '../provider/form-provider'; |
| 4 | + |
| 5 | +describe('PersonAttributeAdapter', () => { |
| 6 | + const mockField = { |
| 7 | + id: 'test-person-attribute', |
| 8 | + type: 'personAttribute', |
| 9 | + questionOptions: { |
| 10 | + attributeType: '7ef225db-94db-4e40-9dd8-fb121d9dc370', |
| 11 | + rendering: 'text', |
| 12 | + }, |
| 13 | + meta: {}, |
| 14 | + } satisfies FormField; |
| 15 | + |
| 16 | + const mockContext = { |
| 17 | + patient: { |
| 18 | + id: 'test-patient-uuid', |
| 19 | + } as fhir.Patient, |
| 20 | + } satisfies Partial<FormContextProps> as FormContextProps; |
| 21 | + |
| 22 | + describe('transformFieldValue', () => { |
| 23 | + it('should return null for empty value', () => { |
| 24 | + const result = PersonAttributeAdapter.transformFieldValue(mockField, '', mockContext); |
| 25 | + expect(result).toBeNull(); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should return null when value equals initial value', () => { |
| 29 | + const field = { |
| 30 | + ...mockField, |
| 31 | + meta: { |
| 32 | + submission: {}, |
| 33 | + initialValue: { |
| 34 | + omrsObject: null, |
| 35 | + refinedValue: 'test-value', |
| 36 | + }, |
| 37 | + }, |
| 38 | + } satisfies FormField; |
| 39 | + const result = PersonAttributeAdapter.transformFieldValue(field, 'test-value', mockContext); |
| 40 | + expect(result).toBeNull(); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should transform field value correctly for new attribute', () => { |
| 44 | + const field = { ...mockField }; |
| 45 | + const result = PersonAttributeAdapter.transformFieldValue(field, 'new-attribute-value', mockContext); |
| 46 | + |
| 47 | + expect(result).toEqual({ |
| 48 | + value: 'new-attribute-value', |
| 49 | + attributeType: '7ef225db-94db-4e40-9dd8-fb121d9dc370', |
| 50 | + uuid: undefined, |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should include uuid when updating existing attribute', () => { |
| 55 | + const field = { |
| 56 | + ...mockField, |
| 57 | + meta: { |
| 58 | + submission: {}, |
| 59 | + initialValue: { |
| 60 | + omrsObject: { uuid: 'existing-attr-uuid' }, |
| 61 | + refinedValue: 'old-value', |
| 62 | + }, |
| 63 | + }, |
| 64 | + } satisfies FormField; |
| 65 | + const result = PersonAttributeAdapter.transformFieldValue(field, 'updated-value', mockContext); |
| 66 | + |
| 67 | + expect(result).toEqual({ |
| 68 | + value: 'updated-value', |
| 69 | + attributeType: '7ef225db-94db-4e40-9dd8-fb121d9dc370', |
| 70 | + uuid: 'existing-attr-uuid', |
| 71 | + }); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + describe('getInitialValue', () => { |
| 76 | + it('should return undefined when no person attribute exists', () => { |
| 77 | + const mockProcessorContext = { |
| 78 | + patient: { |
| 79 | + id: 'test-patient', |
| 80 | + extension: [], |
| 81 | + } as fhir.Patient, |
| 82 | + } satisfies Partial<FormProcessorContextProps> as FormProcessorContextProps; |
| 83 | + |
| 84 | + const result = PersonAttributeAdapter.getInitialValue(mockField, null, mockProcessorContext); |
| 85 | + expect(result).toBeUndefined(); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should return valueString when person attribute exists', () => { |
| 89 | + const mockProcessorContext = { |
| 90 | + patient: { |
| 91 | + id: 'test-patient', |
| 92 | + extension: [ |
| 93 | + { |
| 94 | + url: 'http://fhir.openmrs.org/ext/person-attribute/7ef225db-94db-4e40-9dd8-fb121d9dc370', |
| 95 | + valueString: 'test-attribute-value', |
| 96 | + }, |
| 97 | + ], |
| 98 | + } as fhir.Patient, |
| 99 | + } satisfies Partial<FormProcessorContextProps> as FormProcessorContextProps; |
| 100 | + |
| 101 | + const field = { ...mockField }; |
| 102 | + const result = PersonAttributeAdapter.getInitialValue(field, null, mockProcessorContext); |
| 103 | + |
| 104 | + expect(result).toBe('test-attribute-value'); |
| 105 | + expect((field.meta as any).initialValue.refinedValue).toBe('test-attribute-value'); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should return valueReference when person attribute has reference', () => { |
| 109 | + const mockProcessorContext = { |
| 110 | + patient: { |
| 111 | + id: 'test-patient', |
| 112 | + extension: [ |
| 113 | + { |
| 114 | + url: 'http://fhir.openmrs.org/ext/person-attribute/7ef225db-94db-4e40-9dd8-fb121d9dc370', |
| 115 | + valueReference: { |
| 116 | + reference: 'Location/test-location-uuid', |
| 117 | + }, |
| 118 | + }, |
| 119 | + ], |
| 120 | + } as fhir.Patient, |
| 121 | + } satisfies Partial<FormProcessorContextProps> as FormProcessorContextProps; |
| 122 | + |
| 123 | + const field = { ...mockField }; |
| 124 | + const result = PersonAttributeAdapter.getInitialValue(field, null, mockProcessorContext); |
| 125 | + |
| 126 | + expect(result).toBe('Location/test-location-uuid'); |
| 127 | + expect((field.meta as any).initialValue.refinedValue).toBe('Location/test-location-uuid'); |
| 128 | + }); |
| 129 | + }); |
| 130 | + |
| 131 | + describe('getPreviousValue', () => { |
| 132 | + it('should return null', () => { |
| 133 | + const result = PersonAttributeAdapter.getPreviousValue(mockField, null, {} satisfies Partial<FormProcessorContextProps> as FormProcessorContextProps); |
| 134 | + expect(result).toBeNull(); |
| 135 | + }); |
| 136 | + }); |
| 137 | + |
| 138 | + describe('getDisplayValue', () => { |
| 139 | + it('should return display property if present', () => { |
| 140 | + const value = { display: 'Test Display', value: 'test-value' }; |
| 141 | + const result = PersonAttributeAdapter.getDisplayValue(mockField, value); |
| 142 | + expect(result).toBe('Test Display'); |
| 143 | + }); |
| 144 | + |
| 145 | + it('should return value as-is if no display property', () => { |
| 146 | + const value = 'simple-value'; |
| 147 | + const result = PersonAttributeAdapter.getDisplayValue(mockField, value); |
| 148 | + expect(result).toBe('simple-value'); |
| 149 | + }); |
| 150 | + }); |
| 151 | + |
| 152 | + describe('tearDown', () => { |
| 153 | + it('should execute without errors', () => { |
| 154 | + expect(() => PersonAttributeAdapter.tearDown()).not.toThrow(); |
| 155 | + }); |
| 156 | + }); |
| 157 | +}); |
0 commit comments