diff --git a/src/__tests__/HospitalRun.test.tsx b/src/__tests__/HospitalRun.test.tsx index f0fa359177..6d3f9b9f34 100644 --- a/src/__tests__/HospitalRun.test.tsx +++ b/src/__tests__/HospitalRun.test.tsx @@ -411,7 +411,7 @@ describe('HospitalRun', () => { }) describe('/labs', () => { - it('should render the Labs component when /labs is accessed', () => { + it('should render the Labs component when /labs is accessed', async () => { jest.spyOn(LabRepository, 'findAll').mockResolvedValue([]) const store = mockStore({ title: 'test', @@ -420,18 +420,22 @@ describe('HospitalRun', () => { components: { sidebarCollapsed: false }, }) - const wrapper = mount( - - - - - , - ) + let wrapper: any + await act(async () => { + wrapper = await mount( + + + + + , + ) + }) + wrapper.update() expect(wrapper.find(ViewLabs)).toHaveLength(1) }) - it('should render the dasboard if the user does not have permissions to view labs', () => { + it('should render the dashboard if the user does not have permissions to view labs', () => { jest.spyOn(LabRepository, 'findAll').mockResolvedValue([]) const store = mockStore({ title: 'test', diff --git a/src/__tests__/page-header/ButtonBarProvider.test.tsx b/src/__tests__/page-header/ButtonBarProvider.test.tsx index f718bcf075..af8527735d 100644 --- a/src/__tests__/page-header/ButtonBarProvider.test.tsx +++ b/src/__tests__/page-header/ButtonBarProvider.test.tsx @@ -1,5 +1,5 @@ import '../../__mocks__/matchMediaMock' -import React from 'react' +import React, { useEffect } from 'react' import { renderHook } from '@testing-library/react-hooks' import { ButtonBarProvider, @@ -16,7 +16,10 @@ describe('Button Bar Provider', () => { const { result } = renderHook( () => { const update = useButtonToolbarSetter() - update(expectedButtons) + useEffect(() => { + update(expectedButtons) + }, [update]) + return useButtons() }, { wrapper }, diff --git a/src/labs/ViewLab.tsx b/src/labs/ViewLab.tsx index 96ea16e96d..57c72b860b 100644 --- a/src/labs/ViewLab.tsx +++ b/src/labs/ViewLab.tsx @@ -98,14 +98,14 @@ const ViewLab = () => { } buttons.push( - , ) if (permissions.includes(Permissions.CompleteLab)) { buttons.push( - , ) @@ -113,7 +113,7 @@ const ViewLab = () => { if (permissions.includes(Permissions.CancelLab)) { buttons.push( - , ) @@ -199,7 +199,7 @@ const ViewLab = () => { value={labToView.result} isEditable={isEditable} isInvalid={!!error.result} - feedback={t(error.result || '')} + feedback={t(error.result as string)} onChange={onResultChange} /> { const { permissions } = useSelector((state: RootState) => state.user) const [labs, setLabs] = useState([]) - const getButtons = () => { + const getButtons = useCallback(() => { const buttons: React.ReactNode[] = [] if (permissions.includes(Permissions.RequestLab)) { buttons.push( - , ) } return buttons - } - - setButtons(getButtons()) + }, [permissions, history, t]) useEffect(() => { const fetch = async () => { @@ -51,8 +55,13 @@ const ViewLabs = () => { setLabs(fetchedLabs) } + setButtons(getButtons()) fetch() - }, []) + + return () => { + setButtons([]) + } + }, [getButtons, setButtons]) const onTableRowClick = (lab: Lab) => { history.push(`/labs/${lab.id}`) diff --git a/src/labs/requests/NewLabRequest.tsx b/src/labs/requests/NewLabRequest.tsx index 5f9f03fe5a..fb4035686e 100644 --- a/src/labs/requests/NewLabRequest.tsx +++ b/src/labs/requests/NewLabRequest.tsx @@ -95,7 +95,7 @@ const NewLabRequest = () => { isRequired isEditable isInvalid={!!error.type} - feedback={t(error.type || '')} + feedback={t(error.type as string)} value={newLabRequest.type} onChange={onLabTypeChange} /> diff --git a/src/patients/list/Patients.tsx b/src/patients/list/Patients.tsx index a56f374321..99964c16b6 100644 --- a/src/patients/list/Patients.tsx +++ b/src/patients/list/Patients.tsx @@ -22,17 +22,6 @@ const Patients = () => { const { patients, isLoading } = useSelector((state: RootState) => state.patients) const setButtonToolBar = useButtonToolbarSetter() - setButtonToolBar([ - , - ]) const [searchText, setSearchText] = useState('') @@ -45,28 +34,25 @@ const Patients = () => { useEffect(() => { dispatch(fetchPatients()) + setButtonToolBar([ + , + ]) + return () => { setButtonToolBar([]) } - }, [dispatch, setButtonToolBar]) + }, [dispatch, setButtonToolBar, t, history]) const loadingIndicator = - - const listBody = ( - - {patients.map((p) => ( - history.push(`/patients/${p.id}`)}> - {p.code} - {p.givenName} - {p.familyName} - {p.sex} - {p.dateOfBirth ? format(new Date(p.dateOfBirth), 'yyyy-MM-dd') : ''} - - ))} - - ) - - const list = ( + const table = ( @@ -77,7 +63,17 @@ const Patients = () => { - {isLoading ? loadingIndicator : listBody} + + {patients.map((p) => ( + history.push(`/patients/${p.id}`)}> + + + + + + + ))} +
{t('patient.dateOfBirth')}
{p.code}{p.givenName}{p.familyName}{p.sex}{p.dateOfBirth ? format(new Date(p.dateOfBirth), 'yyyy-MM-dd') : ''}
) @@ -99,7 +95,7 @@ const Patients = () => { - {list} + {isLoading ? loadingIndicator : table} ) } diff --git a/src/patients/view/ViewPatient.tsx b/src/patients/view/ViewPatient.tsx index 27703a5e28..7be7a08c7d 100644 --- a/src/patients/view/ViewPatient.tsx +++ b/src/patients/view/ViewPatient.tsx @@ -40,25 +40,6 @@ const ViewPatient = () => { const setButtonToolBar = useButtonToolbarSetter() - const buttons = [] - if (permissions.includes(Permissions.WritePatients)) { - buttons.push( - , - ) - } - - setButtonToolBar(buttons) - const breadcrumbs = [ { i18nKey: 'patients.label', location: '/patients' }, { text: getPatientFullName(patient), location: `/patients/${patient.id}` }, @@ -71,10 +52,29 @@ const ViewPatient = () => { dispatch(fetchPatient(id)) } + const buttons = [] + if (permissions.includes(Permissions.WritePatients)) { + buttons.push( + , + ) + } + + setButtonToolBar(buttons) + return () => { setButtonToolBar([]) } - }, [dispatch, id, setButtonToolBar]) + }, [dispatch, id, setButtonToolBar, history, patient.id, permissions, t]) if (isLoading || !patient) { return diff --git a/src/scheduling/appointments/Appointments.tsx b/src/scheduling/appointments/Appointments.tsx index f152f49507..74cbcb4143 100644 --- a/src/scheduling/appointments/Appointments.tsx +++ b/src/scheduling/appointments/Appointments.tsx @@ -28,26 +28,26 @@ const Appointments = () => { const { appointments } = useSelector((state: RootState) => state.appointments) const [events, setEvents] = useState([]) const setButtonToolBar = useButtonToolbarSetter() - setButtonToolBar([ - , - ]) useAddBreadcrumbs(breadcrumbs, true) useEffect(() => { dispatch(fetchAppointments()) + setButtonToolBar([ + , + ]) return () => { setButtonToolBar([]) } - }, [dispatch, setButtonToolBar]) + }, [dispatch, setButtonToolBar, history, t]) useEffect(() => { const getAppointments = async () => { diff --git a/src/scheduling/appointments/view/ViewAppointment.tsx b/src/scheduling/appointments/view/ViewAppointment.tsx index ed09b28cd7..f54bd98093 100644 --- a/src/scheduling/appointments/view/ViewAppointment.tsx +++ b/src/scheduling/appointments/view/ViewAppointment.tsx @@ -21,9 +21,14 @@ const ViewAppointment = () => { const { appointment, patient, isLoading } = useSelector((state: RootState) => state.appointment) const { permissions } = useSelector((state: RootState) => state.user) const [showDeleteConfirmation, setShowDeleteConfirmation] = useState(false) - const setButtonToolBar = useButtonToolbarSetter() + const breadcrumbs = [ + { i18nKey: 'scheduling.appointments.label', location: '/appointments' }, + { text: getAppointmentLabel(appointment), location: `/patients/${appointment.id}` }, + ] + useAddBreadcrumbs(breadcrumbs, true) + const onAppointmentDeleteButtonClick = (event: React.MouseEvent) => { event.preventDefault() setShowDeleteConfirmation(true) @@ -39,43 +44,39 @@ const ViewAppointment = () => { setShowDeleteConfirmation(false) } - const buttons = [] - if (permissions.includes(Permissions.WriteAppointments)) { - buttons.push( - , - ) - } - - if (permissions.includes(Permissions.DeleteAppointment)) { - buttons.push( - , - ) - } + useEffect(() => { + const buttons = [] + if (permissions.includes(Permissions.WriteAppointments)) { + buttons.push( + , + ) + } - setButtonToolBar(buttons) + if (permissions.includes(Permissions.DeleteAppointment)) { + buttons.push( + , + ) + } - const breadcrumbs = [ - { i18nKey: 'scheduling.appointments.label', location: '/appointments' }, - { text: getAppointmentLabel(appointment), location: `/patients/${appointment.id}` }, - ] - useAddBreadcrumbs(breadcrumbs, true) + setButtonToolBar(buttons) + }, [appointment.id, history, permissions, setButtonToolBar, t]) useEffect(() => { if (id) {