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

feat(patient): add paging and sorting functionality #2026

Merged
merged 65 commits into from
Jun 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
dff2b3e
feat(viewpatients): add paging feature in ViewPatients component
akshay-ap Apr 28, 2020
c4109a4
feat: add Sort request in ViewPatients
akshay-ap Apr 28, 2020
11b6c8b
fix(viewpatients.tsx): add userPageRequest in dependency array
akshay-ap Apr 28, 2020
d302f89
Merge branch 'master' into feat-#1969
matteovivona Apr 29, 2020
7cf0f41
Merge branch 'master' into feat-#1969
Apr 29, 2020
025a4b8
Merge branch 'master' into feat-#1969
Apr 30, 2020
5308f5f
feat(viewpatients): refactor code as recommended
akshay-ap May 1, 2020
8727f38
Merge branch 'feat-#1969' of https://github.com/akshay-ap/hospitalrun…
akshay-ap May 1, 2020
7a2f820
Merge branch 'master' into feat-#1969
May 2, 2020
6b90ee1
Merge branch 'master' into feat-#1969
May 2, 2020
0e26ac5
Merge branch 'master' into feat-#1969
May 2, 2020
629c58b
Merge branch 'master' into feat-#1969
May 2, 2020
d1c55e7
feat(viewpatients): add a new field 'index', paging in next direction
akshay-ap May 3, 2020
810f49c
Merge branch 'feat-#1969' of https://github.com/akshay-ap/hospitalrun…
akshay-ap May 3, 2020
220ad9c
test: fix failing tests
akshay-ap May 3, 2020
c62b13d
Merge branch 'master' into feat-#1969
May 3, 2020
52a59d3
feat(viewpatients): enables to navigation to previous page
akshay-ap May 3, 2020
f2608f5
Merge branch 'feat-#1969' of https://github.com/akshay-ap/hospitalrun…
akshay-ap May 3, 2020
e95e04a
Merge branch 'master' into feat-#1969
May 3, 2020
6cdf785
Merge branch 'master' into feat-#1969
May 3, 2020
923560f
refactor: resolve merge conflict
akshay-ap May 5, 2020
c91088f
Merge branch 'master' into feat-#1969
May 5, 2020
66e23d1
Merge branch 'master' into feat-#1969
May 5, 2020
b96680f
feat(viewpatients): add paging for search patients
akshay-ap May 5, 2020
0be439d
Merge branch 'feat-#1969' of https://github.com/akshay-ap/hospitalrun…
akshay-ap May 5, 2020
c453f01
refactor(useupdateeffect): disable warning
akshay-ap May 5, 2020
acc9023
Merge branch 'master' into feat-#1969
May 6, 2020
ef09ba7
Merge branch 'master' into feat-#1969
May 6, 2020
d7defb5
test(patientrepository.test.ts): add test cases for paging
akshay-ap May 10, 2020
570947f
Merge branch 'feat-#1969' of https://github.com/akshay-ap/hospitalrun…
akshay-ap May 10, 2020
71ac1ca
refactor(patientrepository): changes for findAllPaged
akshay-ap May 12, 2020
7411ad0
feat(pagecomponent): user can change page size
akshay-ap May 14, 2020
efba900
refactor: resolve merge conflict
akshay-ap May 14, 2020
10c426d
Merge branch 'master' into feat-#1969
May 16, 2020
05cf4d4
Merge branch 'master' into feat-#1969
May 16, 2020
62c2c59
refactor: resolve merge conflict
akshay-ap May 18, 2020
7797677
Merge branch 'feat-#1969' of https://github.com/akshay-ap/hospitalrun…
akshay-ap May 18, 2020
bff3ddd
Merge branch 'master' into feat-#1969
May 19, 2020
70c3481
Merge branch 'master' into feat-#1969
May 19, 2020
948c95c
Merge branch 'master' into feat-#1969
May 19, 2020
b285303
Merge branch 'master' into feat-#1969
May 20, 2020
edd1f6d
Merge branch 'master' into feat-#1969
May 20, 2020
671ad02
feat(viewpatients): add Tests, Fix bug
akshay-ap May 20, 2020
62c37b7
Merge branch 'feat-#1969' of https://github.com/akshay-ap/hospitalrun…
akshay-ap May 20, 2020
f5e0f07
Merge branch 'master' into feat-#1969
May 23, 2020
630fcaf
Merge branch 'master' into feat-#1969
May 23, 2020
cf0fbfd
Merge branch 'master' into feat-#1969
May 23, 2020
82b671d
Merge branch 'master' into feat-#1969
May 23, 2020
f11bb49
Merge branch 'master' into feat-#1969
May 23, 2020
3c0b3d9
Merge branch 'master' into feat-#1969
May 23, 2020
09173f9
Merge branch 'master' into feat-#1969
May 23, 2020
47f693c
Merge branch 'master' into feat-#1969
May 23, 2020
7cd9d20
Merge branch 'master' into feat-#1969
May 24, 2020
76759ad
Merge branch 'master' into feat-#1969
May 26, 2020
f9f70e7
Merge branch 'master' into feat-#1969
May 26, 2020
fb63b64
Merge branch 'master' into feat-#1969
May 28, 2020
d69a641
Merge branch 'master' into feat-#1969
May 30, 2020
6d15f9f
refactor(pagerequest): add new line
akshay-ap May 31, 2020
0677b2a
Merge branch 'master' into feat-#1969
Jun 2, 2020
19b1e70
Merge branch 'master' into feat-#1969
Jun 2, 2020
2dcc5a3
Merge branch 'master' into feat-#1969
Jun 3, 2020
e291484
Merge branch 'master' into feat-#1969
Jun 4, 2020
5eef660
Merge branch 'master' into feat-#1969
matteovivona Jun 5, 2020
10f5c77
Merge branch 'master' into feat-#1969
Jun 7, 2020
abcf92c
Merge branch 'master' into feat-#1969
Jun 7, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
195 changes: 195 additions & 0 deletions src/__tests__/clients/db/PatientRepository.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { getTime, isAfter } from 'date-fns'
import shortid from 'shortid'

