Skip to content

Commit

Permalink
add test for GetDataFormUseCase
Browse files Browse the repository at this point in the history
  • Loading branch information
fdelemarre committed May 24, 2024
1 parent 0eec7d2 commit 14103b0
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/domain/common/usecases/__tests__/GetDataFormUseCase.spec.ts
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 { 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<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();
});
});
21 changes: 21 additions & 0 deletions src/domain/common/usecases/__tests__/data/dataForm.ts
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: [],
};

0 comments on commit 14103b0

Please sign in to comment.