From 85414d5f16c90821dc6142ce6e0f5518d2bcbcc1 Mon Sep 17 00:00:00 2001 From: James Zheng Date: Sun, 18 Apr 2021 14:19:20 -0700 Subject: [PATCH] fix(extractfullname.ts): scaffolds extractFullName() Previously, the extractUserName() function was returning a mocked "some user." We scaffolded out this function for when we complete the user model. fix #2539 fixed extractFullName import remove reportedBy make app test less spurious --- src/__tests__/App.test.tsx | 8 ++++---- src/__tests__/incidents/hooks/useReportIncident.test.tsx | 2 -- src/incidents/hooks/useReportIncident.tsx | 2 +- src/incidents/report/ReportIncident.tsx | 4 ++++ src/incidents/view/ViewIncidentDetails.tsx | 4 ++-- src/shared/util/extractFullName.ts | 2 ++ 6 files changed, 13 insertions(+), 9 deletions(-) create mode 100644 src/shared/util/extractFullName.ts diff --git a/src/__tests__/App.test.tsx b/src/__tests__/App.test.tsx index c98e594d80..f11a1eec7f 100644 --- a/src/__tests__/App.test.tsx +++ b/src/__tests__/App.test.tsx @@ -1,4 +1,4 @@ -import { render, screen } from '@testing-library/react' +import { render, screen, waitFor } from '@testing-library/react' import React from 'react' import { Provider } from 'react-redux' import createMockStore from 'redux-mock-store' @@ -32,9 +32,9 @@ it('renders without crashing', async () => { , ) - expect( - await screen.findByRole('heading', { name: /dashboard\.label/i }, { timeout: 8000 }), - ).toBeInTheDocument() + await waitFor(() => { + expect(screen.getByRole('heading', { name: /dashboard\.label/i })).toBeInTheDocument() + }) // eslint-disable-next-line no-console ;(console.log as jest.Mock).mockRestore() diff --git a/src/__tests__/incidents/hooks/useReportIncident.test.tsx b/src/__tests__/incidents/hooks/useReportIncident.test.tsx index 7753dceedf..49f52892a5 100644 --- a/src/__tests__/incidents/hooks/useReportIncident.test.tsx +++ b/src/__tests__/incidents/hooks/useReportIncident.test.tsx @@ -18,7 +18,6 @@ describe('useReportIncident', () => { const expectedCode = '123456' const expectedDate = new Date(Date.now()) const expectedStatus = 'reported' - const expectedReportedBy = 'some user' Date.now = jest.fn().mockReturnValue(expectedDate) const givenIncidentRequest = { @@ -34,7 +33,6 @@ describe('useReportIncident', () => { code: `I-${expectedCode}`, reportedOn: expectedDate.toISOString(), status: expectedStatus, - reportedBy: expectedReportedBy, } as Incident jest.spyOn(shortid, 'generate').mockReturnValue(expectedCode) jest.spyOn(IncidentRepository, 'save').mockResolvedValue(expectedIncident) diff --git a/src/incidents/hooks/useReportIncident.tsx b/src/incidents/hooks/useReportIncident.tsx index 1421e4ff88..87af3765fa 100644 --- a/src/incidents/hooks/useReportIncident.tsx +++ b/src/incidents/hooks/useReportIncident.tsx @@ -10,12 +10,12 @@ const getIncidentCode = (): string => `I-${shortid.generate()}` export function reportIncident(incident: Incident): Promise { const error = validateIncident(incident) + if (isEmpty(error)) { const updatedIncident: Incident = { ...incident, code: getIncidentCode(), status: 'reported', - reportedBy: 'some user', reportedOn: new Date(Date.now()).toISOString(), } return IncidentRepository.save(updatedIncident) diff --git a/src/incidents/report/ReportIncident.tsx b/src/incidents/report/ReportIncident.tsx index 62bee68cf8..65633d0fe0 100644 --- a/src/incidents/report/ReportIncident.tsx +++ b/src/incidents/report/ReportIncident.tsx @@ -1,5 +1,6 @@ import { Button, Row, Column, Typeahead, Label } from '@hospitalrun/components' import React, { useState, useEffect } from 'react' +import { useSelector } from 'react-redux' import { useHistory } from 'react-router-dom' import useAddBreadcrumbs from '../../page-header/breadcrumbs/useAddBreadcrumbs' @@ -11,6 +12,7 @@ import PatientRepository from '../../shared/db/PatientRepository' import useTranslator from '../../shared/hooks/useTranslator' import Incident from '../../shared/model/Incident' import Patient from '../../shared/model/Patient' +import { RootState } from '../../shared/store' import useReportIncident from '../hooks/useReportIncident' import { IncidentError } from '../util/validate-incident' @@ -19,6 +21,7 @@ const ReportIncident = () => { const history = useHistory() const { t } = useTranslator() const updateTitle = useUpdateTitle() + const { user } = useSelector((state: RootState) => state.user) useEffect(() => { updateTitle(t('incidents.reports.new')) }) @@ -30,6 +33,7 @@ const ReportIncident = () => { ] useAddBreadcrumbs(breadcrumbs) const [incident, setIncident] = useState({ + reportedBy: user?.id || 'some user', date: new Date().toISOString(), department: '', category: '', diff --git a/src/incidents/view/ViewIncidentDetails.tsx b/src/incidents/view/ViewIncidentDetails.tsx index ca3ac5a409..8f09608bc6 100644 --- a/src/incidents/view/ViewIncidentDetails.tsx +++ b/src/incidents/view/ViewIncidentDetails.tsx @@ -8,7 +8,7 @@ import TextFieldWithLabelFormGroup from '../../shared/components/input/TextField import TextInputWithLabelFormGroup from '../../shared/components/input/TextInputWithLabelFormGroup' import useTranslator from '../../shared/hooks/useTranslator' import Permissions from '../../shared/model/Permissions' -import { extractUsername } from '../../shared/util/extractUsername' +import { extractFullName } from '../../shared/util/extractFullName' import useIncident from '../hooks/useIncident' import useResolveIncident from '../hooks/useResolveIncident' @@ -83,7 +83,7 @@ function ViewIncidentDetails(props: Props) {

{t('incidents.reports.reportedBy')}

-
{extractUsername(data.reportedBy)}
+
{extractFullName(data.reportedBy)}
diff --git a/src/shared/util/extractFullName.ts b/src/shared/util/extractFullName.ts new file mode 100644 index 0000000000..f85fa6df13 --- /dev/null +++ b/src/shared/util/extractFullName.ts @@ -0,0 +1,2 @@ +// TODO: extract fullname from userID when we have information on other users +export const extractFullName = (userID: string) => userID