import PageRequest, { UnpagedRequest } from '../../../clients/db/PageRequest'
import PatientRepository from '../../../clients/db/PatientRepository'
import SortRequest from '../../../clients/db/SortRequest'
import { patients } from '../../../config/pouchdb'
import Patient from '../../../model/Patient'

Expand Down Expand Up @@ -224,4 +226,197 @@ describe('patient repository', () => {
expect(allDocs.total_rows).toEqual(0)
})
})

describe('findAllPaged', () => {
const patientsData = [
{ _id: 'a', fullName: 'a', code: 'P-a', index: 'aP-a' },
{ _id: 'b', fullName: 'b', code: 'P-b', index: 'bP-b' },
{ _id: 'c', fullName: 'c', code: 'P-c', index: 'cP-c' },
]

afterEach(async () => {
await removeAllDocs()
})

beforeEach(async () => {
await PatientRepository.createIndex()
patientsData.forEach((patientData) => patients.put(patientData))
})

it('should find all patients in the database', async () => {
const result = await PatientRepository.findAllPaged()

expect(result.hasNext).toEqual(false)
expect(result.hasPrevious).toEqual(false)
expect(result.content).toHaveLength(patientsData.length)
})

it('should find all patients in the database with sort request and page request', async () => {
const sortRequest: SortRequest = {
sorts: [{ field: 'index', direction: 'asc' }],
}
const pageRequest: PageRequest = {
number: 1,
size: 1,
direction: 'next',
nextPageInfo: undefined,
previousPageInfo: undefined,
}

const result = await PatientRepository.findAllPaged(sortRequest, pageRequest)

expect(result.content).toHaveLength(1)
expect(result.hasNext).toEqual(true)
expect(result.hasPrevious).toEqual(false)
expect(result.pageRequest?.nextPageInfo).toEqual({ index: 'bP-b' })
})

it('page request less than number of records', async () => {
const sortRequest: SortRequest = {
sorts: [{ field: 'index', direction: 'asc' }],
}

const pageRequest: PageRequest = {
number: 1,
size: 4,
direction: 'next',
nextPageInfo: undefined,
previousPageInfo: undefined,
}

const result = await PatientRepository.findAllPaged(sortRequest, pageRequest)

expect(result.content).toHaveLength(patientsData.length)
expect(result.hasNext).toEqual(false)
expect(result.hasPrevious).toEqual(false)
expect(result.pageRequest?.nextPageInfo).toBe(undefined)
expect(result.pageRequest?.previousPageInfo).toBe(undefined)
})

it('go till last page', async () => {
const sortRequest: SortRequest = {
sorts: [{ field: 'index', direction: 'asc' }],
}
const pageRequest1: PageRequest = {
number: 1,
size: 1,
direction: 'next',
nextPageInfo: undefined,
previousPageInfo: undefined,
}

const result1 = await PatientRepository.findAllPaged(sortRequest, pageRequest1)

const pageRequest2: PageRequest = {
number: 2,
size: 1,
direction: 'next',
nextPageInfo: result1.pageRequest?.nextPageInfo,
previousPageInfo: undefined,
}
const result2 = await PatientRepository.findAllPaged(sortRequest, pageRequest2)

expect(result2.hasPrevious).toBe(true)
expect(result2.hasNext).toBe(true)

const pageRequest3: PageRequest = {
number: 2,
size: 1,
direction: 'next',
nextPageInfo: result2.pageRequest?.nextPageInfo,
previousPageInfo: undefined,
}
const result3 = await PatientRepository.findAllPaged(sortRequest, pageRequest3)

expect(result3.content).toHaveLength(1)
expect(result3.hasNext).toEqual(false)
expect(result3.hasPrevious).toEqual(true)
expect(result3.content.length).toEqual(1)
expect(result3.pageRequest?.previousPageInfo).toEqual({ index: 'cP-c' })
})

it('go to previous page', async () => {
const sortRequest: SortRequest = {
sorts: [{ field: 'index', direction: 'asc' }],
}
const pageRequest1: PageRequest = {
number: 1,
size: 1,
direction: 'next',
nextPageInfo: undefined,
previousPageInfo: undefined,
}

const result1 = await PatientRepository.findAllPaged(sortRequest, pageRequest1)

const pageRequest2: PageRequest = {
number: 2,
size: 1,
direction: 'next',
nextPageInfo: result1.pageRequest?.nextPageInfo,
previousPageInfo: undefined,
}
const result2 = await PatientRepository.findAllPaged(sortRequest, pageRequest2)

expect(result2.hasPrevious).toBe(true)
expect(result2.hasNext).toBe(true)

const pageRequest3: PageRequest = {
number: 1,
size: 1,
direction: 'previous',
nextPageInfo: undefined,
previousPageInfo: result2.pageRequest?.previousPageInfo,
}
const result3 = await PatientRepository.findAllPaged(sortRequest, pageRequest3)

expect(result3.content).toHaveLength(1)
expect(result3.hasNext).toEqual(true)
expect(result3.hasPrevious).toEqual(false)
expect(result3.content.length).toEqual(1)
expect(result3.content[0].index).toEqual('aP-a')
expect(result3.pageRequest?.nextPageInfo).toEqual({ index: 'bP-b' })
})
})

