From 32e00f63a75dedca485ebbdbdd9bf6f279512f08 Mon Sep 17 00:00:00 2001 From: Samuel Gaudreault Date: Tue, 5 May 2020 17:22:13 -0400 Subject: [PATCH] test(appointments): add missing tests for AppointmentsList --- .../appointments/AppointmentsList.test.tsx | 54 ++++++++++++++++--- 1 file changed, 46 insertions(+), 8 deletions(-) diff --git a/src/__tests__/patients/appointments/AppointmentsList.test.tsx b/src/__tests__/patients/appointments/AppointmentsList.test.tsx index 55697d7f1b..249ce68023 100644 --- a/src/__tests__/patients/appointments/AppointmentsList.test.tsx +++ b/src/__tests__/patients/appointments/AppointmentsList.test.tsx @@ -1,6 +1,6 @@ import '../../../__mocks__/matchMediaMock' import React from 'react' -import { mount } from 'enzyme' +import { mount, ReactWrapper } from 'enzyme' import { createMemoryHistory } from 'history' import configureMockStore from 'redux-mock-store' import thunk from 'redux-thunk' @@ -10,20 +10,30 @@ import { Provider } from 'react-redux' import AppointmentsList from 'patients/appointments/AppointmentsList' import * as components from '@hospitalrun/components' import { act } from 'react-dom/test-utils' -// import PatientRepository from 'clients/db/PatientRepository' # Lint warning: 'PatientRepository' is defined but never used +import * as appointmentsSlice from '../../../scheduling/appointments/appointments-slice' const expectedPatient = { id: '123', } as Patient + const expectedAppointments = [ + { + id: '456', + rev: '1', + patientId: '1234', + startDateTime: new Date(2020, 1, 1, 9, 0, 0, 0).toISOString(), + endDateTime: new Date(2020, 1, 1, 9, 30, 0, 0).toISOString(), + location: 'location', + reason: 'Follow Up', + }, { id: '123', rev: '1', patientId: '1234', - startDateTime: new Date().toISOString(), - endDateTime: new Date().toISOString(), + startDateTime: new Date(2020, 1, 1, 8, 0, 0, 0).toISOString(), + endDateTime: new Date(2020, 1, 1, 8, 30, 0, 0).toISOString(), location: 'location', - reason: 'reason', + reason: 'Checkup', }, ] @@ -41,12 +51,40 @@ const setup = (patient = expectedPatient, appointments = expectedAppointments) = , ) - return wrapper } describe('AppointmentsList', () => { - describe('add new appointment button', () => { + it('should render a list of appointments', () => { + const wrapper = setup() + const listItems: ReactWrapper = wrapper.find(components.ListItem) + + expect(listItems.length === 2).toBeTruthy() + expect(listItems.at(0).text()).toEqual( + new Date(expectedAppointments[0].startDateTime).toLocaleString(), + ) + expect(listItems.at(1).text()).toEqual( + new Date(expectedAppointments[1].startDateTime).toLocaleString(), + ) + }) + + it('should search for "ch" in the list', () => { + jest.spyOn(appointmentsSlice, 'fetchPatientAppointments') + const searchText = 'ch' + const wrapper = setup() + + const searchInput: ReactWrapper = wrapper.find('input').at(0) + searchInput.simulate('change', { target: { value: searchText } }) + + wrapper.find('button').at(1).simulate('click') + + expect(appointmentsSlice.fetchPatientAppointments).toHaveBeenCalledWith( + expectedPatient.id, + searchText, + ) + }) + + describe('New appointment button', () => { it('should render a new appointment button', () => { const wrapper = setup() @@ -59,7 +97,7 @@ describe('AppointmentsList', () => { const wrapper = setup() act(() => { - wrapper.find(components.Button).at(0).prop('onClick')() + wrapper.find(components.Button).at(0).simulate('click') }) wrapper.update()