Files
content-factory/apps/frontend/tests/article_form_validation.test.mjs
T

46 lines
1.3 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/article-intake/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 validateArticleDraft = moduleExports.validateArticleDraft;
const initialDraft = moduleExports.initialDraft;
const draft = {
targetSiteId: "",
briefDescription: "Keep this text intact",
workingTitle: "",
contentType: "longform_guide",
primaryKeyword: "",
};
const errors = validateArticleDraft(draft);
assert.ok(errors.targetSiteId, "validation should require target site");
assert.equal(
draft.briefDescription,
"Keep this text intact",
"brief description value should remain in form state when validation fails",
);
const fallbackDraft = initialDraft([]);
assert.equal(fallbackDraft.contentType, "longform_guide", "default content type preserved");