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

Commit

Permalink
Merge pull request #225 from codyarose/codyarose_NewLabRequest
Browse files Browse the repository at this point in the history
refactor(NewLabRequest): improve queries - remove function call tests
  • Loading branch information
nobrayner authored Jan 7, 2021
2 parents 4903ba5 + 5d6952c commit ac35b21
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 42 deletions.
59 changes: 18 additions & 41 deletions src/__tests__/labs/requests/NewLabRequest.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Toaster } from '@hospitalrun/components'
import { render, screen, within, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import format from 'date-fns/format'
Expand Down Expand Up @@ -54,7 +55,9 @@ const setup = (
visits: expectedVisits,
} as Patient

jest.resetAllMocks()
jest.spyOn(PatientRepository, 'search').mockResolvedValue([expectedPatient])
jest.spyOn(PatientRepository, 'find').mockResolvedValue(expectedPatient)
jest.spyOn(LabRepository, 'save').mockResolvedValue(expectedLab)

const history = createMemoryHistory({ initialEntries: ['/labs/new'] })
Expand All @@ -71,6 +74,7 @@ const setup = (
<NewLabRequest />
</titleUtil.TitleProvider>
</Router>
<Toaster draggable hideProgressBar />
</Provider>,
),
}
Expand Down Expand Up @@ -137,8 +141,7 @@ describe('New Lab Request', () => {
const { expectedVisits } = setup()

const patientTypeahead = screen.getByPlaceholderText(/labs.lab.patient/i)
const visitsInput = screen.getByPlaceholderText('-- Choose --')

const visitsInput = within(screen.getByTestId('visit-field')).getByRole('combobox')
userEvent.type(patientTypeahead, 'Jim Bob')
userEvent.click(await screen.findByText(/Jim Bob/i))
expect(patientTypeahead).toHaveDisplayValue(/Jim Bob/i)
Expand Down Expand Up @@ -183,25 +186,21 @@ describe('New Lab Request', () => {
type: 'some type error',
} as LabError

beforeAll(() => {
jest.spyOn(validationUtil, 'validateLabRequest').mockReturnValue(error)
expectOneConsoleError(error)
})

it('should display errors', async () => {
setup()
jest.spyOn(validationUtil, 'validateLabRequest').mockReturnValue(error)
expectOneConsoleError(error)
const saveButton = screen.getByRole('button', { name: /labs\.requests\.new/i })

userEvent.click(saveButton)

const alert = screen.findByRole('alert')
const { getByText: getByTextInAlert } = within(await alert)
const alert = await screen.findByRole('alert')
const patientInput = screen.getByPlaceholderText(/labs\.lab\.patient/i)
const typeInput = screen.getByPlaceholderText(/labs\.lab\.type/i)

expect(getByTextInAlert(error.message)).toBeInTheDocument()
expect(getByTextInAlert(/states\.error/i)).toBeInTheDocument()
expect(await alert).toHaveClass('alert-danger')
expect(within(alert).getByText(error.message)).toBeInTheDocument()
expect(within(alert).getByText(/states\.error/i)).toBeInTheDocument()
expect(alert).toHaveClass('alert-danger')
expect(patientInput).toHaveClass('is-invalid')
expect(typeInput).toHaveClass('is-invalid')
expect(typeInput.nextSibling).toHaveTextContent(error.type as string)
Expand All @@ -219,43 +218,21 @@ describe('New Lab Request', () => {
})

describe('on save', () => {
const store = mockStore({
lab: { status: 'loading', error: {} },
user: { user: { id: 'fake id' } },
} as any)

it('should save the lab request and navigate to "/labs/:id"', async () => {
const { expectedLab, history } = setup(store)
const { expectedLab, history } = setup()

userEvent.type(screen.getByPlaceholderText(/labs.lab.patient/i), 'Jim Bob')

await waitFor(
() => {
expect(screen.getByText(/jim bob/i)).toBeVisible()
},
{ timeout: 3000 },
)
expect(await screen.findByText(/jim bob/i)).toBeVisible()
userEvent.click(screen.getByText(/jim bob/i))
userEvent.type(screen.getByPlaceholderText(/labs\.lab\.type/i), expectedLab.type)
userEvent.type(screen.getByLabelText(/labs\.lab\.notes/i), (expectedLab.notes as string[])[0])

userEvent.click(screen.getByRole('button', { name: /labs\.requests\.new/i }))

await waitFor(() => {
expect(LabRepository.save).toHaveBeenCalledTimes(1)
})
expect(LabRepository.save).toHaveBeenCalledWith(
expect.objectContaining({
patient: expectedLab.patient,
type: expectedLab.type,
notes: expectedLab.notes,
status: 'requested',
}),
)

await waitFor(() => {
expect(history.location.pathname).toEqual(`/labs/${expectedLab.id}`)
})
expect(await screen.findByRole('alert')).toBeInTheDocument()
expect(
within(screen.getByRole('alert')).getByText(/labs\.successfullyCreated/i),
).toBeInTheDocument()
expect(history.location.pathname).toEqual(`/labs/${expectedLab.id}`)
}, 15000)
})
})
2 changes: 1 addition & 1 deletion src/labs/requests/NewLabRequest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ const NewLabRequest = () => {
</div>
</Column>
<Column>
<div className="form-group">
<div className="form-group" data-testid="visit-field">
<SelectWithLabelFormGroup
name="visit"
label={t('patient.visit')}
Expand Down

0 comments on commit ac35b21

Please sign in to comment.