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 test for lab related routes
Browse files Browse the repository at this point in the history
  • Loading branch information
jackcmeyer committed Apr 3, 2020
1 parent 691572b commit f354c94
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 219 deletions.
45 changes: 45 additions & 0 deletions src/__tests__/HospitalRun.test.tsx
Expand Up @@ -14,6 +14,8 @@ import Appointments from 'scheduling/appointments/Appointments'
import NewAppointment from 'scheduling/appointments/new/NewAppointment'
import EditAppointment from 'scheduling/appointments/edit/EditAppointment'
import { addBreadcrumbs } from 'breadcrumbs/breadcrumbs-slice'
import ViewLabs from 'labs/ViewLabs'
import LabRepository from 'clients/db/LabRepository'
import NewPatient from '../patients/new/NewPatient'
import EditPatient from '../patients/edit/EditPatient'
import ViewPatient from '../patients/view/ViewPatient'
Expand Down Expand Up @@ -407,6 +409,49 @@ describe('HospitalRun', () => {
expect(wrapper.find(Dashboard)).toHaveLength(1)
})
})

describe('/labs', () => {
it('should render the Labs component when /labs is accessed', () => {
jest.spyOn(LabRepository, 'findAll').mockResolvedValue([])
const store = mockStore({
title: 'test',
user: { permissions: [Permissions.ViewLabs] },
breadcrumbs: { breadcrumbs: [] },
components: { sidebarCollapsed: false },
})

const wrapper = mount(
<Provider store={store}>
<MemoryRouter initialEntries={['/labs']}>
<HospitalRun />
</MemoryRouter>
</Provider>,
)

expect(wrapper.find(ViewLabs)).toHaveLength(1)
})

it('should render the dasboard if the user does not have permissions to view labs', () => {
jest.spyOn(LabRepository, 'findAll').mockResolvedValue([])
const store = mockStore({
title: 'test',
user: { permissions: [] },
breadcrumbs: { breadcrumbs: [] },
components: { sidebarCollapsed: false },
})

const wrapper = mount(
<Provider store={store}>
<MemoryRouter initialEntries={['/labs']}>
<HospitalRun />
</MemoryRouter>
</Provider>,
)

expect(wrapper.find(ViewLabs)).toHaveLength(0)
expect(wrapper.find(Dashboard)).toHaveLength(1)
})
})
})

