76 lines
2.3 KiB
JavaScript
76 lines
2.3 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import { readFile } from "node:fs/promises";
|
|
import { resolve } from "node:path";
|
|
|
|
const source = await readFile(resolve("src/utils/longTextCard.js"), "utf8");
|
|
const moduleUrl = `data:text/javascript;base64,${Buffer.from(source).toString("base64")}`;
|
|
const longTextCard = await import(moduleUrl);
|
|
|
|
const {
|
|
LONG_TEXT_FIELD_CONFIG,
|
|
LONG_TEXT_KEYS,
|
|
parseLongTextDisplayValue,
|
|
sanitizeLongTextDisplayValue,
|
|
hasLongTextDisplayValue,
|
|
formatLongTextDisplayValue,
|
|
} = longTextCard;
|
|
|
|
assert.deepEqual(
|
|
parseLongTextDisplayValue('[ "see bridge", "hear water" ]'),
|
|
["see bridge", "hear water"],
|
|
"serialized arrays should be parsed for display"
|
|
);
|
|
|
|
assert.deepEqual(
|
|
parseLongTextDisplayValue('{"spot_name":"bridge","spot_longitude:":107.712345}'),
|
|
{ spot_name: "bridge", "spot_longitude:": 107.712345 },
|
|
"serialized objects should be parsed for display"
|
|
);
|
|
|
|
assert.deepEqual(
|
|
sanitizeLongTextDisplayValue(
|
|
{
|
|
content: "hidden",
|
|
components: ["hidden"],
|
|
view_section_items: '[ "check piers", "count arches" ]',
|
|
nested: {
|
|
title: "keep",
|
|
},
|
|
},
|
|
["content", "components"]
|
|
),
|
|
{
|
|
view_section_items: ["check piers", "count arches"],
|
|
nested: {
|
|
title: "keep",
|
|
},
|
|
},
|
|
"sanitizing should parse JSON-like strings and ignore configured keys"
|
|
);
|
|
|
|
assert.equal(hasLongTextDisplayValue('["follow up"]'), true);
|
|
assert.equal(hasLongTextDisplayValue({ a: "", b: [" ", null] }), false);
|
|
assert.equal(formatLongTextDisplayValue(true), "\u662f");
|
|
assert.equal(formatLongTextDisplayValue({ title: "bridge" }), '{"title":"bridge"}');
|
|
|
|
const expectedNewKeys = {
|
|
preparationSectionTitle: "preparation_section_title",
|
|
preparationSectionItems: "preparation_section_items",
|
|
sectionSuggestionTitle: "section_suggestion_title",
|
|
sectionSuggestionContent: "section_suggestion_content",
|
|
pitfallSectionTitle: "pitfall_section_title",
|
|
pitfallSectionItems: "pitfall_section_items",
|
|
commodityList: "commodity_list",
|
|
};
|
|
|
|
for (const [keyName, keyValue] of Object.entries(expectedNewKeys)) {
|
|
assert.equal(LONG_TEXT_KEYS[keyName], keyValue, `${keyName} should be registered`);
|
|
assert.equal(
|
|
LONG_TEXT_FIELD_CONFIG.some((item) => item.key === keyValue),
|
|
true,
|
|
`${keyValue} should be in LONG_TEXT_FIELD_CONFIG`
|
|
);
|
|
}
|
|
|
|
console.log("longTextCard display helpers passed");
|