describe('searchPaged', () => {
const patientsData = [
{ _id: 'a', fullName: 'a', code: 'P-a', index: 'aP-a' },
{ _id: 'b', fullName: 'b', code: 'P-b', index: 'bP-b' },
{ _id: 'c', fullName: 'c', code: 'P-c', index: 'cP-c' },
]

afterEach(async () => {
await removeAllDocs()
})

beforeEach(async () => {
await PatientRepository.createIndex()
patientsData.forEach((patientData) => patients.put(patientData))
})

it('should search patient in the database', async () => {
const result = await PatientRepository.searchPaged('a')

expect(result.content).toHaveLength(1)
expect(result.hasNext).toEqual(false)
expect(result.hasPrevious).toEqual(false)

expect(result.content.length).toEqual(1)
})

it('should search patient in the database with sort request', async () => {
const sortRequest: SortRequest = {
sorts: [{ field: 'index', direction: 'asc' }],
}

const result = await PatientRepository.searchPaged('a', UnpagedRequest, sortRequest)

expect(result.content).toHaveLength(1)
expect(result.hasNext).toEqual(false)
expect(result.hasPrevious).toEqual(false)

expect(result.content.length).toEqual(1)
})
})
})
31 changes: 31 additions & 0 deletions src/__tests__/components/PageComponent.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import '../../__mocks__/matchMediaMock'
import { Button, Select } from '@hospitalrun/components'
import { mount } from 'enzyme'
import React from 'react'

import PageComponent, { defaultPageSize } from '../../components/PageComponent'

describe('PageComponenet test', () => {
it('should render PageComponent Component', () => {
const wrapper = mount(
<PageComponent
hasNext={false}
hasPrevious={false}
pageNumber={1}
setPreviousPageRequest={jest.fn()}
setNextPageRequest={jest.fn()}
onPageSizeChange={jest.fn()}
/>,
)
const buttons = wrapper.find(Button)
expect(buttons).toHaveLength(2)
expect(buttons.at(0).prop('disabled')).toBeTruthy()
expect(buttons.at(1).prop('disabled')).toBeTruthy()

const select = wrapper.find(Select)
expect(select.prop('defaultValue')).toEqual(defaultPageSize.value?.toString())

const options = select.find('option')
expect(options).toHaveLength(5)
})
})
15 changes: 15 additions & 0 deletions src/__tests__/hooks/useUpdateEffect.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { renderHook } from '@testing-library/react-hooks'

import useUpdateEffect from '../../hooks/useUpdateEffect'

describe('useUpdateEffect', () => {
it('should call the function after udpate', () => {
const mockFn = jest.fn()
let someVal = 'someVal'
const { rerender } = renderHook(() => useUpdateEffect(mockFn, [someVal]))
expect(mockFn).not.toHaveBeenCalled()
someVal = 'newVal'
rerender()
expect(mockFn).toHaveBeenCalledTimes(1)
})
})
1 change: 1 addition & 0 deletions src/__tests__/patients/edit/EditPatient.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ describe('Edit Patient', () => {
address: 'address',
code: 'P00001',
dateOfBirth: subDays(new Date(), 2).toISOString(),
index: 'givenName familyName suffixP00001',
} as Patient

let history: any
Expand Down
Loading