Skip to content

Commit

Permalink
Merge branch 'master' into feat-HospitalRun#1969
Browse files Browse the repository at this point in the history
  • Loading branch information
matteovivona committed Jun 5, 2020
2 parents e291484 + b0ba451 commit 5eef660
Show file tree
Hide file tree
Showing 21 changed files with 1,404 additions and 28 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"@commitlint/core": "~8.3.5",
"@commitlint/prompt": "~8.3.5",
"@testing-library/react": "~10.1.0",
"@testing-library/react-hooks": "~3.2.1",
"@testing-library/react-hooks": "~3.3.0",
"@types/enzyme": "^3.10.5",
"@types/jest": "~25.2.0",
"@types/lodash": "^4.14.150",
Expand Down
120 changes: 120 additions & 0 deletions src/__tests__/patients/care-plans/AddCarePlanModal.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import '../../../__mocks__/matchMediaMock'
import { Modal } from '@hospitalrun/components'
import { mount } from 'enzyme'
import { createMemoryHistory } from 'history'
import React from 'react'
import { act } from 'react-dom/test-utils'
import { Provider } from 'react-redux'
import { Router } from 'react-router-dom'
import createMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'

import { CarePlanIntent, CarePlanStatus } from '../../../model/CarePlan'
import Patient from '../../../model/Patient'
import AddCarePlanModal from '../../../patients/care-plans/AddCarePlanModal'
import CarePlanForm from '../../../patients/care-plans/CarePlanForm'
import * as patientSlice from '../../../patients/patient-slice'
import { RootState } from '../../../store'

const mockStore = createMockStore<RootState, any>([thunk])

describe('Add Care Plan Modal', () => {
const patient = {
id: 'patientId',
diagnoses: [{ id: '123', name: 'some name', diagnosisDate: new Date().toISOString() }],
carePlans: [
{
id: '123',
title: 'some title',
description: 'some description',
diagnosisId: '123',
startDate: new Date().toISOString(),
endDate: new Date().toISOString(),
status: CarePlanStatus.Active,
intent: CarePlanIntent.Proposal,
},
],
} as Patient

const carePlanError = {
title: 'some care plan error',
}

const onCloseSpy = jest.fn()
const setup = () => {
const store = mockStore({ patient: { patient, carePlanError } } as any)
const history = createMemoryHistory()
const wrapper = mount(
<Provider store={store}>
<Router history={history}>
<AddCarePlanModal show onCloseButtonClick={onCloseSpy} />
</Router>
</Provider>,
)

wrapper.update()
return { wrapper }
}

it('should render a modal', () => {
const { wrapper } = setup()

const modal = wrapper.find(Modal)

expect(modal).toHaveLength(1)

const successButton = modal.prop('successButton')
const cancelButton = modal.prop('closeButton')
expect(modal.prop('title')).toEqual('patient.carePlan.new')
expect(successButton?.children).toEqual('patient.carePlan.new')
expect(successButton?.icon).toEqual('add')
expect(cancelButton?.children).toEqual('actions.cancel')
})

it('should render the care plan form', () => {
const { wrapper } = setup()

const carePlanForm = wrapper.find(CarePlanForm)
expect(carePlanForm).toHaveLength(1)
expect(carePlanForm.prop('carePlanError')).toEqual(carePlanError)
expect(carePlanForm.prop('patient')).toEqual(patient)
})

it('should dispatch add care plan when the save button is clicked', async () => {
const { wrapper } = setup()
jest.spyOn(patientSlice, 'addCarePlan')

act(() => {
const carePlanForm = wrapper.find(CarePlanForm)
const onChange = carePlanForm.prop('onChange') as any
onChange(patient.carePlans[0])
})
wrapper.update()

await act(async () => {
const modal = wrapper.find(Modal)
const successButton = modal.prop('successButton')
const onClick = successButton?.onClick as any
await onClick()
})

expect(patientSlice.addCarePlan).toHaveBeenCalledTimes(1)
expect(patientSlice.addCarePlan).toHaveBeenCalledWith(patient.id, patient.carePlans[0])
})

it('should call the on close function when the cancel button is clicked', () => {
const { wrapper } = setup()

const modal = wrapper.find(Modal)

expect(modal).toHaveLength(1)

act(() => {
const cancelButton = modal.prop('closeButton')
const onClick = cancelButton?.onClick as any
onClick()
})

expect(onCloseSpy).toHaveBeenCalledTimes(1)
})
})
Loading

0 comments on commit 5eef660

Please sign in to comment.