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

Commit

Permalink
feat(viewpatients): add Tests, Fix bug
Browse files Browse the repository at this point in the history
feat #1969
  • Loading branch information
akshay-ap committed May 20, 2020
1 parent 7797677 commit 671ad02
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 6 deletions.
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)
})
})
44 changes: 43 additions & 1 deletion src/__tests__/patients/list/ViewPatients.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import '../../../__mocks__/matchMediaMock'

import { TextInput, Spinner } from '@hospitalrun/components'
import { TextInput, Spinner, Select } from '@hospitalrun/components'
import format from 'date-fns/format'
import { mount } from 'enzyme'
import React from 'react'
Expand All @@ -13,6 +13,7 @@ import { mocked } from 'ts-jest/utils'

import { UnpagedRequest } from '../../../clients/db/PageRequest'
import PatientRepository from '../../../clients/db/PatientRepository'
import SortRequest from '../../../clients/db/SortRequest'
import Page from '../../../clients/Page'
import { defaultPageSize } from '../../../components/PageComponent'
import Patient from '../../../model/Patient'
Expand Down Expand Up @@ -155,6 +156,47 @@ describe('Patients', () => {
})
})

describe('change page size', () => {
afterEach(() => {
jest.restoreAllMocks()
})
it('should call the change handler on change', () => {
const searchPagedSpy = jest.spyOn(patientSlice, 'searchPatients')
const wrapper = setup()
const sortRequest: SortRequest = {
sorts: [{ field: 'index', direction: 'asc' }],
}

expect(searchPagedSpy).toBeCalledWith('', sortRequest, {
direction: 'next',
nextPageInfo: { index: null },
number: 1,
previousPageInfo: { index: null },
size: defaultPageSize.value,
})

act(() => {
;(wrapper.find(Select).prop('onChange') as any)({
target: {
value: '50',
},
} as React.ChangeEvent<HTMLInputElement>)
})

wrapper.update()

expect(searchPagedSpy).toHaveBeenCalledTimes(2)

expect(searchPagedSpy).toBeCalledWith('', sortRequest, {
direction: 'next',
nextPageInfo: { index: null },
number: 1,
previousPageInfo: { index: null },
size: 50,
})
})
})

describe('search functionality', () => {
beforeEach(() => jest.useFakeTimers())

Expand Down
10 changes: 5 additions & 5 deletions src/clients/db/Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,17 @@ export default class Repository<T extends AbstractDBModel> {
}

const nextPageInfo: { [key: string]: string } = {}
const previousPageInfo: { [key: string]: string } = {}

if (mappedResult.length > 0) {
sort.sorts.forEach((s) => {
nextPageInfo[s.field] = mappedResult[mappedResult.length - 1][s.field]
})
sort.sorts.forEach((s) => {
previousPageInfo[s.field] = mappedResult[0][s.field]
})
}

const previousPageInfo: { [key: string]: string } = {}
sort.sorts.forEach((s) => {
previousPageInfo[s.field] = mappedResult[0][s.field]
})

const hasNext: boolean =
pageRequest.size !== undefined && mappedResult.length === pageRequest.size + 1
const hasPrevious: boolean = pageRequest.number !== undefined && pageRequest.number > 1
Expand Down

0 comments on commit 671ad02

Please sign in to comment.