This repository has been archived by the owner on Jan 9, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(patients): add tests for patients-slice and move files
- Loading branch information
1 parent
a7bf838
commit c2ef440
Showing
5 changed files
with
111 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters