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

Commit

Permalink
Merge pull request #2026 from akshay-ap/feat-#1969
Browse files Browse the repository at this point in the history
feat(patient): add paging and sorting functionality
  • Loading branch information
fox1t authored Jun 8, 2020
2 parents f3b3c7f + abcf92c commit 8089564
Show file tree
Hide file tree
Showing 17 changed files with 778 additions and 64 deletions.
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

1 comment on commit 8089564

@vercel
Copy link

@vercel vercel bot commented on 8089564 Jun 8, 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.