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

feat(viewlabs): sort Labs by requestedOn field #2004

Merged
merged 13 commits into from
Apr 22, 2020
Merged
34 changes: 34 additions & 0 deletions src/__tests__/labs/ViewLabs.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import LabRepository from 'clients/db/LabRepository'
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 Down Expand Up @@ -160,4 +161,37 @@ describe('View Labs', () => {
expect(history.location.pathname).toEqual(`/labs/${expectedLab.id}`)
})
})

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

it('should have called findAll with sort request', () => {
const sortRequest: SortRequest = {
sorts: [
{
field: 'requestedOn',
direction: 'desc',
},
],
}
expect(findAllSpy).toHaveBeenCalledWith(sortRequest)
})
})
})
11 changes: 10 additions & 1 deletion src/labs/ViewLabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useButtonToolbarSetter } from 'page-header/ButtonBarProvider'
import { Button } from '@hospitalrun/components'
import { useHistory } from 'react-router'
import LabRepository from 'clients/db/LabRepository'
import SortRequest from 'clients/db/SortRequest'
import Lab from 'model/Lab'
import { useSelector } from 'react-redux'
import Permissions from 'model/Permissions'
Expand Down Expand Up @@ -38,7 +39,15 @@ const ViewLabs = () => {

useEffect(() => {
const fetch = async () => {
const fetchedLabs = await LabRepository.findAll()
const sortRequest: SortRequest = {
akshay-ap marked this conversation as resolved.
Show resolved Hide resolved
sorts: [
{
field: 'requestedOn',
direction: 'desc',
},
],
}
const fetchedLabs = await LabRepository.findAll(sortRequest)
setLabs(fetchedLabs)
}

Expand Down