Files
content-factory/apps/frontend/tests/boundary_questions.model.test.mjs

54 lines
1.6 KiB
JavaScript

import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import ts from "typescript";
const testDir = path.dirname(fileURLToPath(import.meta.url));
const sourcePath = path.resolve(
testDir,
"../src/features/boundary-questions/model.ts",
);
const source = readFileSync(sourcePath, "utf8");
const compiled = ts.transpileModule(source, {
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 },
});
const moduleExports = {};
new Function("exports", compiled.outputText)(moduleExports);
const { buildBoundaryQuestionRows, validateRequiredAnswers } = moduleExports;
const rows = buildBoundaryQuestionRows([
{
id: "11111111-1111-1111-1111-111111111111",
article_id: "22222222-2222-2222-2222-222222222222",
sort_order: 1,
category: "audience",
question: "Audience?",
answer: "",
is_required: true,
created_at: "2026-05-21T00:00:00Z",
updated_at: "2026-05-21T00:00:00Z",
},
{
id: "33333333-3333-3333-3333-333333333333",
article_id: "22222222-2222-2222-2222-222222222222",
sort_order: 2,
category: "visual_expectations",
question: "Visuals?",
answer: "",
is_required: false,
created_at: "2026-05-21T00:00:00Z",
updated_at: "2026-05-21T00:00:00Z",
},
]);
assert.equal(rows[0].stateLabel, "Required");
assert.equal(rows[1].stateLabel, "Optional");
const errors = validateRequiredAnswers(rows);
assert.equal(errors["11111111-1111-1111-1111-111111111111"], "Required answer");
assert.equal(errors["33333333-3333-3333-3333-333333333333"], undefined);