Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
feat(labs): adds view lab tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jackcmeyer committed Apr 3, 2020
1 parent 3518b77 commit 1867543
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 43 deletions.
134 changes: 104 additions & 30 deletions src/__tests__/labs/ViewLab.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ import Lab from 'model/Lab'
import Patient from 'model/Patient'
import * as ButtonBarProvider from 'page-header/ButtonBarProvider'
import createMockStore from 'redux-mock-store'
import { Badge, Button } from '@hospitalrun/components'
import { Badge, Button, Alert } from '@hospitalrun/components'
import TextFieldWithLabelFormGroup from 'components/input/TextFieldWithLabelFormGroup'
import ButtonToolBar from 'page-header/ButtonToolBar'
import * as titleUtil from '../../page-header/useTitle'
import ViewLab from '../../labs/ViewLab'

Expand All @@ -36,8 +35,10 @@ describe('View Labs', () => {
let setButtonToolBarSpy: any
let titleSpy: any
let labRepositorySaveSpy: any
const expectedDate = new Date()
const setup = async (lab: Lab, permissions: Permissions[]) => {
jest.resetAllMocks()
Date.now = jest.fn(() => expectedDate.valueOf())
setButtonToolBarSpy = jest.fn()
titleSpy = jest.spyOn(titleUtil, 'default')
jest.spyOn(ButtonBarProvider, 'useButtonToolbarSetter').mockReturnValue(setButtonToolBarSpy)
Expand Down Expand Up @@ -196,14 +197,36 @@ describe('View Labs', () => {
expect(badge.text().trim()).toEqual(expectedLab.status)
})

it('should display a complete lab and cancel lab button if the lab is in a requested state', async () => {
it('should display a update lab, complete lab, and cancel lab button if the lab is in a requested state', async () => {
const expectedLab = { ...mockLab, notes: 'expected notes' } as Lab

setup(expectedLab, [Permissions.ViewLab, Permissions.CompleteLab, Permissions.CancelLab])
const wrapper = await setup(expectedLab, [
Permissions.ViewLab,
Permissions.CompleteLab,
Permissions.CancelLab,
])

const buttons = wrapper.find(Button)
expect(
buttons
.at(0)
.text()
.trim(),
).toEqual('actions.update')

const actualButtons: React.ReactNode[] = setButtonToolBarSpy.mock.calls[0][0]
expect((actualButtons[0] as any).props.children).toEqual('labs.requests.complete')
expect((actualButtons[1] as any).props.children).toEqual('labs.requests.cancel')
expect(
buttons
.at(1)
.text()
.trim(),
).toEqual('labs.requests.complete')

expect(
buttons
.at(2)
.text()
.trim(),
).toEqual('labs.requests.cancel')
})
})

Expand Down Expand Up @@ -249,18 +272,17 @@ describe('View Labs', () => {
).toEqual('2020-03-29 11:45 PM')
})

it('should not display complete and cancel button if the lab is canceled', async () => {
it('should not display update, complete, and cancel button if the lab is canceled', async () => {
const expectedLab = { ...mockLab, status: 'canceled' } as Lab

await setup(expectedLab, [
const wrapper = await setup(expectedLab, [
Permissions.ViewLab,
Permissions.CompleteLab,
Permissions.CancelLab,
])

const { calls } = setButtonToolBarSpy.mock
const actualButtons: React.ReactNode[] = calls[calls.length - 1][0]
expect(actualButtons as any).toEqual([])
const buttons = wrapper.find(Button)
expect(buttons).toHaveLength(0)
})

it('should not display an update button if the lab is canceled', async () => {
Expand Down Expand Up @@ -314,26 +336,17 @@ describe('View Labs', () => {
).toEqual('2020-03-29 11:44 PM')
})

it('should not display complete and cancel button if the lab is completed', async () => {
it('should not display update, complete, and cancel buttons if the lab is completed', async () => {
const expectedLab = { ...mockLab, status: 'completed' } as Lab

await setup(expectedLab, [
const wrapper = await setup(expectedLab, [
Permissions.ViewLab,
Permissions.CompleteLab,
Permissions.CancelLab,
])

const { calls } = setButtonToolBarSpy.mock
const actualButtons: React.ReactNode[] = calls[calls.length - 1][0]
expect(actualButtons as any).toEqual([])
})

it('should not display an update button if the lab is completed', async () => {
const expectedLab = { ...mockLab, status: 'canceled' } as Lab
const wrapper = await setup(expectedLab, [Permissions.ViewLab])

const updateButton = wrapper.find(Button)
expect(updateButton).toHaveLength(0)
const buttons = wrapper.find(Button)
expect(buttons).toHaveLength(0)
})
})
})
Expand Down Expand Up @@ -385,25 +398,86 @@ describe('View Labs', () => {
const onChange = resultTextField.prop('onChange')
await onChange({ currentTarget: { value: expectedResult } })
})
wrapper.update()

const completeButton = setButtonToolBarSpy.mock.calls[0][0][0]
const completeButton = wrapper.find(Button).at(1)
await act(async () => {
const { onClick } = completeButton.props
const onClick = completeButton.prop('onClick')
await onClick()
})
wrapper.update()

expect(labRepositorySaveSpy).toHaveBeenCalledTimes(1)
expect(labRepositorySaveSpy).toHaveBeenCalledWith(
expect.objectContaining({ ...mockLab, result: expectedResult }),
expect.objectContaining({
...mockLab,
result: expectedResult,
status: 'completed',
completedOn: expectedDate.toISOString(),
}),
)
expect(history.location.pathname).toEqual('/labs')
})

it('should validate that the result has been filled in', () => {})
it('should validate that the result has been filled in', async () => {
const wrapper = await setup(mockLab, [
Permissions.ViewLab,
Permissions.CompleteLab,
Permissions.CancelLab,
])

const completeButton = wrapper.find(Button).at(1)
await act(async () => {
const onClick = completeButton.prop('onClick')
await onClick()
})
wrapper.update()

const alert = wrapper.find(Alert)
const resultField = wrapper.find(TextFieldWithLabelFormGroup).at(0)
expect(alert).toHaveLength(1)
expect(alert.prop('title')).toEqual('states.error')
expect(alert.prop('message')).toEqual('labs.requests.error.unableToComplete')
expect(resultField.prop('isInvalid')).toBeTruthy()
expect(resultField.prop('feedback')).toEqual('labs.requests.error.resultRequiredToComplete')

expect(labRepositorySaveSpy).not.toHaveBeenCalled()
})
})

describe('on cancel', () => {
it('should mark the status as completed and fill in the cancelled on date with the current time', () => {})
it('should mark the status as canceled and fill in the cancelled on date with the current time', async () => {
const wrapper = await setup(mockLab, [
Permissions.ViewLab,
Permissions.CompleteLab,
Permissions.CancelLab,
])
const expectedResult = 'expected result'

const resultTextField = wrapper.find(TextFieldWithLabelFormGroup).at(0)
await act(async () => {
const onChange = resultTextField.prop('onChange')
await onChange({ currentTarget: { value: expectedResult } })
})
wrapper.update()

const cancelButton = wrapper.find(Button).at(2)
await act(async () => {
const onClick = cancelButton.prop('onClick')
await onClick()
})
wrapper.update()

expect(labRepositorySaveSpy).toHaveBeenCalledTimes(1)
expect(labRepositorySaveSpy).toHaveBeenCalledWith(
expect.objectContaining({
...mockLab,
result: expectedResult,
status: 'canceled',
canceledOn: expectedDate.toISOString(),
}),
)
expect(history.location.pathname).toEqual('/labs')
})
})
})
23 changes: 10 additions & 13 deletions src/labs/ViewLab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import useTitle from 'page-header/useTitle'
import { useTranslation } from 'react-i18next'
import { Row, Column, Badge, Button, Alert } from '@hospitalrun/components'
import TextFieldWithLabelFormGroup from 'components/input/TextFieldWithLabelFormGroup'
import { useButtonToolbarSetter } from 'page-header/ButtonBarProvider'
import useAddBreadcrumbs from 'breadcrumbs/useAddBreadcrumbs'
import { useSelector } from 'react-redux'
import Permissions from 'model/Permissions'
Expand All @@ -22,8 +21,6 @@ const ViewLab = () => {
const { id } = useParams()
const { t } = useTranslation()
const history = useHistory()
const setButtons = useButtonToolbarSetter()

const { permissions } = useSelector((state: RootState) => state.user)
const [patient, setPatient] = useState<Patient | undefined>()
const [lab, setLab] = useState<Lab | undefined>()
Expand Down Expand Up @@ -93,7 +90,7 @@ const ViewLab = () => {

await LabRepository.saveOrUpdate({
...newLab,
completedOn: new Date().toISOString(),
completedOn: new Date(Date.now().valueOf()).toISOString(),
status: 'completed',
})
history.push('/labs')
Expand All @@ -103,7 +100,7 @@ const ViewLab = () => {
const newLab = lab as Lab
await LabRepository.saveOrUpdate({
...newLab,
canceledOn: new Date().toISOString(),
canceledOn: new Date(Date.now().valueOf()).toISOString(),
status: 'canceled',
})
history.push('/labs')
Expand All @@ -115,9 +112,15 @@ const ViewLab = () => {
return buttons
}

buttons.push(
<Button className="mr-2" color="success" onClick={onUpdate}>
{t('actions.update')}
</Button>,
)

if (permissions.includes(Permissions.CompleteLab)) {
buttons.push(
<Button onClick={onComplete} color="primary">
<Button className="mr-2" onClick={onComplete} color="primary">
{t('labs.requests.complete')}
</Button>,
)
Expand All @@ -134,8 +137,6 @@ const ViewLab = () => {
return buttons
}

setButtons(getButtons())

if (lab && patient) {
const getBadgeColor = () => {
if (lab.status === 'completed') {
Expand Down Expand Up @@ -225,11 +226,7 @@ const ViewLab = () => {
/>
{isEditable && (
<div className="row float-right">
<div className="btn-group btn-group-lg mt-3">
<Button className="mr-2" color="success" onClick={onUpdate}>
{t('actions.update')}
</Button>
</div>
<div className="btn-group btn-group-lg mt-3">{getButtons()}</div>
</div>
)}
</form>
Expand Down

0 comments on commit 1867543

Please sign in to comment.