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

Commit

Permalink
feat(appointments): add create appointment functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
jackcmeyer committed Jan 26, 2020
1 parent 86c9a32 commit 723bec3
Show file tree
Hide file tree
Showing 9 changed files with 721 additions and 12 deletions.
14 changes: 14 additions & 0 deletions src/__tests__/clients/db/AppointmentRepository.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
import AppointmentRepository from 'clients/db/AppointmentsRepository'
import { appointments } from 'config/pouchdb'
import Appointment from 'model/Appointment'
import { fromUnixTime } from 'date-fns'

describe('Appointment Repository', () => {
it('should create a repository with the database set to the appointments database', () => {
expect(AppointmentRepository.db).toEqual(appointments)
})

describe('save', () => {
it('should create an id that is a timestamp', async () => {
const newAppointment = await AppointmentRepository.save({
patientId: 'id',
} as Appointment)

expect(fromUnixTime(parseInt(newAppointment.id, 10)).getTime() > 0).toBeTruthy()

await appointments.remove(await appointments.get(newAppointment.id))
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import '../../../__mocks__/matchMediaMock'
import React, { ChangeEvent } from 'react'
import { DateTimePicker, Label } from '@hospitalrun/components'
import { shallow } from 'enzyme'
import DateTimePickerWithLabelFormGroup from '../../../components/input/DateTimePickerWithLabelFormGroup'

describe('date picker with label form group', () => {
describe('layout', () => {
it('should render a label', () => {
const expectedName = 'test'
const wrapper = shallow(
<DateTimePickerWithLabelFormGroup
name={expectedName}
label="test"
value={new Date()}
isEditable
onChange={jest.fn()}
/>,
)

const label = wrapper.find(Label)
expect(label).toHaveLength(1)
expect(label.prop('htmlFor')).toEqual(`${expectedName}DateTimePicker`)
expect(label.prop('text')).toEqual(expectedName)
})

it('should render and date time picker', () => {
const expectedName = 'test'
const wrapper = shallow(
<DateTimePickerWithLabelFormGroup
name={expectedName}
label="test"
value={new Date()}
isEditable
onChange={jest.fn()}
/>,
)

const input = wrapper.find(DateTimePicker)
expect(input).toHaveLength(1)
})

it('should render disabled is isDisable disabled is true', () => {
const expectedName = 'test'
const wrapper = shallow(
<DateTimePickerWithLabelFormGroup
name={expectedName}
label="test"
value={new Date()}
isEditable={false}
onChange={jest.fn()}
/>,
)

const input = wrapper.find(DateTimePicker)
expect(input).toHaveLength(1)
expect(input.prop('disabled')).toBeTruthy()
})

it('should render the proper value', () => {
const expectedName = 'test'
const expectedValue = new Date()
const wrapper = shallow(
<DateTimePickerWithLabelFormGroup
name={expectedName}
label="test"
value={expectedValue}
isEditable={false}
onChange={jest.fn()}
/>,
)

const input = wrapper.find(DateTimePicker)
expect(input).toHaveLength(1)
expect(input.prop('selected')).toEqual(expectedValue)
})
})

describe('change handler', () => {
it('should call the change handler on change', () => {
const expectedName = 'test'
const expectedValue = new Date()
const handler = jest.fn()
const wrapper = shallow(
<DateTimePickerWithLabelFormGroup
name={expectedName}
label="test"
value={expectedValue}
isEditable={false}
onChange={handler}
/>,
)

const input = wrapper.find(DateTimePicker)
input.prop('onChange')(new Date(), {
target: { value: new Date().toISOString() },
} as ChangeEvent<HTMLInputElement>)
expect(handler).toHaveBeenCalledTimes(1)
})
})
})
89 changes: 89 additions & 0 deletions src/__tests__/scheduling/appointments-slice.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { AnyAction } from 'redux'
import Appointment from 'model/Appointment'
import { createMemoryHistory } from 'history'
import AppointmentRepository from 'clients/db/AppointmentsRepository'
import appointments, {
createAppointmentStart,
createAppointment,
} from '../../scheduling/appointments/appointments-slice'
import { mocked } from 'ts-jest/utils'

describe('appointments slice', () => {
describe('appointments reducer', () => {
it('should create the initial state properly', () => {
const appointmentsStore = appointments(undefined, {} as AnyAction)

expect(appointmentsStore.isLoading).toBeFalsy()
})
it('should handle the CREATE_APPOINTMENT_START action', () => {
const appointmentsStore = appointments(undefined, {
type: createAppointmentStart.type,
})

expect(appointmentsStore.isLoading).toBeTruthy()
})
})

describe('createAppointments()', () => {
it('should dispatch the CREATE_APPOINTMENT_START action', async () => {
jest.spyOn(AppointmentRepository, 'save')
mocked(AppointmentRepository, true).save.mockResolvedValue({ id: '123' } as Appointment)
const dispatch = jest.fn()
const getState = jest.fn()
const expectedAppointment = {
patientId: '123',
startDateTime: new Date().toISOString(),
endDateTime: new Date().toISOString(),
location: 'location',
type: 'type',
reason: 'reason',
} as Appointment

await createAppointment(expectedAppointment, createMemoryHistory())(dispatch, getState, null)

expect(dispatch).toHaveBeenCalledWith({ type: createAppointmentStart.type })
})

it('should call the the AppointmentRepository save function with the correct data', async () => {
const dispatch = jest.fn()
const getState = jest.fn()
const appointmentRepositorySaveSpy = jest.spyOn(AppointmentRepository, 'save')
mocked(AppointmentRepository, true).save.mockResolvedValue({ id: '123' } as Appointment)

const expectedAppointment = {
patientId: '123',
startDateTime: new Date().toISOString(),
endDateTime: new Date().toISOString(),
location: 'location',
type: 'type',
reason: 'reason',
} as Appointment

await createAppointment(expectedAppointment, createMemoryHistory())(dispatch, getState, null)

expect(appointmentRepositorySaveSpy).toHaveBeenCalled()
expect(appointmentRepositorySaveSpy).toHaveBeenCalledWith(expectedAppointment)
})

it('should navigate the /appointments when an appointment is successfully created', async () => {
jest.spyOn(AppointmentRepository, 'save')
mocked(AppointmentRepository, true).save.mockResolvedValue({ id: '123' } as Appointment)
const dispatch = jest.fn()
const getState = jest.fn()
const history = createMemoryHistory()

const expectedAppointment = {
patientId: '123',
startDateTime: new Date().toISOString(),
endDateTime: new Date().toISOString(),
location: 'location',
type: 'type',
reason: 'reason',
} as Appointment

await createAppointment(expectedAppointment, history)(dispatch, getState, null)

expect(history.location.pathname).toEqual('/appointments')
})
})
})
Loading

0 comments on commit 723bec3

Please sign in to comment.