From 671ad02d6992727f73c777f673a6c305fd57b1b2 Mon Sep 17 00:00:00 2001 From: Akshay Patel Date: Wed, 20 May 2020 18:02:14 +0530 Subject: [PATCH] feat(viewpatients): add Tests, Fix bug feat #1969 --- .../components/PageComponent.test.tsx | 31 +++++++++++++ src/__tests__/hooks/useUpdateEffect.test.ts | 15 +++++++ .../patients/list/ViewPatients.test.tsx | 44 ++++++++++++++++++- src/clients/db/Repository.ts | 10 ++--- 4 files changed, 94 insertions(+), 6 deletions(-) create mode 100644 src/__tests__/components/PageComponent.test.tsx create mode 100644 src/__tests__/hooks/useUpdateEffect.test.ts diff --git a/src/__tests__/components/PageComponent.test.tsx b/src/__tests__/components/PageComponent.test.tsx new file mode 100644 index 0000000000..fa1bf5a4ef --- /dev/null +++ b/src/__tests__/components/PageComponent.test.tsx @@ -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( + , + ) + 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) + }) +}) diff --git a/src/__tests__/hooks/useUpdateEffect.test.ts b/src/__tests__/hooks/useUpdateEffect.test.ts new file mode 100644 index 0000000000..06b044f946 --- /dev/null +++ b/src/__tests__/hooks/useUpdateEffect.test.ts @@ -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) + }) +}) diff --git a/src/__tests__/patients/list/ViewPatients.test.tsx b/src/__tests__/patients/list/ViewPatients.test.tsx index cd5a8375dc..0e730dadb7 100644 --- a/src/__tests__/patients/list/ViewPatients.test.tsx +++ b/src/__tests__/patients/list/ViewPatients.test.tsx @@ -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' @@ -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' @@ -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) + }) + + 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()) diff --git a/src/clients/db/Repository.ts b/src/clients/db/Repository.ts index ff7d27175f..7a82d2631b 100644 --- a/src/clients/db/Repository.ts +++ b/src/clients/db/Repository.ts @@ -98,17 +98,17 @@ export default class Repository { } 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