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 fs from "fs";
import path from "path"; import path from "path";
import { describe, it, expect } from "vitest"; import { describe, it, expect } from "vitest";
import { TotalsServerSide } from "../job-totals"; // adjust the path as needed import { TotalsServerSide } from "../job-totals";
import Dinero from "dinero.js";
// 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") { if (value && typeof value === "object" && typeof value.toObject === "function") {
return value.toObject(); return value.toObject();
} }
return value; 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)); return JSON.parse(JSON.stringify(obj, dineroReplacer));
} };
/**
* This test suite is designed to validate the functionality of the TotalsServerSide function
*/
describe("TotalsServerSide fixture tests", () => { 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"); 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")); 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 = { const dummyClient = {
request: async () => { request: async () => {
// Return an empty object (or any other value that makes sense for your tests).
return {}; return {};
} }
}; };
// Create a dummy response object. TotalsServerSide might only use it to send error statuses.
const dummyRes = { const dummyRes = {
status: () => ({ send: () => {} }) status: () => ({ send: () => {} })
}; };
@@ -55,10 +58,8 @@ describe("TotalsServerSide fixture tests", () => {
user: {} user: {}
}; };
// Call the TotalsServerSide function with the fixture input.
const computedOutput = await TotalsServerSide(req, dummyRes); 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 normalizedComputed = normalizeOutput(computedOutput);
const normalizedExpected = normalizeOutput(expectedOutput); const normalizedExpected = normalizeOutput(expectedOutput);