From 1919c8d7f5052cd210eba27dc29e19ece832f904 Mon Sep 17 00:00:00 2001 From: Braydon Hall <40751395+nobrayner@users.noreply.github.com> Date: Sun, 27 Dec 2020 08:06:54 +1100 Subject: [PATCH 1/2] fix(test): fix view appointments --- .../appointments/ViewAppointments.test.tsx | 108 ++++++++++-------- 1 file changed, 59 insertions(+), 49 deletions(-) diff --git a/src/__tests__/scheduling/appointments/ViewAppointments.test.tsx b/src/__tests__/scheduling/appointments/ViewAppointments.test.tsx index 6ab9f81b4c..08c3d2f74f 100644 --- a/src/__tests__/scheduling/appointments/ViewAppointments.test.tsx +++ b/src/__tests__/scheduling/appointments/ViewAppointments.test.tsx @@ -1,6 +1,5 @@ -import { Calendar } from '@hospitalrun/components' -import { act } from '@testing-library/react' -import { mount } from 'enzyme' +import { render, waitFor, screen } from '@testing-library/react' +import addMinutes from 'date-fns/addMinutes' import React from 'react' import { Provider } from 'react-redux' import { MemoryRouter } from 'react-router-dom' @@ -18,77 +17,88 @@ import { RootState } from '../../../shared/store' const { TitleProvider } = titleUtil -describe('ViewAppointments', () => { - const expectedAppointments = [ - { - id: '123', - rev: '1', - patient: '1234', - startDateTime: new Date().toISOString(), - endDateTime: new Date().toISOString(), - location: 'location', - reason: 'reason', - }, - ] as Appointment[] +beforeEach(() => { + jest.clearAllMocks() +}) + +const setup = () => { + const expectedAppointment = { + id: '123', + rev: '1', + patient: '1234', + startDateTime: new Date().toISOString(), + endDateTime: addMinutes(new Date(), 60).toISOString(), + location: 'location', + reason: 'reason', + } as Appointment const expectedPatient = { id: '123', fullName: 'patient full name', } as Patient + jest.spyOn(titleUtil, 'useUpdateTitle').mockReturnValue(jest.fn()) + jest.spyOn(ButtonBarProvider, 'useButtonToolbarSetter').mockImplementation(() => jest.fn()) + jest.spyOn(AppointmentRepository, 'findAll').mockResolvedValue([expectedAppointment]) + jest.spyOn(PatientRepository, 'find').mockResolvedValue(expectedPatient) + + const mockStore = createMockStore([thunk]) - const setup = async () => { - jest.spyOn(titleUtil, 'useUpdateTitle').mockImplementation(() => jest.fn()) - jest.spyOn(AppointmentRepository, 'findAll').mockResolvedValue(expectedAppointments) - jest.spyOn(PatientRepository, 'find').mockResolvedValue(expectedPatient) - const mockStore = createMockStore([thunk]) - return mount( - + return { + expectedPatient, + expectedAppointment, + ...render( + , - ) + ), } +} +describe('ViewAppointments', () => { it('should have called the useUpdateTitle hook', async () => { - await act(async () => { - await setup() + setup() + + await waitFor(() => { + expect(titleUtil.useUpdateTitle).toHaveBeenCalled() }) - expect(titleUtil.useUpdateTitle).toHaveBeenCalled() }) it('should add a "New Appointment" button to the button tool bar', async () => { - const setButtonToolBarSpy = jest.fn() - jest.spyOn(ButtonBarProvider, 'useButtonToolbarSetter').mockReturnValue(setButtonToolBarSpy) + setup() - await act(async () => { - await setup() + await waitFor(() => { + expect(ButtonBarProvider.useButtonToolbarSetter).toHaveBeenCalled() }) - - const actualButtons: React.ReactNode[] = setButtonToolBarSpy.mock.calls[0][0] - expect((actualButtons[0] as any).props.children).toEqual('scheduling.appointments.new') }) it('should render a calendar with the proper events', async () => { - let wrapper: any - await act(async () => { - wrapper = await setup() + const { container, expectedPatient, expectedAppointment } = setup() + + await waitFor(() => { + expect(screen.getByText(expectedPatient.fullName as string)).toBeInTheDocument() }) - wrapper.update() - const expectedEvents = [ - { - id: expectedAppointments[0].id, - start: new Date(expectedAppointments[0].startDateTime), - end: new Date(expectedAppointments[0].endDateTime), - title: 'patient full name', - allDay: false, - }, - ] + const expectedAppointmentStartDate = new Date(expectedAppointment.startDateTime) + const expectedStart = `${expectedAppointmentStartDate.getHours()}:${expectedAppointmentStartDate + .getMinutes() + .toString() + .padStart(2, '0')}` + const expectedAppointmentEndDate = new Date(expectedAppointment.endDateTime) + const expectedEnd = `${expectedAppointmentEndDate.getHours()}:${expectedAppointmentEndDate + .getMinutes() + .toString() + .padStart(2, '0')}` - const calendar = wrapper.find(Calendar) - expect(calendar).toHaveLength(1) - expect(calendar.prop('events')).toEqual(expectedEvents) + expect(container.querySelector('.fc-content-col .fc-time')).toHaveAttribute( + 'data-full', + expect.stringContaining(expectedStart), + ) + expect(container.querySelector('.fc-content-col .fc-time')).toHaveAttribute( + 'data-full', + expect.stringContaining(expectedEnd), + ) }) }) From 2208d2dfea438fb1a6659f5daad55bc98f8677e3 Mon Sep 17 00:00:00 2001 From: Braydon Hall <40751395+nobrayner@users.noreply.github.com> Date: Sun, 27 Dec 2020 08:38:24 +1100 Subject: [PATCH 2/2] fix(test): view appointments bad time format --- .../appointments/ViewAppointments.test.tsx | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/__tests__/scheduling/appointments/ViewAppointments.test.tsx b/src/__tests__/scheduling/appointments/ViewAppointments.test.tsx index 08c3d2f74f..a2978ff194 100644 --- a/src/__tests__/scheduling/appointments/ViewAppointments.test.tsx +++ b/src/__tests__/scheduling/appointments/ViewAppointments.test.tsx @@ -1,5 +1,6 @@ import { render, waitFor, screen } from '@testing-library/react' import addMinutes from 'date-fns/addMinutes' +import format from 'date-fns/format' import React from 'react' import { Provider } from 'react-redux' import { MemoryRouter } from 'react-router-dom' @@ -81,16 +82,8 @@ describe('ViewAppointments', () => { expect(screen.getByText(expectedPatient.fullName as string)).toBeInTheDocument() }) - const expectedAppointmentStartDate = new Date(expectedAppointment.startDateTime) - const expectedStart = `${expectedAppointmentStartDate.getHours()}:${expectedAppointmentStartDate - .getMinutes() - .toString() - .padStart(2, '0')}` - const expectedAppointmentEndDate = new Date(expectedAppointment.endDateTime) - const expectedEnd = `${expectedAppointmentEndDate.getHours()}:${expectedAppointmentEndDate - .getMinutes() - .toString() - .padStart(2, '0')}` + const expectedStart = format(new Date(expectedAppointment.startDateTime), 'h:mm') + const expectedEnd = format(new Date(expectedAppointment.endDateTime), 'h:mm') expect(container.querySelector('.fc-content-col .fc-time')).toHaveAttribute( 'data-full',