Skip to content

Commit

Permalink
Merge pull request #53 from EyeSeeTea/chore/test-apply-to-all-use-case
Browse files Browse the repository at this point in the history
chore: test ApplyToAllUseCase
  • Loading branch information
MiquelAdell authored Jun 14, 2024
2 parents b70fd1c + 787dcf1 commit 4e25fba
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 2 deletions.
74 changes: 74 additions & 0 deletions src/domain/common/usecases/__tests__/ApplyToAllUseCase.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { ApplyToAllUseCase } from "../ApplyToAllUseCase";
import { DataValueStore, DataValueTextMultiple } from "../../entities/DataValue";
import { DataValueRepository } from "../../repositories/DataValueRepository";
import { Row } from "../../../../webapp/reports/autogenerated-forms/GridWithTotalsViewModel";
import { mock, instance, when, verify, anything, deepEqual } from "ts-mockito";
import { Dhis2DataValueRepository } from "../../../../data/common/Dhis2DataValueRepository";
import { dataElement, dataValueNumberSingle, dataValueTextMultiple } from "./data/dataValue";

describe("ApplyToAllUseCase", () => {
let mockDataValueRepository: DataValueRepository;
let mockDataValueStore: DataValueStore;
let applyToAllUseCase: ApplyToAllUseCase;

beforeEach(() => {
mockDataValueRepository = mock<DataValueRepository>(Dhis2DataValueRepository);
mockDataValueStore = mock<DataValueStore>(DataValueStore);
applyToAllUseCase = new ApplyToAllUseCase(instance(mockDataValueRepository));
});

it("updates the store with the new values and calls applyToAll on the repository", async () => {
const row: Row = {
items: [
{
dataElement: { ...dataElement, id: "de2", code: "de2", name: "Data Element 2", type: "TEXT" },
column: {
isSourceType: true,
name: "",
},
disabled: false,
},
],
name: "",
includePadding: 0,
};

const updatedDataValueTextMultiple: DataValueTextMultiple = {
...dataValueTextMultiple,
dataElement: {
...dataValueTextMultiple.dataElement,
id: "de2",
name: "Data Element 2",
},
};

when(
mockDataValueStore.get(
anything(),
deepEqual({
period: dataValueTextMultiple.period,
orgUnitId: dataValueTextMultiple.orgUnitId,
categoryOptionComboId: dataValueTextMultiple.categoryOptionComboId,
})
)
)
.thenReturn(dataValueNumberSingle)
.thenReturn(updatedDataValueTextMultiple);

when(mockDataValueStore.set(anything())).thenReturn(instance(mockDataValueStore));

const stubDataValueStore = instance(mockDataValueStore);

const result = await applyToAllUseCase.execute(stubDataValueStore, dataValueTextMultiple, [], [row]);

verify(
mockDataValueRepository.applyToAll(
deepEqual(dataValueTextMultiple),
deepEqual([{ id: "de2", name: "Data Element 2" }])
)
).once();
verify(mockDataValueStore.get(anything(), anything())).twice();
verify(mockDataValueStore.set(deepEqual(updatedDataValueTextMultiple))).once();
expect(result).toEqual(stubDataValueStore);
});
});
36 changes: 34 additions & 2 deletions src/domain/common/usecases/__tests__/data/dataValue.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { DataElement } from "../../../entities/DataElement";
import { DataValueFile, DataValueTextSingle } from "../../../entities/DataValue";
import {
DataValueFile,
DataValueTextSingle,
DataValueTextMultiple,
DataValueNumberSingle,
} from "../../../entities/DataValue";

const dataElement: Omit<DataElement, "type"> = {
export const dataElement: Omit<DataElement, "type"> = {
id: "1",
code: "DE1",
name: "Element 1",
Expand Down Expand Up @@ -33,6 +38,33 @@ export const dataValueText: DataValueTextSingle = {
value: "10",
};

export const dataValueTextMultiple: DataValueTextMultiple = {
dataElement: { ...dataElement, id: "de1", code: "de1", type: "TEXT" },
period: "202101",
orgUnitId: "ou1",
categoryOptionComboId: "coc1",
values: ["value1", "value2"],
type: "TEXT",
isMultiple: true,
};

export const dataValueNumberSingle: DataValueNumberSingle = {
dataElement: {
...dataElement,
id: "de2",
code: "de2",
name: "Data Element 2",
type: "NUMBER",
numberType: "NUMBER",
},
period: "202101",
orgUnitId: "ou1",
categoryOptionComboId: "coc1",
value: "10",
type: "NUMBER",
isMultiple: false,
};

export const dataValueFile: DataValueFile = {
dataElement: { ...dataElement, type: "FILE" },
period: "202101",
Expand Down

0 comments on commit 4e25fba

Please sign in to comment.