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

Commit

Permalink
feat(labs): ability to filter by status on labs screen (#2033)
Browse files Browse the repository at this point in the history
  • Loading branch information
sotous committed May 6, 2020
1 parent adcad7b commit 2b5c789
Show file tree
Hide file tree
Showing 13 changed files with 633 additions and 82 deletions.
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 @@ -23,6 +23,7 @@ describe('HospitalRun', () => {
const store = mockStore({
title: 'test',
user: { permissions: [Permissions.ViewLabs] },
labs: { labs: [] },
breadcrumbs: { breadcrumbs: [] },
components: { sidebarCollapsed: false },
})
Expand Down
170 changes: 163 additions & 7 deletions src/__tests__/clients/db/LabRepository.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,170 @@
/* eslint "@typescript-eslint/camelcase": "off" */
import shortid from 'shortid'
import LabRepository from '../../../clients/db/LabRepository'
import { labs } from 'config/pouchdb'
import LabRepository from 'clients/db/LabRepository'
import SortRequest from 'clients/db/SortRequest'
import Lab from '../../../model/Lab'

interface SearchContainer {
text: string
status: 'requested' | 'completed' | 'canceled' | 'all'
defaultSortRequest: 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 defaultSortRequest: SortRequest = {
sorts: [
{
field: 'requestedOn',
direction: 'desc',
},
],
}

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

describe('lab repository', () => {
it('should generate a lab code', async () => {
const newLab = await LabRepository.save({
patientId: '123',
type: 'test',
} as Lab)
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)
})

it('should generate a lab code', async () => {
const newLab = await LabRepository.save({
patientId: '123',
type: 'test',
} as Lab)

expect(shortid.isValid(newLab.code)).toBeTruthy()
})
})

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 return all records that contains search text in code', async () => {
const expectedLabCode = 'L-CODE-sam445Pl'
await labs.put({ _id: 'theID13', type: 'Expected', code: 'L-CODE-sam445Pl' })
await labs.put({ _id: 'theID14', type: 'Sencond Lab labs tests', code: 'L-4XXX' })
await labs.put({ _id: 'theID15', type: 'not found', code: 'L-775YYxc' })

searchObject.text = expectedLabCode

const result = await LabRepository.search(searchObject)

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

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' })

searchObject.text = ''
searchObject.status = 'completed'

const result = await LabRepository.search(searchObject)

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' })

searchObject.text = 'the specific lab'
searchObject.status = 'requested'

const result = await LabRepository.search(searchObject)

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(shortid.isValid(newLab.code)).toBeTruthy()
expect(result).toHaveLength(2)
expect(result[0].id).toEqual(expectedLab1.id)
expect(result[1].id).toEqual(expectedLab2.id)
}, 1000)
})
})
})
138 changes: 110 additions & 28 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, Select } 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 @@ -106,6 +110,7 @@ describe('View Labs', () => {
const store = mockStore({
title: '',
user: { permissions: [Permissions.ViewLabs, Permissions.RequestLab] },
labs: { labs: [expectedLab] },
})
history = createMemoryHistory()

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

describe('sort Request', () => {
let findAllSpy: any
beforeEach(async () => {
const store = mockStore({
title: '',
user: { permissions: [Permissions.ViewLabs, Permissions.RequestLab] },
})
findAllSpy = jest.spyOn(LabRepository, 'findAll')
findAllSpy.mockResolvedValue([])
await act(async () => {
await mount(
<Provider store={store}>
<Router history={createMemoryHistory()}>
<ViewLabs />
</Router>
</Provider>,
)
describe('dropdown', () => {
it('should search for labs when dropdown changes', () => {
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()

await act(async () => {
wrapper = await mount(
<Provider store={store}>
<Router history={history}>
<ViewLabs />
</Router>
</Provider>,
)
})

searchLabsSpy.mockClear()

act(() => {
const onChange = wrapper.find(Select).prop('onChange') as any
onChange({
target: {
value: 'requested',
},
preventDefault: jest.fn(),
})
})

wrapper.update()
expect(searchLabsSpy).toHaveBeenCalledTimes(1)
})
})
})

describe('search functionality', () => {
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

it('should have called findAll with sort request', () => {
const sortRequest: SortRequest = {
sorts: [
{
field: 'requestedOn',
direction: 'desc',
},
],
}
expect(findAllSpy).toHaveBeenCalledWith(sortRequest)
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(() => {
const onClick = wrapper.find(TextInput).prop('onChange') as any
onClick({
target: {
value: expectedSearchText,
},
preventDefault: jest.fn(),
})
})

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

wrapper.update()

expect(searchLabsSpy).toHaveBeenCalledTimes(1)
expect(searchLabsSpy).toHaveBeenLastCalledWith(expectedSearchText)
})
})
})
})
Loading

1 comment on commit 2b5c789

@vercel
Copy link

@vercel vercel bot commented on 2b5c789 May 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.