feature/IO-2769-Job-Totals-testing: Setup testing method for job totals

This commit is contained in:
Dave Richer
2025-04-04 13:23:33 -04:00
parent 320ad065d0
commit add88659a4

View File

@@ -1,41 +1,44 @@
import fs from "fs";
import path from "path";
import { describe, it, expect } from "vitest";
import { TotalsServerSide } from "../job-totals"; // adjust the path as needed
import Dinero from "dinero.js";
import { TotalsServerSide } from "../job-totals";
// A custom replacer to normalize Dinero objects
function dineroReplacer(key, value) {
/**
* This function is used to replace the values in the object with their
* @param key
* @param value
* @returns {*}
*/
const dineroReplacer = (key, value) => {
if (value && typeof value === "object" && typeof value.toObject === "function") {
return value.toObject();
}
return value;
}
};
// Normalization function to convert any Dinero instances to plain objects
function normalizeOutput(obj) {
/**
* Normalizes the output of the TotalsServerSide function by converting
* @param obj
* @returns {any}
*/
const normalizeOutput = (obj) => {
return JSON.parse(JSON.stringify(obj, dineroReplacer));
}
};
/**
* This test suite is designed to validate the functionality of the TotalsServerSide function
*/
describe("TotalsServerSide fixture tests", () => {
// Define the fixture directory.
// For example, if this test file is at <projectroot>/server/job/test/,
// then the fixtures are in <projectroot>/server/job/test/fixtures/job-totals/
const fixturesDir = path.join(__dirname, "fixtures", "job-totals");
// Read all fixture JSON files from the fixture directory.
const fixtureFiles = fs.readdirSync(fixturesDir).filter((f) => f.endsWith(".json"));
// Create a dummy client. If your TotalsServerSide uses client.request,
// make sure these paths are either not triggered or stubbed accordingly.
const dummyClient = {
request: async () => {
// Return an empty object (or any other value that makes sense for your tests).
return {};
}
};
// Create a dummy response object. TotalsServerSide might only use it to send error statuses.
const dummyRes = {
status: () => ({ send: () => {} })
};
@@ -55,10 +58,8 @@ describe("TotalsServerSide fixture tests", () => {
user: {}
};
// Call the TotalsServerSide function with the fixture input.
const computedOutput = await TotalsServerSide(req, dummyRes);
// Normalize both computed and expected outputs so that any Dinero objects are replaced with their plain representation.
const normalizedComputed = normalizeOutput(computedOutput);
const normalizedExpected = normalizeOutput(expectedOutput);