From a8efe3772fc5d468141871969714e06ca2aa40b4 Mon Sep 17 00:00:00 2001 From: Pavel R Date: Wed, 4 Oct 2023 13:36:43 +0200 Subject: [PATCH] Update tests --- ...ealthcareServicePractitionerSelect.test.ts | 48 +++++-------------- .../__tests__/utils.ts | 17 +++++-- 2 files changed, 26 insertions(+), 39 deletions(-) diff --git a/src/containers/OrganizationScheduling/HealthcareServicePractitionerSelect/__tests__/HealthcareServicePractitionerSelect.test.ts b/src/containers/OrganizationScheduling/HealthcareServicePractitionerSelect/__tests__/HealthcareServicePractitionerSelect.test.ts index 3c575541..76486a56 100644 --- a/src/containers/OrganizationScheduling/HealthcareServicePractitionerSelect/__tests__/HealthcareServicePractitionerSelect.test.ts +++ b/src/containers/OrganizationScheduling/HealthcareServicePractitionerSelect/__tests__/HealthcareServicePractitionerSelect.test.ts @@ -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'; @@ -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(''); @@ -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( @@ -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( diff --git a/src/containers/OrganizationScheduling/HealthcareServicePractitionerSelect/__tests__/utils.ts b/src/containers/OrganizationScheduling/HealthcareServicePractitionerSelect/__tests__/utils.ts index 87c0b3bc..9990d969 100644 --- a/src/containers/OrganizationScheduling/HealthcareServicePractitionerSelect/__tests__/utils.ts +++ b/src/containers/OrganizationScheduling/HealthcareServicePractitionerSelect/__tests__/utils.ts @@ -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'; @@ -123,13 +124,23 @@ async function createPractitionerRole(practitionerRoleData: Partial { +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]), + }; + }); +}