feature/IO-2769-Job-Totals-testing: Setup testing method for job totals
This commit is contained in:
68
server/job/test/job-totals.test.js
Normal file
68
server/job/test/job-totals.test.js
Normal file
@@ -0,0 +1,68 @@
|
||||
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";
|
||||
|
||||
// A custom replacer to normalize Dinero objects
|
||||
function 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) {
|
||||
return JSON.parse(JSON.stringify(obj, dineroReplacer));
|
||||
}
|
||||
|
||||
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: () => {} })
|
||||
};
|
||||
|
||||
fixtureFiles.forEach((file) => {
|
||||
it(`should produce matching output for fixture file ${file}`, async () => {
|
||||
const fixturePath = path.join(fixturesDir, file);
|
||||
const fixtureData = JSON.parse(fs.readFileSync(fixturePath, "utf8"));
|
||||
|
||||
const { input, output: expectedOutput } = fixtureData;
|
||||
|
||||
const req = {
|
||||
body: {
|
||||
job: input,
|
||||
client: dummyClient
|
||||
},
|
||||
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);
|
||||
|
||||
expect(normalizedComputed).toEqual(normalizedExpected);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user