-
Notifications
You must be signed in to change notification settings - Fork 1
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 #51 from EyeSeeTea/chore/test-get-data-form-use-case
chore: test GetDataFormUseCase
- Loading branch information
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
src/domain/common/usecases/__tests__/GetDataFormUseCase.spec.ts
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,47 @@ | ||
import { Dhis2DataFormRepository } from "../../../../data/common/Dhis2DataFormRepository"; | ||
import { GetDataFormUseCase } from "../GetDataFormUseCase"; | ||
import { DataFormRepository } from "../../repositories/DataFormRepository"; | ||
import { Period } from "../../entities/DataValue"; | ||
import { mock, instance, when, verify, deepEqual } from "ts-mockito"; | ||
import { DataForm } from "../../entities/DataForm"; | ||
import { dataForm } from "./data/dataForm"; | ||
import { Id } from "../../entities/Base"; | ||
|
||
describe("GetDataFormUseCase", () => { | ||
let mockDataFormRepository: DataFormRepository; | ||
const dataSetId: Id = "dataSetId"; | ||
const period: Period = "202101"; | ||
const orgUnitId: Id = "orgUnitId"; | ||
const dataFormRepositoryGetOptions = { id: dataSetId, period, orgUnitId }; | ||
const options = { dataSetId, period, orgUnitId }; | ||
|
||
beforeEach(() => { | ||
mockDataFormRepository = mock<DataFormRepository>(Dhis2DataFormRepository); | ||
}); | ||
|
||
it("calls the repository with correct parameters and returns the data form", async () => { | ||
const expectedDataForm: DataForm = { | ||
id: dataSetId, | ||
...dataForm, | ||
}; | ||
|
||
when(mockDataFormRepository.get(deepEqual(dataFormRepositoryGetOptions))).thenResolve(expectedDataForm); | ||
|
||
const useCase = new GetDataFormUseCase(instance(mockDataFormRepository)); | ||
const result = await useCase.execute(options); | ||
|
||
expect(result).toEqual(expectedDataForm); | ||
verify(mockDataFormRepository.get(deepEqual(dataFormRepositoryGetOptions))).once(); | ||
}); | ||
|
||
it("handles errors thrown by the repository", async () => { | ||
const expectedError = new Error("Repository error"); | ||
|
||
when(mockDataFormRepository.get(deepEqual(dataFormRepositoryGetOptions))).thenReject(expectedError); | ||
|
||
const useCase = new GetDataFormUseCase(instance(mockDataFormRepository)); | ||
|
||
await expect(useCase.execute(options)).rejects.toThrow(expectedError); | ||
verify(mockDataFormRepository.get(deepEqual(dataFormRepositoryGetOptions))).once(); | ||
}); | ||
}); |
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,21 @@ | ||
import { DataForm } from "../../../entities/DataForm"; | ||
|
||
export const dataForm: Omit<DataForm, "id"> = { | ||
expiryDays: 30, | ||
dataInputPeriods: [], | ||
dataElements: [], | ||
sections: [], | ||
texts: { | ||
header: undefined, | ||
footer: undefined, | ||
rowTotals: undefined, | ||
totals: undefined, | ||
name: undefined, | ||
}, | ||
options: { | ||
dataElements: { | ||
de1: { widget: "dropdown" }, | ||
}, | ||
}, | ||
indicators: [], | ||
}; |