From 015fb1ad61154a2a555e24b70bd032eaf4a32fc6 Mon Sep 17 00:00:00 2001 From: fdelemarre Date: Thu, 23 May 2024 10:33:25 +0200 Subject: [PATCH 1/2] add `ts-mockito` package --- package.json | 1 + yarn.lock | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/package.json b/package.json index 7027424..2589b0f 100644 --- a/package.json +++ b/package.json @@ -130,6 +130,7 @@ "prettier": "2.4.1", "react-test-renderer": "17.0.2", "ts-jest": "27.0.7", + "ts-mockito": "^2.6.1", "ts-node": "10.4.0", "typescript": "4.5.2", "wait-on": "6.0.0" diff --git a/yarn.lock b/yarn.lock index d639e22..6baa3b7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -18391,6 +18391,13 @@ ts-jest@27.0.7: semver "7.x" yargs-parser "20.x" +ts-mockito@^2.6.1: + version "2.6.1" + resolved "https://registry.yarnpkg.com/ts-mockito/-/ts-mockito-2.6.1.tgz#bc9ee2619033934e6fad1c4455aca5b5ace34e73" + integrity sha512-qU9m/oEBQrKq5hwfbJ7MgmVN5Gu6lFnIGWvpxSjrqq6YYEVv+RwVFWySbZMBgazsWqv6ctAyVBpo9TmAxnOEKw== + dependencies: + lodash "^4.17.5" + ts-node@10.4.0: version "10.4.0" resolved "https://registry.npmjs.org/ts-node/-/ts-node-10.4.0.tgz" From 0744010550d298f6618849163489d6d354b8ee30 Mon Sep 17 00:00:00 2001 From: fdelemarre Date: Fri, 24 May 2024 11:49:15 +0200 Subject: [PATCH 2/2] test ApplyToAllUseCase --- .../__tests__/ApplyToAllUseCase.spec.ts | 74 +++++++++++++++++++ .../usecases/__tests__/data/dataValue.ts | 61 +++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 src/domain/common/usecases/__tests__/ApplyToAllUseCase.spec.ts create mode 100644 src/domain/common/usecases/__tests__/data/dataValue.ts diff --git a/src/domain/common/usecases/__tests__/ApplyToAllUseCase.spec.ts b/src/domain/common/usecases/__tests__/ApplyToAllUseCase.spec.ts new file mode 100644 index 0000000..5ceda77 --- /dev/null +++ b/src/domain/common/usecases/__tests__/ApplyToAllUseCase.spec.ts @@ -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(Dhis2DataValueRepository); + mockDataValueStore = mock(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); + }); +}); diff --git a/src/domain/common/usecases/__tests__/data/dataValue.ts b/src/domain/common/usecases/__tests__/data/dataValue.ts new file mode 100644 index 0000000..c69aa37 --- /dev/null +++ b/src/domain/common/usecases/__tests__/data/dataValue.ts @@ -0,0 +1,61 @@ +import { DataElement } from "../../../entities/DataElement"; +import { DataValueNumberSingle, DataValueTextMultiple, DataValueTextSingle } from "../../../entities/DataValue"; + +export const dataElement: Omit = { + id: "1", + code: "DE1", + name: "Element 1", + categoryCombos: { + id: "1", + name: "Combo", + categoryOptionCombos: [ + { + id: "1", + name: "Option Combo", + shortName: "OC", + formName: undefined, + }, + ], + }, + categoryOptionCombos: [], + rules: [], + htmlText: undefined, + related: undefined, +}; + +export const dataValueText: DataValueTextSingle = { + dataElement: { ...dataElement, type: "TEXT" }, + period: "202101", + orgUnitId: "ou1", + categoryOptionComboId: "coc1", + isMultiple: false, + type: "TEXT", + 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, +};