From f354c940ceac9e41b6b92d21fc37c91c7eb09121 Mon Sep 17 00:00:00 2001 From: Jack Meyer Date: Thu, 2 Apr 2020 23:00:25 -0500 Subject: [PATCH] feat(labs): adds test for lab related routes --- src/__tests__/HospitalRun.test.tsx | 45 ++++++ src/__tests__/components/Navbar.test.tsx | 10 +- src/__tests__/components/Sidebar.test.tsx | 70 ++-------- src/__tests__/labs/Labs.test.tsx | 114 +++++++++++++++- src/__tests__/labs/ViewLab.test.tsx | 128 ++++-------------- src/__tests__/labs/ViewLabs.test.tsx | 52 ++----- .../labs/requests/NewLabRequest.test.tsx | 5 +- 7 files changed, 205 insertions(+), 219 deletions(-) diff --git a/src/__tests__/HospitalRun.test.tsx b/src/__tests__/HospitalRun.test.tsx index 1f79afa2fc..f0fa359177 100644 --- a/src/__tests__/HospitalRun.test.tsx +++ b/src/__tests__/HospitalRun.test.tsx @@ -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' @@ -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( + + + + + , + ) + + 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( + + + + + , + ) + + expect(wrapper.find(ViewLabs)).toHaveLength(0) + expect(wrapper.find(Dashboard)).toHaveLength(1) + }) + }) }) describe('layout', () => { diff --git a/src/__tests__/components/Navbar.test.tsx b/src/__tests__/components/Navbar.test.tsx index 775dd11e95..8d10a545b0 100644 --- a/src/__tests__/components/Navbar.test.tsx +++ b/src/__tests__/components/Navbar.test.tsx @@ -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') }) diff --git a/src/__tests__/components/Sidebar.test.tsx b/src/__tests__/components/Sidebar.test.tsx index afb593fb67..11bf2804af 100644 --- a/src/__tests__/components/Sidebar.test.tsx +++ b/src/__tests__/components/Sidebar.test.tsx @@ -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 /', () => { @@ -71,12 +66,7 @@ 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', () => { @@ -84,12 +74,7 @@ describe('Sidebar', () => { 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', () => { @@ -97,12 +82,7 @@ describe('Sidebar', () => { 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', () => { @@ -175,12 +155,7 @@ 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', () => { @@ -188,12 +163,7 @@ describe('Sidebar', () => { 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', () => { @@ -201,12 +171,7 @@ describe('Sidebar', () => { 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', () => { @@ -279,12 +244,7 @@ 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', () => { @@ -292,12 +252,7 @@ describe('Sidebar', () => { 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', () => { @@ -305,12 +260,7 @@ describe('Sidebar', () => { 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', () => { diff --git a/src/__tests__/labs/Labs.test.tsx b/src/__tests__/labs/Labs.test.tsx index 83960a6136..178525211c 100644 --- a/src/__tests__/labs/Labs.test.tsx +++ b/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( + + + + + , + ) + + 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( + + + + + , + ) + + 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( + + + + + , + ) + + 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( + + + + + , + ) + + expect(wrapper.find(ViewLab)).toHaveLength(0) + }) + }) + }) }) diff --git a/src/__tests__/labs/ViewLab.test.tsx b/src/__tests__/labs/ViewLab.test.tsx index 3f192495e0..ab3008a498 100644 --- a/src/__tests__/labs/ViewLab.test.tsx +++ b/src/__tests__/labs/ViewLab.test.tsx @@ -84,57 +84,27 @@ describe('View Labs', () => { const expectedLab = { ...mockLab } as Lab const wrapper = await setup(expectedLab, [Permissions.ViewLab]) const forPatientDiv = wrapper.find('.for-patient') - expect( - forPatientDiv - .find('h4') - .text() - .trim(), - ).toEqual('labs.lab.for') - - expect( - forPatientDiv - .find('h5') - .text() - .trim(), - ).toEqual(mockPatient.fullName) + expect(forPatientDiv.find('h4').text().trim()).toEqual('labs.lab.for') + + expect(forPatientDiv.find('h5').text().trim()).toEqual(mockPatient.fullName) }) it('should display the lab type for type', async () => { const expectedLab = { ...mockLab, type: 'expected type' } as Lab const wrapper = await setup(expectedLab, [Permissions.ViewLab]) const labTypeDiv = wrapper.find('.lab-type') - expect( - labTypeDiv - .find('h4') - .text() - .trim(), - ).toEqual('labs.lab.type') - - expect( - labTypeDiv - .find('h5') - .text() - .trim(), - ).toEqual(expectedLab.type) + expect(labTypeDiv.find('h4').text().trim()).toEqual('labs.lab.type') + + expect(labTypeDiv.find('h5').text().trim()).toEqual(expectedLab.type) }) it('should display the requested on date', async () => { const expectedLab = { ...mockLab, requestedOn: '2020-03-30T04:43:20.102Z' } as Lab const wrapper = await setup(expectedLab, [Permissions.ViewLab]) const requestedOnDiv = wrapper.find('.requested-on') - expect( - requestedOnDiv - .find('h4') - .text() - .trim(), - ).toEqual('labs.lab.requestedOn') - - expect( - requestedOnDiv - .find('h5') - .text() - .trim(), - ).toEqual('2020-03-29 11:43 PM') + expect(requestedOnDiv.find('h4').text().trim()).toEqual('labs.lab.requestedOn') + + expect(requestedOnDiv.find('h5').text().trim()).toEqual('2020-03-29 11:43 PM') }) it('should not display the completed date if the lab is not completed', async () => { @@ -178,20 +148,13 @@ describe('View Labs', () => { expect(notesTextField.prop('value')).toEqual(expectedLab.notes) }) - it('should display an update button if the lab is in a requested state', async () => {}) - describe('requested lab request', () => { it('should display a warning badge if the status is requested', async () => { const expectedLab = { ...mockLab, status: 'requested' } as Lab const wrapper = await setup(expectedLab, [Permissions.ViewLab]) const labStatusDiv = wrapper.find('.lab-status') const badge = labStatusDiv.find(Badge) - expect( - labStatusDiv - .find('h4') - .text() - .trim(), - ).toEqual('labs.lab.status') + expect(labStatusDiv.find('h4').text().trim()).toEqual('labs.lab.status') expect(badge.prop('color')).toEqual('warning') expect(badge.text().trim()).toEqual(expectedLab.status) @@ -207,26 +170,11 @@ describe('View Labs', () => { ]) const buttons = wrapper.find(Button) - expect( - buttons - .at(0) - .text() - .trim(), - ).toEqual('actions.update') - - expect( - buttons - .at(1) - .text() - .trim(), - ).toEqual('labs.requests.complete') - - expect( - buttons - .at(2) - .text() - .trim(), - ).toEqual('labs.requests.cancel') + expect(buttons.at(0).text().trim()).toEqual('actions.update') + + expect(buttons.at(1).text().trim()).toEqual('labs.requests.complete') + + expect(buttons.at(2).text().trim()).toEqual('labs.requests.cancel') }) }) @@ -237,12 +185,7 @@ describe('View Labs', () => { const labStatusDiv = wrapper.find('.lab-status') const badge = labStatusDiv.find(Badge) - expect( - labStatusDiv - .find('h4') - .text() - .trim(), - ).toEqual('labs.lab.status') + expect(labStatusDiv.find('h4').text().trim()).toEqual('labs.lab.status') expect(badge.prop('color')).toEqual('danger') expect(badge.text().trim()).toEqual(expectedLab.status) @@ -257,19 +200,9 @@ describe('View Labs', () => { const wrapper = await setup(expectedLab, [Permissions.ViewLab]) const canceledOnDiv = wrapper.find('.canceled-on') - expect( - canceledOnDiv - .find('h4') - .text() - .trim(), - ).toEqual('labs.lab.canceledOn') - - expect( - canceledOnDiv - .find('h5') - .text() - .trim(), - ).toEqual('2020-03-29 11:45 PM') + expect(canceledOnDiv.find('h4').text().trim()).toEqual('labs.lab.canceledOn') + + expect(canceledOnDiv.find('h5').text().trim()).toEqual('2020-03-29 11:45 PM') }) it('should not display update, complete, and cancel button if the lab is canceled', async () => { @@ -301,12 +234,7 @@ describe('View Labs', () => { const wrapper = await setup(expectedLab, [Permissions.ViewLab]) const labStatusDiv = wrapper.find('.lab-status') const badge = labStatusDiv.find(Badge) - expect( - labStatusDiv - .find('h4') - .text() - .trim(), - ).toEqual('labs.lab.status') + expect(labStatusDiv.find('h4').text().trim()).toEqual('labs.lab.status') expect(badge.prop('color')).toEqual('primary') expect(badge.text().trim()).toEqual(expectedLab.status) @@ -321,19 +249,9 @@ describe('View Labs', () => { const wrapper = await setup(expectedLab, [Permissions.ViewLab]) const completedOnDiv = wrapper.find('.completed-on') - expect( - completedOnDiv - .find('h4') - .text() - .trim(), - ).toEqual('labs.lab.completedOn') - - expect( - completedOnDiv - .find('h5') - .text() - .trim(), - ).toEqual('2020-03-29 11:44 PM') + expect(completedOnDiv.find('h4').text().trim()).toEqual('labs.lab.completedOn') + + expect(completedOnDiv.find('h5').text().trim()).toEqual('2020-03-29 11:44 PM') }) it('should not display update, complete, and cancel buttons if the lab is completed', async () => { diff --git a/src/__tests__/labs/ViewLabs.test.tsx b/src/__tests__/labs/ViewLabs.test.tsx index b48a542ddb..bc26d86eeb 100644 --- a/src/__tests__/labs/ViewLabs.test.tsx +++ b/src/__tests__/labs/ViewLabs.test.tsx @@ -131,47 +131,17 @@ describe('View Labs', () => { expect(table).toBeDefined() expect(tableHeader).toBeDefined() expect(tableBody).toBeDefined() - expect( - tableColumnHeaders - .at(0) - .text() - .trim(), - ).toEqual('labs.lab.type') - - expect( - tableColumnHeaders - .at(1) - .text() - .trim(), - ).toEqual('labs.lab.requestedOn') - - expect( - tableColumnHeaders - .at(2) - .text() - .trim(), - ).toEqual('labs.lab.status') - - expect( - tableDataColumns - .at(0) - .text() - .trim(), - ).toEqual(expectedLab.type) - - expect( - tableDataColumns - .at(1) - .text() - .trim(), - ).toEqual('2020-03-29 11:43 PM') - - expect( - tableDataColumns - .at(2) - .text() - .trim(), - ).toEqual(expectedLab.status) + expect(tableColumnHeaders.at(0).text().trim()).toEqual('labs.lab.type') + + expect(tableColumnHeaders.at(1).text().trim()).toEqual('labs.lab.requestedOn') + + expect(tableColumnHeaders.at(2).text().trim()).toEqual('labs.lab.status') + + expect(tableDataColumns.at(0).text().trim()).toEqual(expectedLab.type) + + expect(tableDataColumns.at(1).text().trim()).toEqual('2020-03-29 11:43 PM') + + expect(tableDataColumns.at(2).text().trim()).toEqual(expectedLab.status) }) it('should navigate to the lab when the row is clicked', () => { diff --git a/src/__tests__/labs/requests/NewLabRequest.test.tsx b/src/__tests__/labs/requests/NewLabRequest.test.tsx index 3d8eaae423..87575dbc8e 100644 --- a/src/__tests__/labs/requests/NewLabRequest.test.tsx +++ b/src/__tests__/labs/requests/NewLabRequest.test.tsx @@ -21,7 +21,6 @@ const mockStore = configureMockStore([thunk]) describe('New Lab Request', () => { describe('title and breadcrumbs', () => { - let wrapper: ReactWrapper let titleSpy: any const history = createMemoryHistory() @@ -30,7 +29,7 @@ describe('New Lab Request', () => { titleSpy = jest.spyOn(titleUtil, 'default') history.push('/labs/new') - wrapper = mount( + mount( @@ -42,8 +41,6 @@ describe('New Lab Request', () => { it('should have New Lab Request as the title', () => { expect(titleSpy).toHaveBeenCalledWith('labs.requests.new') }) - - it('should render the breadcrumbs', () => {}) }) describe('form layout', () => {