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

Commit

Permalink
fix(scheduling): added hook tests
Browse files Browse the repository at this point in the history
added delete and new appointment hook tests
  • Loading branch information
WinstonPoh committed Oct 7, 2020
1 parent 9d96aa3 commit da046c7
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/__tests__/scheduling/hooks/useDeleteAppointment.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import useDeleteAppointment from '../../../scheduling/hooks/useDeleteAppointment'
import AppointmentRepository from '../../../shared/db/AppointmentRepository'
import Appointment from '../../../shared/model/Appointment'
import * as uuid from '../../../shared/util/uuid'
import executeMutation from '../../test-utils/use-mutation.util'

describe('use delete appoinment', () => {
beforeEach(() => {
jest.resetAllMocks()
})

it('should remove an appointment with the given id', async () => {
const expectedAppointmentId = '123'

const expectedAppointment = {
id: '123',
patient: '456',
startDateTime: new Date(2020, 10, 1, 0, 0, 0, 0).toISOString(),
endDateTime: new Date(2020, 10, 1, 1, 0, 0, 0).toISOString(),
location: 'location',
reason: 'reason',
type: 'type',
} as Appointment

jest.spyOn(AppointmentRepository, 'find').mockResolvedValue(expectedAppointment)
jest.spyOn(uuid, 'uuid').mockReturnValue(expectedAppointmentId)
jest.spyOn(AppointmentRepository, 'delete').mockResolvedValue(expectedAppointment)

const result = await executeMutation(() => useDeleteAppointment(), {
appointmentId: expectedAppointmentId,
})

expect(AppointmentRepository.find).toHaveBeenCalledTimes(1)
expect(AppointmentRepository.delete).toHaveBeenCalledTimes(1)
expect(AppointmentRepository.delete).toHaveBeenCalledWith(expectedAppointment)
expect(result).toEqual(expectedAppointment)
})
})
36 changes: 36 additions & 0 deletions src/__tests__/scheduling/hooks/useUpdateAppointment.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { act } from '@testing-library/react-hooks'

import useUpdateLab from '../../../labs/hooks/useUpdateLab'
import useUpdateAppointment from '../../../scheduling/hooks/useUpdateAppointment'
import AppointmentRepository from '../../../shared/db/AppointmentRepository'
import Appointment from '../../../shared/model/Appointment'
import executeMutation from '../../test-utils/use-mutation.util'

describe('Use update appointment', () => {
const expectedAppointment = {
id: '123',
patient: '456',
startDateTime: new Date(2020, 10, 1, 0, 0, 0, 0).toISOString(),
endDateTime: new Date(2020, 10, 1, 1, 0, 0, 0).toISOString(),
location: 'location',
reason: 'reason',
type: 'type',
} as Appointment

jest.spyOn(AppointmentRepository, 'saveOrUpdate').mockResolvedValue(expectedAppointment)

it('should update appointment', async () => {
let actualData: any

await act(async () => {
actualData = await executeMutation(() => {
const result = useUpdateAppointment(expectedAppointment)
return [result.mutate]
}, expectedAppointment)
})

expect(AppointmentRepository.saveOrUpdate).toHaveBeenCalledTimes(1)
expect(AppointmentRepository.saveOrUpdate).toHaveBeenCalledWith(expectedAppointment)
expect(actualData).toEqual(expectedAppointment)
})
})

0 comments on commit da046c7

Please # to comment.