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

Commit

Permalink
Merge pull request #240 from kcdraidgroup/nobrayner-prfb-1
Browse files Browse the repository at this point in the history
Better Can Create Care Goal test
  • Loading branch information
nobrayner authored Jan 8, 2021
2 parents 67c29c7 + 2e19466 commit c47ce48
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 68 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -161,5 +161,8 @@
"npm run test:ci",
"git add ."
]
},
"jest": {
"restoreMocks": true
}
}
5 changes: 2 additions & 3 deletions src/__tests__/labs/hooks/useCancelLab.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions src/__tests__/labs/hooks/useLab.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions src/__tests__/labs/hooks/useUpdateLab.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
51 changes: 6 additions & 45 deletions src/__tests__/patients/care-goals/AddCareGoalModal.test.tsx
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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(
<Router history={history}>
<AddCareGoalModal patient={patient} show onCloseButtonClick={onCloseSpy} />
</Router>,
),
onCloseSpy,
}
return render(
<Router history={history}>
<AddCareGoalModal patient={patient} show onCloseButtonClick={jest.fn()} />
</Router>,
)
}

beforeEach(() => {
jest.resetAllMocks()
})

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

Expand All @@ -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)
})
})
6 changes: 4 additions & 2 deletions src/__tests__/patients/care-goals/CareGoalForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<CareGoalForm
careGoal={careGoal2}
Expand All @@ -35,9 +38,8 @@ const setup = (disabled = false, initializeCareGoal = true, error?: any) => {
/>
)
}
const wrapper = render(<TestComponent />)

return { ...wrapper, onCareGoalChangeSpy }
return { ...render(<TestComponent />), onCareGoalChangeSpy }
}

describe('Care Goal Form', () => {
Expand Down
77 changes: 66 additions & 11 deletions src/__tests__/patients/care-goals/CareGoalTab.test.tsx
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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<RootState, any>([thunk])
const { selectAll, arrowDown, enter } = specialChars

type CareGoalTabWrapper = (store: any, history: MemoryHistory) => React.FC

Expand All @@ -36,7 +38,12 @@ const ViewWrapper: CareGoalTabWrapper = (store: any, history: MemoryHistory) =>
</Provider>
)

const setup = (route: string, permissions: Permissions[], wrapper = TabWrapper) => {
const setup = (
route: string,
permissions: Permissions[],
wrapper = TabWrapper,
includeCareGoal = true,
) => {
const expectedCareGoal = {
id: '456',
status: 'accepted',
Expand All @@ -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] })
Expand All @@ -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', [])

Expand All @@ -72,6 +76,57 @@ 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 waitFor(
() => {
expect(screen.queryByRole('dialog')).not.toBeInTheDocument()
},
{
timeout: 3000,
},
)

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()
}, 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])

Expand Down
3 changes: 1 addition & 2 deletions src/__tests__/scheduling/hooks/useUpdateAppointment.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
1 change: 0 additions & 1 deletion src/setupTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ configure({ defaultHidden: true })
jest.setTimeout(10000)

afterEach(() => {
jest.restoreAllMocks()
queryCache.clear()
})

Expand Down

0 comments on commit c47ce48

Please sign in to comment.