diff --git a/spec/support/jasmine.json b/spec/support/jasmine.json index 5043a9a03..85189e03b 100644 --- a/spec/support/jasmine.json +++ b/spec/support/jasmine.json @@ -1,6 +1,9 @@ { - "spec_dir": "spec", - "spec_files": ["./**/*.spec.ts"], + "spec_dir": "", + "spec_files": [ + "spec/**/*.spec.ts", + "src/**/*.spec.ts" + ], "helpers": ["helpers/**/*.js"], "stopSpecOnExpectationFailure": false, "random": true diff --git a/src/share/content_renderer.spec.ts b/src/share/content_renderer.spec.ts new file mode 100644 index 000000000..d79cd7840 --- /dev/null +++ b/src/share/content_renderer.spec.ts @@ -0,0 +1,33 @@ +import { renderCode, type Result } from "./content_renderer.js"; + +describe("content_renderer", () => { + describe("renderCode", () => { + it("identifies empty content", () => { + const emptyResult: Result = { + header: "", + content: " " + }; + renderCode(emptyResult); + expect(emptyResult.isEmpty).toBeTrue(); + }); + + it("identifies unsupported content type", () => { + const emptyResult: Result = { + header: "", + content: Buffer.from("Hello world") + }; + renderCode(emptyResult); + expect(emptyResult.isEmpty).toBeTrue(); + }); + + it("wraps code in
", () => { + const result: Result = { + header: "", + content: "\tHello\nworld" + }; + renderCode(result); + expect(result.isEmpty).toBeFalsy(); + expect(result.content).toBe("\tHello\nworld"); + }); + }); +}); diff --git a/src/share/content_renderer.ts b/src/share/content_renderer.ts index 6680bacda..c9c17b67f 100644 --- a/src/share/content_renderer.ts +++ b/src/share/content_renderer.ts @@ -5,10 +5,14 @@ import shareRoot from "./share_root.js"; import escapeHtml from "escape-html"; import SNote from "./shaca/entities/snote.js"; -interface Result { +/** + * Represents the output of the content renderer. + */ +export interface Result { header: string; content: string | Buffer | undefined; - isEmpty: boolean; + /** Set to `true` if the provided content should be rendered as empty. */ + isEmpty?: boolean; } function getContent(note: SNote) { @@ -137,7 +141,10 @@ function handleAttachmentLink(linkEl: HTMLAnchorElement, href: string) { } } -function renderCode(result: Result) { +/** + * Renders a code note. + */ +export function renderCode(result: Result) { if (typeof result.content !== "string" || !result.content?.trim()) { result.isEmpty = true; } else {