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

Commit

Permalink
feat(patients): add tests for patients-slice and move files
Browse files Browse the repository at this point in the history
  • Loading branch information
jackcmeyer committed Dec 17, 2019
1 parent a7bf838 commit c2ef440
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 7 deletions.
104 changes: 104 additions & 0 deletions src/__tests__/patients/patients-slice.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import * as patientsSlice from '../../patients/patients-slice'
import { AnyAction } from 'redux'
import { createMemoryHistory } from 'history'
import Patient from '../../model/Patient'
import PatientRepository from '../../clients/db/PatientRepository'
import { mocked } from 'ts-jest/utils'

describe('patients slice', () => {
beforeEach(() => {
jest.resetAllMocks()
})

describe('patients reducer', () => {
it('should create the proper initial state with empty patients array', () => {
const patientsStore = patientsSlice.default(undefined, {} as AnyAction)
expect(patientsStore.isLoading).toBeFalsy()
expect(patientsStore.patients).toHaveLength(0)
})

it('should handle the CREATE_PATIENT_START action', () => {
const patientsStore = patientsSlice.default(undefined, {
type: patientsSlice.createPatientStart.type,
})

expect(patientsStore.isLoading).toBeTruthy()
})

it('should handle the CREATE_PATIENT_SUCCESS actions', () => {
const patientsStore = patientsSlice.default(undefined, {
type: patientsSlice.createPatientSuccess().type,
})

expect(patientsStore.isLoading).toBeFalsy()
})
})

describe('createPatient()', () => {
it('should dispatch the CREATE_PATIENT_START action', async () => {
const dispatch = jest.fn()
const getState = jest.fn()
const expectedPatient = {
id: 'id',
} as Patient

await patientsSlice.createPatient(expectedPatient, createMemoryHistory())(
dispatch,
getState,
null,
)

expect(dispatch).toHaveBeenCalledWith({ type: patientsSlice.createPatientStart.type })
})

it('should call the PatientRepository save method with the correct patient', async () => {
const dispatch = jest.fn()
const getState = jest.fn()
jest.spyOn(PatientRepository, 'save')
const expectedPatient = {
id: 'id',
} as Patient

await patientsSlice.createPatient(expectedPatient, createMemoryHistory())(
dispatch,
getState,
null,
)

expect(PatientRepository.save).toHaveBeenCalledWith(expectedPatient)
})

it('should dispatch the CREATE_PATIENT_SUCCESS action', async () => {
const dispatch = jest.fn()
const getState = jest.fn()
const mockedPatientRepository = mocked(PatientRepository, true)
mockedPatientRepository.save.mockResolvedValue({ id: '12345' } as Patient)
const expectedPatient = {
id: 'id',
} as Patient

await patientsSlice.createPatient(expectedPatient, createMemoryHistory())(
dispatch,
getState,
null,
)

expect(dispatch).toHaveBeenCalledWith({ type: patientsSlice.createPatientSuccess().type })
})

it('should navigate to the /patients/:id where id is the new patient id', async () => {
const expectedPatientId = '12345'
const mockedPatientRepository = mocked(PatientRepository, true)
mockedPatientRepository.save.mockResolvedValue({ id: expectedPatientId } as Patient)
const history = createMemoryHistory()

const dispatch = jest.fn()
const getState = jest.fn()
const expectedPatient = {} as Patient

await patientsSlice.createPatient(expectedPatient, history)(dispatch, getState, null)

expect(history.entries[1].pathname).toEqual(`/patients/${expectedPatientId}`)
})
})
})
2 changes: 1 addition & 1 deletion src/containers/ViewPatient.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { withRouter, useParams } from 'react-router-dom'
import { Spinner } from '@hospitalrun/components'
import { useTranslation } from 'react-i18next'
import useTitle from '../util/useTitle'
import { fetchPatient } from '../slices/patient-slice'
import { fetchPatient } from '../patients/patient-slice'
import { RootState } from '../store'

const ViewPatient = () => {
Expand Down
6 changes: 3 additions & 3 deletions src/patients/patients-slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@ const patientsSlice = createSlice({
state.patients = payload
},
createPatientSuccess(state) {
state.isLoading = true
state.isLoading = false
},
},
})

export const {
getPatientsStart,
getAllPatientsSuccess,
Expand All @@ -44,7 +43,8 @@ export const createPatient = (patient: Patient, history: any): AppThunk => async
try {
dispatch(createPatientStart())
const newPatient = await PatientRepository.save(patient)
history.push(newPatient.id)
dispatch(createPatientSuccess())
history.push(`patients/${newPatient.id}`)
} catch (error) {
console.log(error)
}
Expand Down
2 changes: 1 addition & 1 deletion src/patients/patients/Patients.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Link } from 'react-router-dom'
import { useTranslation } from 'react-i18next'
import { Spinner } from '@hospitalrun/components'
import { RootState } from '../../store'
import { fetchPatients } from '../../slices/patients-slice'
import { fetchPatients } from '../patients-slice'
import useTitle from '../../util/useTitle'

const Patients = () => {
Expand Down
4 changes: 2 additions & 2 deletions src/store/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { configureStore, combineReducers, Action } from '@reduxjs/toolkit'
import ReduxThunk, { ThunkAction } from 'redux-thunk'
import patient from '../slices/patient-slice'
import patients from '../slices/patients-slice'
import patient from '../patients/patient-slice'
import patients from '../patients/patients-slice'
import title from '../slices/title-slice'
import user from '../slices/user-slice'

Expand Down

0 comments on commit c2ef440

Please sign in to comment.