Page MenuHomeSealhub

api.test.ts
No OneTemporary

api.test.ts

import { Registry } from "./registry";
import assert from "assert";
import { BlockPrimitive } from "./primitives/block";
import { Primitive, PrimitiveJSON } from "./primitives/primitive";
import { HeadingPrimitive } from "./primitives/heading";
import { assertThrowsAsync } from "./utils/assert-throws";
import { parse_primitives } from "./primitives/parse_primitives";
import { resolve_component } from "./pipeline";
import { TextJSON } from "./primitives/text";
describe("api tests", () => {
it("has ability to add custom components", () => {
const reg = new Registry();
reg.register_component(
"custom",
(json: Record<string, unknown>): PrimitiveJSON => {
return {
type: "text",
content: json["content"] as string,
} as TextJSON;
}
);
let primitives: Record<string, unknown> = resolve_component(
reg,
"custom",
{
type: "custom",
content: "sample",
}
);
assert.deepEqual(primitives, {
type: "text",
content: "sample",
});
});
describe("has ability to extract information from the document", () => {
let parsed_block: Primitive;
before(() => {
parsed_block = parse_primitives([
{
type: "block",
content: [
{
type: "heading",
content: "sampleH",
level: 1,
},
{
type: "heading",
content: "sampleH2",
level: 1,
},
{
type: "image",
content: "sampleI",
},
{
type: "image",
content: "sampleI2",
},
],
},
])[0];
});
it("extract headings", () => {
assert.deepEqual(parsed_block.extract_headings(), [
new HeadingPrimitive({
type: "heading",
content: "sampleH",
level: 1,
}),
new HeadingPrimitive({
type: "heading",
content: "sampleH2",
level: 1,
}),
]);
});
it("extract images", () => {
assert.deepEqual(parsed_block.extract_images(), [
"sampleI",
"sampleI2",
]);
});
});
it("has ability to add a custom output format without changing jdd code itself", () => {
function render(primitives: Primitive[]): number {
let counter = 0;
for (const primitive of primitives) {
counter += primitive.extract_headings().length;
}
return counter;
}
assert.strictEqual(
render([
new BlockPrimitive({
type: "block",
content: [
{
type: "heading",
content: "sample",
level: 1,
},
],
}),
new HeadingPrimitive({ content: "sample1", level: 1 }),
]),
2
);
});
it("has ability to modify parsed primitives before rendering", () => {
const parsed_block = parse_primitives([
{
type: "block",
content: [
{
type: "block",
content: [
{
type: "heading",
content: "sampleH",
level: 1,
},
],
},
{
type: "heading",
content: "sampleH2",
level: 2,
},
],
},
])[0] as any;
for (const heading of parsed_block.extract_headings()) {
heading.level += 1;
}
assert.strictEqual(
parsed_block["content"][0]["content"][0]["level"],
2
);
assert.strictEqual(parsed_block["content"][1]["level"], 3);
});
// will make it pass when assertShape gets added to ts-predicates
it.skip("parse_primitives throws when given wrong input", async () => {
await assertThrowsAsync(
async () => {
return parse_primitives([
{
type: "block",
content: [
{
type: "heading",
content: "sampleH",
},
],
},
]);
},
(e: unknown) => {
assert.strictEqual(
(e as Error).message,
"Missing 'level attribute' in heading 'sampleH'"
);
}
);
});
});

File Metadata

Mime Type
text/x-java
Expires
Sat, Oct 11, 06:45 (11 h, 36 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
983906
Default Alt Text
api.test.ts (3 KB)

Event Timeline