From add88659a4010144519c45e4484290493831e2cf Mon Sep 17 00:00:00 2001 From: Dave Richer Date: Fri, 4 Apr 2025 13:23:33 -0400 Subject: [PATCH] feature/IO-2769-Job-Totals-testing: Setup testing method for job totals --- server/job/test/job-totals.test.js | 37 +++++++++++++++--------------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/server/job/test/job-totals.test.js b/server/job/test/job-totals.test.js index b0a30a29f..d615d169c 100644 --- a/server/job/test/job-totals.test.js +++ b/server/job/test/job-totals.test.js @@ -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 /server/job/test/, - // then the fixtures are in /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);