This repository has been archived by the owner on Jan 9, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2273 from HospitalRun/remove-redux
refactor(redux): add react-query for incidents and patients search
- Loading branch information
Showing
72 changed files
with
2,076 additions
and
1,591 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { renderHook, act } from '@testing-library/react-hooks' | ||
|
||
import useIncident from '../../../incidents/hooks/useIncident' | ||
import IncidentRepository from '../../../shared/db/IncidentRepository' | ||
import Incident from '../../../shared/model/Incident' | ||
import waitUntilQueryIsSuccessful from '../../test-utils/wait-for-query.util' | ||
|
||
describe('useIncident', () => { | ||
it('should get an incident by id', async () => { | ||
const expectedIncidentId = 'some id' | ||
const expectedIncident = { | ||
id: expectedIncidentId, | ||
} as Incident | ||
jest.spyOn(IncidentRepository, 'find').mockResolvedValue(expectedIncident) | ||
|
||
let actualData: any | ||
await act(async () => { | ||
const renderHookResult = renderHook(() => useIncident(expectedIncidentId)) | ||
const { result } = renderHookResult | ||
await waitUntilQueryIsSuccessful(renderHookResult) | ||
actualData = result.current.data | ||
}) | ||
|
||
expect(IncidentRepository.find).toHaveBeenCalledTimes(1) | ||
expect(IncidentRepository.find).toBeCalledWith(expectedIncidentId) | ||
expect(actualData).toEqual(expectedIncident) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { act, renderHook } from '@testing-library/react-hooks' | ||
|
||
import useIncidents from '../../../incidents/hooks/useIncidents' | ||
import IncidentFilter from '../../../incidents/IncidentFilter' | ||
import IncidentSearchRequest from '../../../incidents/model/IncidentSearchRequest' | ||
import IncidentRepository from '../../../shared/db/IncidentRepository' | ||
import Incident from '../../../shared/model/Incident' | ||
import waitUntilQueryIsSuccessful from '../../test-utils/wait-for-query.util' | ||
|
||
describe('useIncidents', () => { | ||
it('it should search incidents', async () => { | ||
const expectedSearchRequest: IncidentSearchRequest = { | ||
status: IncidentFilter.all, | ||
} | ||
const expectedIncidents = [ | ||
{ | ||
id: 'some id', | ||
}, | ||
] as Incident[] | ||
jest.spyOn(IncidentRepository, 'search').mockResolvedValue(expectedIncidents) | ||
|
||
let actualData: any | ||
await act(async () => { | ||
const renderHookResult = renderHook(() => useIncidents(expectedSearchRequest)) | ||
const { result } = renderHookResult | ||
await waitUntilQueryIsSuccessful(renderHookResult) | ||
actualData = result.current.data | ||
}) | ||
|
||
expect(IncidentRepository.search).toHaveBeenCalledTimes(1) | ||
expect(IncidentRepository.search).toBeCalledWith(expectedSearchRequest) | ||
expect(actualData).toEqual(expectedIncidents) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { subDays } from 'date-fns' | ||
import shortid from 'shortid' | ||
|
||
import useReportIncident from '../../../incidents/hooks/useReportIncident' | ||
import * as incidentValidator from '../../../incidents/util/validate-incident' | ||
import { IncidentError } from '../../../incidents/util/validate-incident' | ||
import IncidentRepository from '../../../shared/db/IncidentRepository' | ||
import Incident from '../../../shared/model/Incident' | ||
import executeMutation from '../../test-utils/use-mutation.util' | ||
|
||
describe('useReportIncident', () => { | ||
beforeEach(() => { | ||
jest.restoreAllMocks() | ||
}) | ||
|
||
it('should save the incident with correct data', async () => { | ||
const expectedCode = '123456' | ||
const expectedDate = new Date(Date.now()) | ||
const expectedStatus = 'reported' | ||
const expectedReportedBy = 'some user' | ||
Date.now = jest.fn().mockReturnValue(expectedDate) | ||
|
||
const givenIncidentRequest = { | ||
category: 'some category', | ||
categoryItem: 'some category item', | ||
date: subDays(new Date(), 3).toISOString(), | ||
department: 'some department', | ||
description: 'some description', | ||
} as Incident | ||
|
||
const expectedIncident = { | ||
...givenIncidentRequest, | ||
code: `I-${expectedCode}`, | ||
reportedOn: expectedDate.toISOString(), | ||
status: expectedStatus, | ||
reportedBy: expectedReportedBy, | ||
} as Incident | ||
jest.spyOn(shortid, 'generate').mockReturnValue(expectedCode) | ||
jest.spyOn(IncidentRepository, 'save').mockResolvedValue(expectedIncident) | ||
|
||
const actualData = await executeMutation(() => useReportIncident(), givenIncidentRequest) | ||
expect(IncidentRepository.save).toHaveBeenCalledTimes(1) | ||
expect(IncidentRepository.save).toBeCalledWith(expectedIncident) | ||
expect(actualData).toEqual(expectedIncident) | ||
}) | ||
|
||
it('should throw an error if validation fails', async () => { | ||
const expectedIncidentError = { | ||
description: 'some description error', | ||
} as IncidentError | ||
|
||
jest.spyOn(incidentValidator, 'default').mockReturnValue(expectedIncidentError) | ||
jest.spyOn(IncidentRepository, 'save').mockResolvedValue({} as Incident) | ||
|
||
try { | ||
await executeMutation(() => useReportIncident(), {}) | ||
} catch (e) { | ||
expect(e).toEqual(expectedIncidentError) | ||
expect(IncidentRepository.save).not.toHaveBeenCalled() | ||
} | ||
}) | ||
}) |
Oops, something went wrong.
b1884c8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: