diff --git a/src/domain/common/usecases/__tests__/GetDataFormUseCase.spec.ts b/src/domain/common/usecases/__tests__/GetDataFormUseCase.spec.ts new file mode 100644 index 0000000..aaf1e50 --- /dev/null +++ b/src/domain/common/usecases/__tests__/GetDataFormUseCase.spec.ts @@ -0,0 +1,47 @@ +import { Dhis2DataFormRepository } from "../../../../data/common/Dhis2DataFormRepository"; +import { GetDataFormUseCase } from "../GetDataFormUseCase"; +import { DataFormRepository } from "../../repositories/DataFormRepository"; +import { Id } from "@eyeseetea/d2-api"; +import { Period } from "../../entities/DataValue"; +import { mock, instance, when, verify, deepEqual } from "ts-mockito"; +import { DataForm } from "../../entities/DataForm"; +import { dataForm } from "./data/dataForm"; + +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(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(); + }); +}); diff --git a/src/domain/common/usecases/__tests__/data/dataForm.ts b/src/domain/common/usecases/__tests__/data/dataForm.ts new file mode 100644 index 0000000..42db18b --- /dev/null +++ b/src/domain/common/usecases/__tests__/data/dataForm.ts @@ -0,0 +1,21 @@ +import { DataForm } from "../../../entities/DataForm"; + +export const dataForm: Omit = { + expiryDays: 30, + dataInputPeriods: [], + dataElements: [], + sections: [], + texts: { + header: undefined, + footer: undefined, + rowTotals: undefined, + totals: undefined, + name: undefined, + }, + options: { + dataElements: { + de1: { widget: "dropdown" }, + }, + }, + indicators: [], +};