mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-07-31 20:22:27 +08:00
chore(test): add test for content_renderer#renderCode
This commit is contained in:
parent
f12057f799
commit
a05e174640
@ -1,6 +1,9 @@
|
|||||||
{
|
{
|
||||||
"spec_dir": "spec",
|
"spec_dir": "",
|
||||||
"spec_files": ["./**/*.spec.ts"],
|
"spec_files": [
|
||||||
|
"spec/**/*.spec.ts",
|
||||||
|
"src/**/*.spec.ts"
|
||||||
|
],
|
||||||
"helpers": ["helpers/**/*.js"],
|
"helpers": ["helpers/**/*.js"],
|
||||||
"stopSpecOnExpectationFailure": false,
|
"stopSpecOnExpectationFailure": false,
|
||||||
"random": true
|
"random": true
|
||||||
|
33
src/share/content_renderer.spec.ts
Normal file
33
src/share/content_renderer.spec.ts
Normal file
@ -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 <pre>", () => {
|
||||||
|
const result: Result = {
|
||||||
|
header: "",
|
||||||
|
content: "\tHello\nworld"
|
||||||
|
};
|
||||||
|
renderCode(result);
|
||||||
|
expect(result.isEmpty).toBeFalsy();
|
||||||
|
expect(result.content).toBe("<pre>\tHello\nworld</pre>");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
@ -5,10 +5,14 @@ import shareRoot from "./share_root.js";
|
|||||||
import escapeHtml from "escape-html";
|
import escapeHtml from "escape-html";
|
||||||
import SNote from "./shaca/entities/snote.js";
|
import SNote from "./shaca/entities/snote.js";
|
||||||
|
|
||||||
interface Result {
|
/**
|
||||||
|
* Represents the output of the content renderer.
|
||||||
|
*/
|
||||||
|
export interface Result {
|
||||||
header: string;
|
header: string;
|
||||||
content: string | Buffer | undefined;
|
content: string | Buffer | undefined;
|
||||||
isEmpty: boolean;
|
/** Set to `true` if the provided content should be rendered as empty. */
|
||||||
|
isEmpty?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getContent(note: SNote) {
|
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()) {
|
if (typeof result.content !== "string" || !result.content?.trim()) {
|
||||||
result.isEmpty = true;
|
result.isEmpty = true;
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user