describe('layout', () => {
Expand Down
10 changes: 2 additions & 8 deletions src/__tests__/components/Navbar.test.tsx
Expand Up @@ -96,20 +96,14 @@ describe('Navbar', () => {

it('should navigate to to /labs when the labs list option is selected', () => {
act(() => {
labsLinkList
.first()
.props()
.children[0].props.onClick()
labsLinkList.first().props().children[0].props.onClick()
})
expect(history.location.pathname).toEqual('/labs')
})

it('should navigate to /labs/new when the new labs list option is selected', () => {
act(() => {
labsLinkList
.first()
.props()
.children[1].props.onClick()
labsLinkList.first().props().children[1].props.onClick()
})
expect(history.location.pathname).toEqual('/labs/new')
})
Expand Down
70 changes: 10 additions & 60 deletions src/__tests__/components/Sidebar.test.tsx
Expand Up @@ -35,12 +35,7 @@ describe('Sidebar', () => {

const listItems = wrapper.find(ListItem)

expect(
listItems
.at(1)
.text()
.trim(),
).toEqual('dashboard.label')
expect(listItems.at(1).text().trim()).toEqual('dashboard.label')
})

it('should be active when the current path is /', () => {
Expand Down Expand Up @@ -71,38 +66,23 @@ describe('Sidebar', () => {

const listItems = wrapper.find(ListItem)

expect(
listItems
.at(2)
.text()
.trim(),
).toEqual('patients.label')
expect(listItems.at(2).text().trim()).toEqual('patients.label')
})

it('should render the new_patient link', () => {
const wrapper = setup('/patients')

const listItems = wrapper.find(ListItem)

expect(
listItems
.at(3)
.text()
.trim(),
).toEqual('patients.newPatient')
expect(listItems.at(3).text().trim()).toEqual('patients.newPatient')
})

it('should render the patients_list link', () => {
const wrapper = setup('/patients')

const listItems = wrapper.find(ListItem)

expect(
listItems
.at(4)
.text()
.trim(),
).toEqual('patients.patientsList')
expect(listItems.at(4).text().trim()).toEqual('patients.patientsList')
})

it('main patients link should be active when the current path is /patients', () => {
Expand Down Expand Up @@ -175,38 +155,23 @@ describe('Sidebar', () => {

const listItems = wrapper.find(ListItem)

expect(
listItems
.at(3)
.text()
.trim(),
).toEqual('scheduling.label')
expect(listItems.at(3).text().trim()).toEqual('scheduling.label')
})

it('should render the new appointment link', () => {
const wrapper = setup('/appointments/new')

const listItems = wrapper.find(ListItem)

expect(
listItems
.at(4)
.text()
.trim(),
).toEqual('scheduling.appointments.new')
expect(listItems.at(4).text().trim()).toEqual('scheduling.appointments.new')
})

it('should render the appointments schedule link', () => {
const wrapper = setup('/appointments')

const listItems = wrapper.find(ListItem)

expect(
listItems
.at(5)
.text()
.trim(),
).toEqual('scheduling.appointments.schedule')
expect(listItems.at(5).text().trim()).toEqual('scheduling.appointments.schedule')
})

it('main scheduling link should be active when the current path is /appointments', () => {
Expand Down Expand Up @@ -279,38 +244,23 @@ describe('Sidebar', () => {

const listItems = wrapper.find(ListItem)

expect(
listItems
.at(4)
.text()
.trim(),
).toEqual('labs.label')
expect(listItems.at(4).text().trim()).toEqual('labs.label')
})

it('should render the new labs request link', () => {
const wrapper = setup('/labs')

const listItems = wrapper.find(ListItem)

expect(
listItems
.at(5)
.text()
.trim(),
).toEqual('labs.requests.new')
expect(listItems.at(5).text().trim()).toEqual('labs.requests.new')
})

it('should render the labs list link', () => {
const wrapper = setup('/labs')

const listItems = wrapper.find(ListItem)

expect(
listItems
.at(6)
.text()
.trim(),
).toEqual('labs.requests.label')
expect(listItems.at(6).text().trim()).toEqual('labs.requests.label')
})

it('main labs link should be active when the current path is /labs', () => {
Expand Down
114 changes: 113 additions & 1 deletion src/__tests__/labs/Labs.test.tsx
@@ -1,3 +1,115 @@
import '../../__mocks__/matchMediaMock'
import React from 'react'
import { mount } from 'enzyme'
import { MemoryRouter } from 'react-router'
import { Provider } from 'react-redux'
import thunk from 'redux-thunk'
import configureMockStore from 'redux-mock-store'

import { act } from 'react-dom/test-utils'
import Labs from 'labs/Labs'
import NewLabRequest from 'labs/requests/NewLabRequest'
import Permissions from 'model/Permissions'
import ViewLab from 'labs/ViewLab'
import LabRepository from 'clients/db/LabRepository'
import Lab from 'model/Lab'
import Patient from 'model/Patient'
import PatientRepository from 'clients/db/PatientRepository'

const mockStore = configureMockStore([thunk])

describe('Labs', () => {
it('should render the routes', () => {})
jest.spyOn(LabRepository, 'findAll').mockResolvedValue([])
jest
.spyOn(LabRepository, 'find')
.mockResolvedValue({ id: '1234', requestedOn: new Date().toISOString() } as Lab)
jest
.spyOn(PatientRepository, 'find')
.mockResolvedValue({ id: '12345', fullName: 'test test' } as Patient)

describe('routing', () => {
describe('/labs/new', () => {
it('should render the new lab request screen when /labs/new is accessed', () => {
const store = mockStore({
title: 'test',
user: { permissions: [Permissions.RequestLab] },
breadcrumbs: { breadcrumbs: [] },
components: { sidebarCollapsed: false },
})

const wrapper = mount(
<Provider store={store}>
<MemoryRouter initialEntries={['/labs/new']}>
<Labs />
</MemoryRouter>
</Provider>,
)

expect(wrapper.find(NewLabRequest)).toHaveLength(1)
})

it('should not navigate to /labs/new if the user does not have RequestLab permissions', () => {
const store = mockStore({
title: 'test',
user: { permissions: [] },
breadcrumbs: { breadcrumbs: [] },
components: { sidebarCollapsed: false },
})

const wrapper = mount(
<Provider store={store}>
<MemoryRouter initialEntries={['/labs/new']}>
<Labs />
</MemoryRouter>
</Provider>,
)

expect(wrapper.find(NewLabRequest)).toHaveLength(0)
})
})

describe('/labs/:id', () => {
it('should render the view lab screen when /labs/:id is accessed', async () => {
const store = mockStore({
title: 'test',
user: { permissions: [Permissions.ViewLab] },
breadcrumbs: { breadcrumbs: [] },
components: { sidebarCollapsed: false },
})

let wrapper: any

await act(async () => {
wrapper = await mount(
<Provider store={store}>
<MemoryRouter initialEntries={['/labs/1234']}>
<Labs />
</MemoryRouter>
</Provider>,
)

expect(wrapper.find(ViewLab)).toHaveLength(1)
})
})

it('should not navigate to /labs/:id if the user does not have ViewLab permissions', async () => {
const store = mockStore({
title: 'test',
user: { permissions: [] },
breadcrumbs: { breadcrumbs: [] },
components: { sidebarCollapsed: false },
})

const wrapper = await mount(
<Provider store={store}>
<MemoryRouter initialEntries={['/labs/1234']}>
<Labs />
</MemoryRouter>
</Provider>,
)

expect(wrapper.find(ViewLab)).toHaveLength(0)
})
})
})
})

0 comments on commit f354c94

Please sign in to comment.