Skip to content

Commit

Permalink
test(patient-slice): fix failing test cases after adding pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
akshay-ap committed Apr 27, 2020
1 parent f500d95 commit 81f9ce7
Showing 1 changed file with 33 additions and 13 deletions.
46 changes: 33 additions & 13 deletions src/__tests__/patients/patients-slice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import '../../__mocks__/matchMediaMock'
import { AnyAction } from 'redux'
import { mocked } from 'ts-jest/utils'
import { UnpagedRequest } from 'clients/db/PageRequest'
import Page from 'clients/Page'
import { Unsorted } from 'clients/db/SortRequest'
import patients, {
fetchPatientsStart,
fetchPatientsSuccess,
Expand Down Expand Up @@ -67,38 +69,56 @@ describe('patients slice', () => {
it('should call the PatientRepository search method with the correct search criteria', async () => {
const dispatch = jest.fn()
const getState = jest.fn()
jest.spyOn(PatientRepository, 'search')
jest.spyOn(PatientRepository, 'searchPaged')

const expectedSearchString = 'search string'
await searchPatients(expectedSearchString)(dispatch, getState, null)

expect(PatientRepository.search).toHaveBeenCalledWith(expectedSearchString)
expect(PatientRepository.searchPaged).toHaveBeenCalledWith(
expectedSearchString,
UnpagedRequest,
)
})

it('should call the PatientRepository findAll method if there is no string text', async () => {
it('should call the PatientRepository findAllPaged method if there is no string text', async () => {
const dispatch = jest.fn()
const getState = jest.fn()
jest.spyOn(PatientRepository, 'findAll')
jest.spyOn(PatientRepository, 'findAllPaged')

await searchPatients('')(dispatch, getState, null)

expect(PatientRepository.findAll).toHaveBeenCalledTimes(1)
expect(PatientRepository.findAllPaged).toHaveBeenCalledTimes(1)
})

it('should dispatch the FETCH_PATIENTS_SUCCESS action', async () => {
const dispatch = jest.fn()
const getState = jest.fn()

const expectedPatients = [
{
id: '1234',
},
] as Patient[]
const expectedPatients = {
content: [
{
id: '123',
fullName: 'test test',
isApproximateDateOfBirth: false,
givenName: 'test',
familyName: 'test',
code: 'P12345',
sex: 'male',
dateOfBirth: new Date().toISOString(),
phoneNumber: '99999999',
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
rev: '',
},
],
hasNext: false,
hasPrevious: false,
pageRequest: UnpagedRequest,
} as Page<Patient>

const mockedPatientRepository = mocked(PatientRepository, true)
mockedPatientRepository.search.mockResolvedValue(expectedPatients)
mockedPatientRepository.searchPaged.mockResolvedValue(expectedPatients)

await searchPatients('search string')(dispatch, getState, null)
await searchPatients('search string', Unsorted)(dispatch, getState, null)

expect(dispatch).toHaveBeenLastCalledWith({
type: fetchPatientsSuccess.type,
Expand Down

0 comments on commit 81f9ce7

Please sign in to comment.