Skip to content

Commit

Permalink
Merge pull request #50 from EyeSeeTea/chore/test-save-data-form-value
Browse files Browse the repository at this point in the history
chore: test SaveDataFormValue use case
  • Loading branch information
MiquelAdell authored Jun 7, 2024
2 parents 14b0a92 + 7312e40 commit 4ff52d1
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
87 changes: 87 additions & 0 deletions src/domain/common/usecases/__tests__/SaveDataFormValue.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { SaveDataFormValueUseCase } from "../SaveDataFormValue";
import { DataValue, DataValueStore } from "../../entities/DataValue";
import { DataValueRepository } from "../../repositories/DataValueRepository";
import { instance, mock, when, verify, anything, deepEqual } from "ts-mockito";
import { Dhis2DataValueRepository } from "../../../../data/common/Dhis2DataValueRepository";
import { dataValueText, dataValueFile } from "./data/dataValue";

let mockDataValueRepository: DataValueRepository;
let mockDataValueStore: DataValueStore;

describe("SaveDataFormValueUseCase", () => {
beforeEach(() => {
mockDataValueRepository = mock<DataValueRepository>(Dhis2DataValueRepository);
mockDataValueStore = mock<DataValueStore>(DataValueStore);
});

it("returns the store if the existing data value is equal to the new data value and type is not FILE", async () => {
const useCase = givenSaveDataFormValueUseCase();
const dataValue = dataValueText;
givenExistingDataValue(dataValue);

const stubDataValueStore = instance(mockDataValueStore);

const result = await useCase.execute(stubDataValueStore, dataValue);

verify(mockDataValueStore.get(dataValue.dataElement, dataValue)).once();
verify(mockDataValueStore.set(anything())).never();
verify(mockDataValueRepository.save(anything())).never();
expect(result).toBe(stubDataValueStore);
});

it("updates the store with the new data value and saves it", async () => {
const useCase = givenSaveDataFormValueUseCase();
const dataValue = dataValueText;
const saveDataValue = { ...dataValueText, value: "20" };
givenExistingDataValue(dataValue);
givenSavedDataValue(saveDataValue);

const dataValueStore = DataValueStore.from([dataValue]);
const saveDataValueStore = DataValueStore.from([saveDataValue]);

const result = await useCase.execute(dataValueStore, saveDataValue);

verify(mockDataValueRepository.save(deepEqual(saveDataValue))).once();
expect(result).toStrictEqual(saveDataValueStore);
});

it("updates the store with the new FILE data value and saves it", async () => {
const useCase = givenSaveDataFormValueUseCase();
const dataValue = dataValueFile;
const saveDataValue = {
...dataValueFile,
file: {
id: "1",
name: "Test file",
size: 1024,
url: "/updated/path/to/file",
},
};
givenExistingDataValue(dataValue);
givenSavedDataValue(saveDataValue);

const dataValueStore = DataValueStore.from([dataValue]);
const saveDataValueStore = DataValueStore.from([saveDataValue]);

const result = await useCase.execute(dataValueStore, saveDataValue);

verify(mockDataValueRepository.save(deepEqual(saveDataValue))).once();
expect(result).toStrictEqual(saveDataValueStore);
});
});

function givenSaveDataFormValueUseCase() {
const stubDataValueRepository = instance(mockDataValueRepository);
return new SaveDataFormValueUseCase(stubDataValueRepository);
}

function givenExistingDataValue(dataValue: DataValue) {
when(mockDataValueStore.get(dataValue.dataElement, dataValue)).thenReturn(dataValue);
}

function givenSavedDataValue(savedDataValue: DataValue) {
const stubDataValueStore = instance(mockDataValueStore);

when(mockDataValueRepository.save(savedDataValue)).thenResolve(savedDataValue);
when(mockDataValueStore.set(savedDataValue)).thenReturn(stubDataValueStore);
}
49 changes: 49 additions & 0 deletions src/domain/common/usecases/__tests__/data/dataValue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { DataElement } from "../../../entities/DataElement";
import { DataValueFile, DataValueTextSingle } from "../../../entities/DataValue";

const dataElement: Omit<DataElement, "type"> = {
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 dataValueFile: DataValueFile = {
dataElement: { ...dataElement, type: "FILE" },
period: "202101",
orgUnitId: "ou1",
categoryOptionComboId: "coc1",
isMultiple: false,
type: "FILE",
file: {
id: "1",
name: "Test file",
size: 1024,
url: "/path/to/file",
},
};
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -18391,6 +18391,13 @@ [email protected]:
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"

[email protected]:
version "10.4.0"
resolved "https://registry.npmjs.org/ts-node/-/ts-node-10.4.0.tgz"
Expand Down

0 comments on commit 4ff52d1

Please sign in to comment.