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

feat(labs): ability to filter by status on labs screen #2033

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,5 @@ typings/
package-lock.json
.DS_Store
yarn.lock
.vscode/launch.json
.vscode/tasks.json
1 change: 1 addition & 0 deletions src/__tests__/HospitalRun.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ describe('HospitalRun', () => {
const store = mockStore({
title: 'test',
user: { permissions: [Permissions.ViewLabs] },
labs: { labs: [] },
breadcrumbs: { breadcrumbs: [] },
components: { sidebarCollapsed: false },
})
Expand Down
151 changes: 151 additions & 0 deletions src/__tests__/clients/db/LabRepository.tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/* eslint "@typescript-eslint/camelcase": "off" */
sotous marked this conversation as resolved.
Show resolved Hide resolved
import { labs } from 'config/pouchdb'
import LabRepository from 'clients/db/LabRepository'
import SortRequest from 'clients/db/SortRequest'

interface SearchContainer {
text: string
status: 'requested' | 'completed' | 'canceled' | 'all'
sortRequest: SortRequest
}

const removeAllDocs = async () => {
const allDocs = await labs.allDocs({ include_docs: true })
await Promise.all(
allDocs.rows.map(async (row) => {
if (row.doc) {
await labs.remove(row.doc)
}
}),
)
}

const sortRequest: SortRequest = {
sorts: [
{
field: 'requestedOn',
direction: 'desc',
},
],
}

const searchObject: SearchContainer = {
text: '',
status: 'all',
sortRequest,
}

describe('lab repository', () => {
describe('find', () => {
afterEach(async () => {
await removeAllDocs()
})

it('should return a lab with the correct data', async () => {
// first lab to store is to have mock data to make sure we are getting the expected
await labs.put({ _id: 'id1111' })
const expectedLab = await labs.put({ _id: 'id2222' })

const actualLab = await LabRepository.find('id2222')

expect(actualLab).toBeDefined()
expect(actualLab.id).toEqual(expectedLab.id)
})
})

describe('search', () => {
it('should return all records that lab type matches search text', async () => {
const expectedLabType = 'more tests'
await labs.put({ _id: 'someId1', type: expectedLabType, status: 'requested' })
await labs.put({ _id: 'someId2', type: 'P00002', status: 'requested' })

searchObject.text = expectedLabType

const result = await LabRepository.search(searchObject)

expect(result).toHaveLength(1)
expect(result[0].type).toEqual(expectedLabType)
})

it('should return all records that contains search text in the type', async () => {
const expectedLabType = 'Labs Tests'
await labs.put({ _id: 'someId3', type: expectedLabType })
await labs.put({ _id: 'someId4', type: 'Sencond Lab labs tests' })
await labs.put({ _id: 'someId5', type: 'not found' })

searchObject.text = expectedLabType

const result = await LabRepository.search(searchObject)

expect(result).toHaveLength(2)
expect(result[0].id).toEqual('someId3')
expect(result[1].id).toEqual('someId4')
})

it('should match search criteria with case insesitive match', async () => {
await labs.put({ _id: 'id3333', type: 'lab tests' })
await labs.put({ _id: 'id4444', type: 'not found' })

searchObject.text = 'LAB TESTS'

const result = await LabRepository.search(searchObject)

expect(result).toHaveLength(1)
expect(result[0].id).toEqual('id3333')
})

it('should return all records that matches an specific status', async () => {
await labs.put({ _id: 'id5555', type: 'lab tests', status: 'requested' })
await labs.put({ _id: 'id6666', type: 'lab tests', status: 'requested' })
await labs.put({ _id: 'id7777', type: 'lab tests', status: 'completed' })
await labs.put({ _id: 'id8888', type: 'not found', status: 'completed' })

const searchData: SearchContainer = {
text: '',
status: 'completed',
sortRequest,
}

const result = await LabRepository.search(searchData)

expect(result).toHaveLength(2)
expect(result[0].id).toEqual('id7777')
expect(result[1].id).toEqual('id8888')
})

it('should return records with search text and specific status', async () => {
await labs.put({ _id: 'theID09', type: 'the specific lab', status: 'requested' })
await labs.put({ _id: 'theID10', type: 'not found', status: 'cancelled' })

const expectedSearch: SearchContainer = {
text: 'the specific lab',
status: 'requested',
sortRequest,
}

const result = await LabRepository.search(expectedSearch)

expect(result).toHaveLength(1)
expect(result[0].id).toEqual('theID09')
})
})

describe('findAll', () => {
afterEach(async () => {
await removeAllDocs()
})
it('should find all labs in the database sorted by their requestedOn', async () => {
const expectedLab1 = await labs.put({ _id: 'theID11' })

setTimeout(async () => {
const expectedLab2 = await labs.put({ _id: 'theID12' })

const result = await LabRepository.findAll()

expect(result).toHaveLength(2)
expect(result[0].id).toEqual(expectedLab1.id)
expect(result[1].id).toEqual(expectedLab2.id)
}, 1000)
})
})
})
94 changes: 64 additions & 30 deletions src/__tests__/labs/ViewLabs.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import { Provider } from 'react-redux'
import { Router } from 'react-router'
import ViewLabs from 'labs/ViewLabs'
import { mount, ReactWrapper } from 'enzyme'
import { TextInput } from '@hospitalrun/components'
import configureMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'
import { createMemoryHistory } from 'history'
import Permissions from 'model/Permissions'
import { act } from '@testing-library/react'
import LabRepository from 'clients/db/LabRepository'
import * as labsSlice from 'labs/labs-slice'
import Lab from 'model/Lab'
import format from 'date-fns/format'
import * as ButtonBarProvider from 'page-header/ButtonBarProvider'
import SortRequest from 'clients/db/SortRequest'
import * as titleUtil from '../../page-header/useTitle'

const mockStore = configureMockStore([thunk])
Expand All @@ -25,6 +26,7 @@ describe('View Labs', () => {
const store = mockStore({
title: '',
user: { permissions: [Permissions.ViewLabs, Permissions.RequestLab] },
labs: { labs: [] },
})
titleSpy = jest.spyOn(titleUtil, 'default')
jest.spyOn(LabRepository, 'findAll').mockResolvedValue([])
Expand All @@ -49,6 +51,7 @@ describe('View Labs', () => {
const store = mockStore({
title: '',
user: { permissions: [Permissions.ViewLabs, Permissions.RequestLab] },
labs: { labs: [] },
})
const setButtonToolBarSpy = jest.fn()
jest.spyOn(ButtonBarProvider, 'useButtonToolbarSetter').mockReturnValue(setButtonToolBarSpy)
Expand All @@ -71,6 +74,7 @@ describe('View Labs', () => {
const store = mockStore({
title: '',
user: { permissions: [Permissions.ViewLabs] },
labs: { labs: [] },
})
const setButtonToolBarSpy = jest.fn()
jest.spyOn(ButtonBarProvider, 'useButtonToolbarSetter').mockReturnValue(setButtonToolBarSpy)
Expand Down Expand Up @@ -105,6 +109,7 @@ describe('View Labs', () => {
const store = mockStore({
title: '',
user: { permissions: [Permissions.ViewLabs, Permissions.RequestLab] },
labs: { labs: [expectedLab] },
})
history = createMemoryHistory()

Expand Down Expand Up @@ -162,36 +167,65 @@ describe('View Labs', () => {
})
})

describe('sort Request', () => {
let findAllSpy: any
beforeEach(async () => {
const store = mockStore({
title: '',
user: { permissions: [Permissions.ViewLabs, Permissions.RequestLab] },
describe('search functionality', () => {
sotous marked this conversation as resolved.
Show resolved Hide resolved
beforeEach(() => jest.useFakeTimers())

afterEach(() => jest.useRealTimers())

it('should search for labs after the search text has not changed for 500 milliseconds', () => {
const searchLabsSpy = jest.spyOn(labsSlice, 'searchLabs')
let wrapper: ReactWrapper
let history: any
const expectedLab = {
id: '1234',
type: 'lab type',
patientId: 'patientId',
status: 'requested',
requestedOn: '2020-03-30T04:43:20.102Z',
} as Lab

beforeEach(async () => {
const store = mockStore({
title: '',
user: { permissions: [Permissions.ViewLabs, Permissions.RequestLab] },
labs: { labs: [expectedLab] },
})
history = createMemoryHistory()

jest.spyOn(LabRepository, 'findAll').mockResolvedValue([expectedLab])
await act(async () => {
wrapper = await mount(
<Provider store={store}>
<Router history={history}>
<ViewLabs />
</Router>
</Provider>,
)
})

searchLabsSpy.mockClear()
const expectedSearchText = 'search text'

act(() => {
;(wrapper.find(TextInput).prop('onChange') as any)({
target: {
value: expectedSearchText,
},
preventDefault(): void {
// noop
},
sotous marked this conversation as resolved.
Show resolved Hide resolved
} as React.ChangeEvent<HTMLInputElement>)
})

act(() => {
jest.advanceTimersByTime(500)
})

wrapper.update()

expect(searchLabsSpy).toHaveBeenCalledTimes(1)
expect(searchLabsSpy).toHaveBeenLastCalledWith(expectedSearchText)
})
findAllSpy = jest.spyOn(LabRepository, 'findAll')
findAllSpy.mockResolvedValue([])
await act(async () => {
await mount(
<Provider store={store}>
<Router history={createMemoryHistory()}>
<ViewLabs />
</Router>
</Provider>,
)
})
})

it('should have called findAll with sort request', () => {
const sortRequest: SortRequest = {
sorts: [
{
field: 'requestedOn',
direction: 'desc',
},
],
}
expect(findAllSpy).toHaveBeenCalledWith(sortRequest)
})
})
})
Loading