Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
projkov committed Oct 4, 2023
1 parent 4f13363 commit a8efe37
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { renderHook } from '@testing-library/react';
import { HealthcareService } from 'fhir/r4b';
import { act } from 'react-dom/test-utils';

import { renderHumanName } from 'shared/src/utils/fhir';

import { initialSetup, optionExistInOptionList } from './utils';
import { getPractitionerRoleNamesMapping, initialSetup, optionExistInOptionList } from './utils';
import { useHealthcareServicePractitionerSelect } from '../hooks';
import { SelectOption } from '../types';

Expand All @@ -14,31 +13,20 @@ describe('useHealthcareServicePractitionerSelect', () => {

const options = await result.current.healthcareServiceOptions('');

const expectOptionToExist = (healthcareService?: HealthcareService) => {
const option = { value: healthcareService?.id, label: healthcareService?.name } as SelectOption;
expect(optionExistInOptionList(options as SelectOption[], option)).toEqual(true);
};

expect(options.length).toBeGreaterThan(0);
expect(
optionExistInOptionList(
options as SelectOption[],
{ value: healthcareServices?.[0]?.id, label: healthcareServices?.[0]?.name } as SelectOption,
),
).toEqual(true);
expect(
optionExistInOptionList(
options as SelectOption[],
{ value: healthcareServices?.[1]?.id, label: healthcareServices?.[1]?.name } as SelectOption,
),
).toEqual(true);
expectOptionToExist(healthcareServices?.[0]);
expectOptionToExist(healthcareServices?.[1]);
});

it('should fetch practitionerRoleOptions', async () => {
const { practitionerRoles, practitioners } = await initialSetup();
const { result } = renderHook(() => useHealthcareServicePractitionerSelect());
const practitionerRoleNamesMapping = practitionerRoles.map((pr) => {
const currentPractitioner = practitioners.find((p) => p.id === pr.practitioner?.reference?.split('/')[1]);
return {
id: pr.id,
name: renderHumanName(currentPractitioner?.name?.[0]),
};
});
const practitionerRoleNamesMapping = getPractitionerRoleNamesMapping(practitionerRoles, practitioners);

const options: SelectOption[] = await result.current.practitionerRoleOptions('');

Expand All @@ -60,13 +48,7 @@ describe('useHealthcareServicePractitionerSelect', () => {
it('should fetch practitionerRoles based on healthcare service selected', async () => {
const { practitionerRoles, practitioners, healthcareServices } = await initialSetup();
const { result } = renderHook(() => useHealthcareServicePractitionerSelect());
const practitionerRoleNamesMapping = practitionerRoles.map((pr) => {
const currentPractitioner = practitioners.find((p) => p.id === pr.practitioner?.reference?.split('/')[1]);
return {
id: pr.id,
name: renderHumanName(currentPractitioner?.name?.[0]),
};
});
const practitionerRoleNamesMapping = getPractitionerRoleNamesMapping(practitionerRoles, practitioners);

act(() => {
result.current.onChange(
Expand Down Expand Up @@ -101,13 +83,7 @@ describe('useHealthcareServicePractitionerSelect', () => {
it('should fetch healthcareServiceOptions based on selected practitionerRole', async () => {
const { healthcareServices, practitionerRoles, practitioners } = await initialSetup();
const { result } = renderHook(() => useHealthcareServicePractitionerSelect());
const practitionerRoleNamesMapping = practitionerRoles.map((pr) => {
const currentPractitioner = practitioners.find((p) => p.id === pr.practitioner?.reference?.split('/')[1]);
return {
id: pr.id,
name: renderHumanName(currentPractitioner?.name?.[0]),
};
});
const practitionerRoleNamesMapping = getPractitionerRoleNamesMapping(practitionerRoles, practitioners);

act(() => {
result.current.onChange(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { PractitionerRole } from 'fhir/r4b';
import { Practitioner, PractitionerRole } from 'fhir/r4b';

import { createFHIRResource, getReference } from 'fhir-react/lib/services/fhir';
import { ensure, withRootAccess } from 'fhir-react/lib/utils/tests';

import { User } from 'shared/src/contrib/aidbox';
import { renderHumanName } from 'shared/src/utils/fhir';

import { createHealthcareService, createPractitioner, ensureSave, login } from 'src/setupTests';

Expand Down Expand Up @@ -123,13 +124,23 @@ async function createPractitionerRole(practitionerRoleData: Partial<Practitioner
);
}

export const initialSetup = async () => {
export async function initialSetup() {
const data = await dataSetup();
await login({ ...data.user, password: 'password' });

return data;
};
}

export function optionExistInOptionList(optionList: SelectOption[], targetList: SelectOption): boolean {
return optionList.some((dict) => JSON.stringify(dict) === JSON.stringify(targetList));
}

export function getPractitionerRoleNamesMapping(practitionerRoles: PractitionerRole[], practitioners: Practitioner[]) {
return practitionerRoles.map((pr) => {
const currentPractitioner = practitioners.find((p) => p.id === pr.practitioner?.reference?.split('/')[1]);
return {
id: pr.id,
name: renderHumanName(currentPractitioner?.name?.[0]),
};
});
}

0 comments on commit a8efe37

Please sign in to comment.