Skip to content

Commit

Permalink
(test) Adding test coverage for encounter location handler (#305)
Browse files Browse the repository at this point in the history
* (test):Adding test coverage for encounter location handler

* (test): reuse a single field object across the suite
  • Loading branch information
gitcliff authored Jun 6, 2024
1 parent 30e7a1f commit 34d7cfb
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 3 deletions.
180 changes: 180 additions & 0 deletions src/submission-handlers/encounterLocationHandler.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
import { type EncounterContext } from '../form-context';
import { type FormField } from '../types';
import { EncounterLocationSubmissionHandler } from './encounterLocationHandler';
import { getAllLocations } from '../api/api';

jest.mock('../api/api');

const mockedGetAllLocations = getAllLocations as jest.MockedFunction<typeof getAllLocations>;

const encounterWithLocation = {
uuid: '773455da-3ec4-453c-b565-7c1fe35426be',
location: {
uuid: '81e6e516-c1f0-11eb-8529-0242ac130003',
},
encounterProviders: [],
obs: [],
};

const encounterWithoutLocation = {
uuid: '773455da-3ec4-453c-b565-7c1fe35426be',
encounterProviders: [],
obs: [],
location: undefined,
};

const contextWithoutLocation: EncounterContext = {
patient: {
id: '833eb896-c1f0-11eb-8529-0242ac130003',
},
encounter: encounterWithoutLocation,
sessionMode: 'enter',
encounterDate: new Date(2020, 11, 29),
setEncounterDate: (value) => {},
encounterProvider: '2c95f6f5-788e-4e73-9079-5626911231fa',
setEncounterProvider: jest.fn,
setEncounterLocation: jest.fn(),
encounterRole: '6d95f6f5-788e-4e73-9079-5626911231f4',
setEncounterRole: jest.fn,
location: undefined,
};

const encounterContext: EncounterContext = {
patient: {
id: '833eb896-c1f0-11eb-8529-0242ac130003',
},
location: {
uuid: '81e6e516-c1f0-11eb-8529-0242ac130003',
},
encounter: {
uuid: '773455da-3ec4-453c-b565-7c1fe35426be',
encounterProviders: [],
obs: [],
},
sessionMode: 'enter',
encounterDate: new Date(2020, 11, 29),
setEncounterDate: (value) => {},
encounterProvider: '2c95f6f5-788e-4e73-9079-5626911231fa',
setEncounterProvider: jest.fn,
setEncounterLocation: jest.fn(),
encounterRole: '6d95f6f5-788e-4e73-9079-5626911231f4',
setEncounterRole: jest.fn,
};

describe('EncounterLocationSubmissionHandler', () => {
let field: FormField;

beforeEach(() => {
// Define the field once before each test
field = {
label: 'Encounter Location',
type: 'encounterLocation',
required: false,
id: 'encounterLocation',
questionOptions: {
rendering: 'ui-select-extended',
},
validators: [],
};
});

afterEach(() => {
// Clean up any side effects if needed
jest.clearAllMocks();
});

describe('handleFieldSubmission', () => {
it('should handle encounter location submission', async () => {
// Setup mock
const locations = [{ uuid: '5c95f6f5-788e-4e73-9079-5626911231fa', display: 'Test Location' }];
mockedGetAllLocations.mockResolvedValue(locations);

// Replay
await EncounterLocationSubmissionHandler.handleFieldSubmission(
field,
'5c95f6f5-788e-4e73-9079-5626911231fa',
encounterContext,
);

// Verify
expect(encounterContext.setEncounterLocation).toHaveBeenCalledWith(locations[0]);
});
});

describe('getInitialValue', () => {
it('should return the location UUID from the encounter if present', () => {
const initialValue = EncounterLocationSubmissionHandler.getInitialValue(
encounterWithLocation,
field,
[],
contextWithoutLocation,
);
expect(initialValue).toEqual('81e6e516-c1f0-11eb-8529-0242ac130003');
});

it('should return the location UUID from the context if encounter location is not present', () => {
const initialValue = EncounterLocationSubmissionHandler.getInitialValue(
encounterContext.encounter,
field,
[],
encounterContext,
);
expect(initialValue).toEqual('81e6e516-c1f0-11eb-8529-0242ac130003');
});

it('should return undefined if neither the encounter nor the context has a location', () => {
const initialValue = EncounterLocationSubmissionHandler.getInitialValue(
encounterWithoutLocation,
field,
[],
contextWithoutLocation,
);
expect(initialValue).toBeUndefined();
});
});

describe('getDisplayValue', () => {
it('should return display value when value is defined', () => {
const value = { display: 'Test Location', uuid: '5c95f6f5-788e-4e73-9079-5626911231fa' };
const displayValue = EncounterLocationSubmissionHandler.getDisplayValue(field, value);

expect(displayValue).toEqual('Test Location');
});

it('should return undefined when value is null', () => {
const value = null;
const displayValue = EncounterLocationSubmissionHandler.getDisplayValue(field, value);

expect(displayValue).toBeUndefined();
});

it('should return undefined when value is undefined', () => {
const value = undefined;
const displayValue = EncounterLocationSubmissionHandler.getDisplayValue(field, value);

expect(displayValue).toBeUndefined();
});
});

describe('getPreviousValue', () => {
it('should return display and value when encounter has location', () => {
const encounter = {
location: { name: 'Previous Location', uuid: '95e6e516-c1f0-11eb-8529-0242ac130006' },
};
const allFormFields = [];

const previousValue = EncounterLocationSubmissionHandler.getPreviousValue(field, encounter, allFormFields);

expect(previousValue).toEqual({ display: 'Previous Location', value: '95e6e516-c1f0-11eb-8529-0242ac130006' });
});

it('should return undefined when encounter has no location', () => {
const encounter = {};
const allFormFields = [];

const previousValue = EncounterLocationSubmissionHandler.getPreviousValue(field, encounter, allFormFields);

expect(previousValue).toEqual({ display: undefined, value: undefined });
});
});
});
6 changes: 3 additions & 3 deletions src/submission-handlers/encounterLocationHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const EncounterLocationSubmissionHandler: SubmissionHandler = {
},

getInitialValue: (encounter: any, field: FormField, allFormFields: Array<FormField>, context: EncounterContext) => {
if (encounter) {
if (encounter && encounter.location) {
return encounter.location.uuid;
} else {
return context?.location?.uuid;
Expand All @@ -23,8 +23,8 @@ export const EncounterLocationSubmissionHandler: SubmissionHandler = {

getPreviousValue: (field: FormField, encounter: any, allFormFields: Array<FormField>) => {
return {
display: encounter.location.name,
value: encounter.location.uuid,
display: encounter?.location?.name,
value: encounter?.location?.uuid,
};
},
};

0 comments on commit 34d7cfb

Please sign in to comment.