From f12b4dbb02bd638fbb75b83b307455dc1f50d0a9 Mon Sep 17 00:00:00 2001 From: Braydon Hall <40751395+nobrayner@users.noreply.github.com> Date: Fri, 8 Jan 2021 09:21:47 +1100 Subject: [PATCH 1/7] fix(test): can create care goal --- .../care-goals/AddCareGoalModal.test.tsx | 51 ++------------ .../patients/care-goals/CareGoalForm.test.tsx | 6 +- .../patients/care-goals/CareGoalTab.test.tsx | 70 ++++++++++++++++--- 3 files changed, 69 insertions(+), 58 deletions(-) diff --git a/src/__tests__/patients/care-goals/AddCareGoalModal.test.tsx b/src/__tests__/patients/care-goals/AddCareGoalModal.test.tsx index 2d71c46d0f..32cf77a205 100644 --- a/src/__tests__/patients/care-goals/AddCareGoalModal.test.tsx +++ b/src/__tests__/patients/care-goals/AddCareGoalModal.test.tsx @@ -1,11 +1,9 @@ -import { render, screen, waitFor } from '@testing-library/react' -import userEvent from '@testing-library/user-event' +import { render, screen } from '@testing-library/react' import { createMemoryHistory } from 'history' import React from 'react' import { Router } from 'react-router-dom' import AddCareGoalModal from '../../../patients/care-goals/AddCareGoalModal' -import PatientRepository from '../../../shared/db/PatientRepository' import CareGoal from '../../../shared/model/CareGoal' import Patient from '../../../shared/model/Patient' @@ -17,25 +15,15 @@ describe('Add Care Goal Modal', () => { } as Patient const setup = () => { - const onCloseSpy = jest.fn() - jest.spyOn(PatientRepository, 'find').mockResolvedValue(patient) - jest.spyOn(PatientRepository, 'saveOrUpdate') const history = createMemoryHistory() - return { - ...render( - - - , - ), - onCloseSpy, - } + return render( + + + , + ) } - beforeEach(() => { - jest.resetAllMocks() - }) - it('should render a modal', () => { setup() @@ -51,31 +39,4 @@ describe('Add Care Goal Modal', () => { expect(screen.getByLabelText('care-goal-form')).toBeInTheDocument() }) - - it('should save care goal when save button is clicked and close', async () => { - const expectedCreatedDate = new Date() - Date.now = jest.fn().mockReturnValue(expectedCreatedDate) - - const expectedCareGoal = { - description: 'some description', - createdOn: expectedCreatedDate.toISOString(), - } - - const { onCloseSpy } = setup() - - userEvent.type(screen.getAllByRole('textbox')[0], expectedCareGoal.description) - userEvent.click(screen.getByRole('button', { name: /patient.careGoal.new/i })) - - await waitFor(() => { - expect(PatientRepository.saveOrUpdate).toHaveBeenCalledTimes(1) - }) - - expect(PatientRepository.saveOrUpdate).toHaveBeenCalledWith( - expect.objectContaining({ - careGoals: expect.arrayContaining([expect.objectContaining(expectedCareGoal)]), - }), - ) - - expect(onCloseSpy).toHaveBeenCalledTimes(1) - }) }) diff --git a/src/__tests__/patients/care-goals/CareGoalForm.test.tsx b/src/__tests__/patients/care-goals/CareGoalForm.test.tsx index b40995d784..bbc809f7eb 100644 --- a/src/__tests__/patients/care-goals/CareGoalForm.test.tsx +++ b/src/__tests__/patients/care-goals/CareGoalForm.test.tsx @@ -23,9 +23,12 @@ const careGoal = { const setup = (disabled = false, initializeCareGoal = true, error?: any) => { const onCareGoalChangeSpy = jest.fn() + const TestComponent = () => { const [careGoal2, setCareGoal] = React.useState(initializeCareGoal ? careGoal : {}) + onCareGoalChangeSpy.mockImplementation(setCareGoal) + return ( { /> ) } - const wrapper = render() - return { ...wrapper, onCareGoalChangeSpy } + return { ...render(), onCareGoalChangeSpy } } describe('Care Goal Form', () => { diff --git a/src/__tests__/patients/care-goals/CareGoalTab.test.tsx b/src/__tests__/patients/care-goals/CareGoalTab.test.tsx index 51013a17da..190d2d8a36 100644 --- a/src/__tests__/patients/care-goals/CareGoalTab.test.tsx +++ b/src/__tests__/patients/care-goals/CareGoalTab.test.tsx @@ -1,5 +1,6 @@ -import { render, screen, waitFor, waitForElementToBeRemoved } from '@testing-library/react' -import userEvent from '@testing-library/user-event' +import { render, screen, waitFor, waitForElementToBeRemoved, within } from '@testing-library/react' +import userEvent, { specialChars } from '@testing-library/user-event' +import format from 'date-fns/format' import { createMemoryHistory, MemoryHistory } from 'history' import React from 'react' import { Provider } from 'react-redux' @@ -9,12 +10,13 @@ import thunk from 'redux-thunk' import CareGoalTab from '../../../patients/care-goals/CareGoalTab' import PatientRepository from '../../../shared/db/PatientRepository' -import CareGoal from '../../../shared/model/CareGoal' +import CareGoal, { CareGoalStatus } from '../../../shared/model/CareGoal' import Patient from '../../../shared/model/Patient' import Permissions from '../../../shared/model/Permissions' import { RootState } from '../../../shared/store' const mockStore = createMockStore([thunk]) +const { selectAll, arrowDown, enter } = specialChars type CareGoalTabWrapper = (store: any, history: MemoryHistory) => React.FC @@ -36,7 +38,12 @@ const ViewWrapper: CareGoalTabWrapper = (store: any, history: MemoryHistory) => ) -const setup = (route: string, permissions: Permissions[], wrapper = TabWrapper) => { +const setup = ( + route: string, + permissions: Permissions[], + wrapper = TabWrapper, + includeCareGoal = true, +) => { const expectedCareGoal = { id: '456', status: 'accepted', @@ -48,7 +55,10 @@ const setup = (route: string, permissions: Permissions[], wrapper = TabWrapper) createdOn: new Date().toISOString(), note: '', } as CareGoal - const expectedPatient = { id: '123', careGoals: [expectedCareGoal] } as Patient + const expectedPatient = { + id: '123', + careGoals: includeCareGoal ? [expectedCareGoal] : [], + } as Patient jest.spyOn(PatientRepository, 'find').mockResolvedValue(expectedPatient) const history = createMemoryHistory({ initialEntries: [route] }) @@ -58,12 +68,6 @@ const setup = (route: string, permissions: Permissions[], wrapper = TabWrapper) } describe('Care Goals Tab', () => { - it('should render add care goal button if user has correct permissions', async () => { - setup('/patients/123/care-goals', [Permissions.AddCareGoal]) - - expect(await screen.findByRole('button', { name: /patient.careGoal.new/i })).toBeInTheDocument() - }) - it('should not render add care goal button if user does not have permissions', async () => { const { container } = setup('/patients/123/care-goals', []) @@ -72,6 +76,50 @@ describe('Care Goals Tab', () => { expect(screen.queryByRole('button', { name: /patient.careGoal.new/i })).not.toBeInTheDocument() }) + it('should be able to create a new care goal if user has permissions', async () => { + const expectedCareGoal = { + description: 'some description', + status: CareGoalStatus.Accepted, + startDate: new Date('2020-01-01'), + dueDate: new Date('2020-02-01'), + } + + setup('/patients/123/care-goals', [Permissions.AddCareGoal], TabWrapper, false) + + userEvent.click(await screen.findByRole('button', { name: /patient.careGoal.new/i })) + + const modal = await screen.findByRole('dialog') + + userEvent.type(within(modal).getAllByRole('textbox')[0], expectedCareGoal.description) + userEvent.type( + within(modal).getAllByRole('combobox')[1], + `${selectAll}${expectedCareGoal.status}${arrowDown}${enter}`, + ) + userEvent.type( + within(modal).getAllByRole('textbox')[4], + `${selectAll}${format(expectedCareGoal.startDate, 'MM/dd/yyyy')}${enter}`, + ) + userEvent.type( + within(modal).getAllByRole('textbox')[5], + `${selectAll}${format(expectedCareGoal.dueDate, 'MM/dd/yyyy')}${enter}`, + ) + + userEvent.click(within(modal).getByRole('button', { name: /patient.careGoal.new/i })) + + await waitForElementToBeRemoved(modal) + + expect( + await screen.findByRole('cell', { name: expectedCareGoal.description }), + ).toBeInTheDocument() + expect(await screen.findByRole('cell', { name: expectedCareGoal.status })).toBeInTheDocument() + expect( + await screen.findByRole('cell', { name: format(expectedCareGoal.startDate, 'yyyy-MM-dd') }), + ).toBeInTheDocument() + expect( + await screen.findByRole('cell', { name: format(expectedCareGoal.dueDate, 'yyyy-MM-dd') }), + ).toBeInTheDocument() + }, 20000) + it('should open and close the modal when the add care goal and close buttons are clicked', async () => { setup('/patients/123/care-goals', [Permissions.AddCareGoal]) From 467f063d54aeb68ce87dd5e56c862d70d7ff9d7c Mon Sep 17 00:00:00 2001 From: Braydon Hall <40751395+nobrayner@users.noreply.github.com> Date: Fri, 8 Jan 2021 09:47:37 +1100 Subject: [PATCH 2/7] fix(test): increase timeout --- src/__tests__/patients/care-goals/CareGoalTab.test.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/__tests__/patients/care-goals/CareGoalTab.test.tsx b/src/__tests__/patients/care-goals/CareGoalTab.test.tsx index 190d2d8a36..c47a6d9e13 100644 --- a/src/__tests__/patients/care-goals/CareGoalTab.test.tsx +++ b/src/__tests__/patients/care-goals/CareGoalTab.test.tsx @@ -118,7 +118,7 @@ describe('Care Goals Tab', () => { expect( await screen.findByRole('cell', { name: format(expectedCareGoal.dueDate, 'yyyy-MM-dd') }), ).toBeInTheDocument() - }, 20000) + }, 30000) it('should open and close the modal when the add care goal and close buttons are clicked', async () => { setup('/patients/123/care-goals', [Permissions.AddCareGoal]) From a9acd72d9278aad68f75deaa84973453c23408f8 Mon Sep 17 00:00:00 2001 From: Braydon Hall <40751395+nobrayner@users.noreply.github.com> Date: Fri, 8 Jan 2021 10:55:48 +1100 Subject: [PATCH 3/7] fix(test): better wait for assertion --- src/__tests__/patients/care-goals/CareGoalTab.test.tsx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/__tests__/patients/care-goals/CareGoalTab.test.tsx b/src/__tests__/patients/care-goals/CareGoalTab.test.tsx index c47a6d9e13..1a7188e518 100644 --- a/src/__tests__/patients/care-goals/CareGoalTab.test.tsx +++ b/src/__tests__/patients/care-goals/CareGoalTab.test.tsx @@ -106,7 +106,9 @@ describe('Care Goals Tab', () => { userEvent.click(within(modal).getByRole('button', { name: /patient.careGoal.new/i })) - await waitForElementToBeRemoved(modal) + await waitFor(() => { + expect(screen.queryByRole('dialog')).not.toBeInTheDocument() + }) expect( await screen.findByRole('cell', { name: expectedCareGoal.description }), From 3115437f622237c94783aeacb79543d7fac75199 Mon Sep 17 00:00:00 2001 From: Braydon Hall <40751395+nobrayner@users.noreply.github.com> Date: Fri, 8 Jan 2021 13:17:04 +1100 Subject: [PATCH 4/7] fix(test): increase waitFor timeout --- src/__tests__/patients/care-goals/CareGoalTab.test.tsx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/__tests__/patients/care-goals/CareGoalTab.test.tsx b/src/__tests__/patients/care-goals/CareGoalTab.test.tsx index 1a7188e518..d038a66954 100644 --- a/src/__tests__/patients/care-goals/CareGoalTab.test.tsx +++ b/src/__tests__/patients/care-goals/CareGoalTab.test.tsx @@ -108,6 +108,8 @@ describe('Care Goals Tab', () => { await waitFor(() => { expect(screen.queryByRole('dialog')).not.toBeInTheDocument() + }, { + timeout: 3000, }) expect( From 5e4eba4bb72232d667d7c63965b3db85ed8ec142 Mon Sep 17 00:00:00 2001 From: Braydon Hall <40751395+nobrayner@users.noreply.github.com> Date: Fri, 8 Jan 2021 13:28:05 +1100 Subject: [PATCH 5/7] fix(test): linting --- .../patients/care-goals/CareGoalTab.test.tsx | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/__tests__/patients/care-goals/CareGoalTab.test.tsx b/src/__tests__/patients/care-goals/CareGoalTab.test.tsx index d038a66954..8c2ac21444 100644 --- a/src/__tests__/patients/care-goals/CareGoalTab.test.tsx +++ b/src/__tests__/patients/care-goals/CareGoalTab.test.tsx @@ -106,11 +106,14 @@ describe('Care Goals Tab', () => { userEvent.click(within(modal).getByRole('button', { name: /patient.careGoal.new/i })) - await waitFor(() => { - expect(screen.queryByRole('dialog')).not.toBeInTheDocument() - }, { - timeout: 3000, - }) + await waitFor( + () => { + expect(screen.queryByRole('dialog')).not.toBeInTheDocument() + }, + { + timeout: 3000, + }, + ) expect( await screen.findByRole('cell', { name: expectedCareGoal.description }), From 4cf0e7689d8d3d0b7c70510c427ae5c01feaea53 Mon Sep 17 00:00:00 2001 From: Braydon Hall <40751395+nobrayner@users.noreply.github.com> Date: Fri, 8 Jan 2021 15:27:59 +1100 Subject: [PATCH 6/7] chore: use jest config instead of setup tests --- package.json | 3 +++ src/__tests__/labs/hooks/useCancelLab.test.ts | 5 ++--- src/__tests__/labs/hooks/useLab.test.ts | 3 +-- src/__tests__/labs/hooks/useUpdateLab.test.ts | 3 +-- src/__tests__/patients/care-goals/CareGoalTab.test.tsx | 6 +++--- .../scheduling/hooks/useUpdateAppointment.test.tsx | 3 +-- src/setupTests.js | 1 - 7 files changed, 11 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index f409394c1a..11c2c27fa5 100644 --- a/package.json +++ b/package.json @@ -161,5 +161,8 @@ "npm run test:ci", "git add ." ] + }, + "jest": { + "restoreMocks": true } } diff --git a/src/__tests__/labs/hooks/useCancelLab.test.ts b/src/__tests__/labs/hooks/useCancelLab.test.ts index 31809fed8c..465826df29 100644 --- a/src/__tests__/labs/hooks/useCancelLab.test.ts +++ b/src/__tests__/labs/hooks/useCancelLab.test.ts @@ -15,10 +15,9 @@ describe('Use Cancel Lab', () => { canceledOn: expectedCanceledOnDate.toISOString(), } as Lab - Date.now = jest.fn(() => expectedCanceledOnDate.valueOf()) - jest.spyOn(LabRepository, 'saveOrUpdate').mockResolvedValue(expectedCanceledLab) - it('should cancel a lab', async () => { + Date.now = jest.fn(() => expectedCanceledOnDate.valueOf()) + jest.spyOn(LabRepository, 'saveOrUpdate').mockResolvedValue(expectedCanceledLab) const actualData = await executeMutation(() => useCancelLab(), lab) expect(LabRepository.saveOrUpdate).toHaveBeenCalledTimes(1) diff --git a/src/__tests__/labs/hooks/useLab.test.ts b/src/__tests__/labs/hooks/useLab.test.ts index 6c95da4cc9..93c03dd05c 100644 --- a/src/__tests__/labs/hooks/useLab.test.ts +++ b/src/__tests__/labs/hooks/useLab.test.ts @@ -9,9 +9,8 @@ describe('Use lab', () => { id: expectedLabId, } as Lab - jest.spyOn(LabRepository, 'find').mockResolvedValue(expectedLab) - it('should get a lab by id', async () => { + jest.spyOn(LabRepository, 'find').mockResolvedValue(expectedLab) const actualData = await executeQuery(() => useLab(expectedLabId)) expect(LabRepository.find).toHaveBeenCalledTimes(1) diff --git a/src/__tests__/labs/hooks/useUpdateLab.test.ts b/src/__tests__/labs/hooks/useUpdateLab.test.ts index 9e3de160ec..5ae054a78d 100644 --- a/src/__tests__/labs/hooks/useUpdateLab.test.ts +++ b/src/__tests__/labs/hooks/useUpdateLab.test.ts @@ -9,9 +9,8 @@ describe('Use update lab', () => { notes: ['some note'], } as Lab - jest.spyOn(LabRepository, 'saveOrUpdate').mockResolvedValue(expectedLab) - it('should update lab', async () => { + jest.spyOn(LabRepository, 'saveOrUpdate').mockResolvedValue(expectedLab) const actualData = await executeMutation(() => useUpdateLab(), expectedLab) expect(LabRepository.saveOrUpdate).toHaveBeenCalledTimes(1) diff --git a/src/__tests__/patients/care-goals/CareGoalTab.test.tsx b/src/__tests__/patients/care-goals/CareGoalTab.test.tsx index 8c2ac21444..ad075d2f1d 100644 --- a/src/__tests__/patients/care-goals/CareGoalTab.test.tsx +++ b/src/__tests__/patients/care-goals/CareGoalTab.test.tsx @@ -110,9 +110,9 @@ describe('Care Goals Tab', () => { () => { expect(screen.queryByRole('dialog')).not.toBeInTheDocument() }, - { - timeout: 3000, - }, + // { + // timeout: 3000, + // }, ) expect( diff --git a/src/__tests__/scheduling/hooks/useUpdateAppointment.test.tsx b/src/__tests__/scheduling/hooks/useUpdateAppointment.test.tsx index 37745c2702..bed1ac5158 100644 --- a/src/__tests__/scheduling/hooks/useUpdateAppointment.test.tsx +++ b/src/__tests__/scheduling/hooks/useUpdateAppointment.test.tsx @@ -14,9 +14,8 @@ describe('Use update appointment', () => { type: 'type', } as Appointment - jest.spyOn(AppointmentRepository, 'saveOrUpdate').mockResolvedValue(expectedAppointment) - it('should update appointment', async () => { + jest.spyOn(AppointmentRepository, 'saveOrUpdate').mockResolvedValue(expectedAppointment) const actualData = await executeMutation(() => { const result = useUpdateAppointment(expectedAppointment) return [result.mutate] diff --git a/src/setupTests.js b/src/setupTests.js index 9d4f554f8c..fb33a0a634 100644 --- a/src/setupTests.js +++ b/src/setupTests.js @@ -14,7 +14,6 @@ configure({ defaultHidden: true }) jest.setTimeout(10000) afterEach(() => { - jest.restoreAllMocks() queryCache.clear() }) From 2e194663e4150fbaa2ce27e0b9dce60ce5aedbc3 Mon Sep 17 00:00:00 2001 From: Braydon Hall <40751395+nobrayner@users.noreply.github.com> Date: Fri, 8 Jan 2021 17:49:49 +1100 Subject: [PATCH 7/7] fix(test): increase timeout --- src/__tests__/patients/care-goals/CareGoalTab.test.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/__tests__/patients/care-goals/CareGoalTab.test.tsx b/src/__tests__/patients/care-goals/CareGoalTab.test.tsx index ad075d2f1d..8c2ac21444 100644 --- a/src/__tests__/patients/care-goals/CareGoalTab.test.tsx +++ b/src/__tests__/patients/care-goals/CareGoalTab.test.tsx @@ -110,9 +110,9 @@ describe('Care Goals Tab', () => { () => { expect(screen.queryByRole('dialog')).not.toBeInTheDocument() }, - // { - // timeout: 3000, - // }, + { + timeout: 3000, + }, ) expect(