import { mock } from "vitest-mock-extended";
import { formatItemNotes, formatZoteroNotes, splitNotes } from "../../src/utils";
import { ZItemNote } from "Types/transforms";
describe("Splitting HTML notes", () => {
const notes = ([
{ data: { note: "
Some text
\nSome other text
" } },
{ data: { note: "Some paragraph
Another paragraph
" } }
] as const).map(it => mock(it));
test("No separator provided - function throws", () => {
// @ts-expect-error "Test expects bad input"
expect(() => splitNotes([notes[0]]))
.toThrow();
});
test("Incorrect type of separator provided - function throws", () => {
// @ts-expect-error "Test expects bad input"
expect(() => splitNotes([notes[0]], ["abc"]))
.toThrow("Input is of type object, expected String");
});
test("Simple separator", () => {
expect(splitNotes([notes[0]], "\n"))
.toEqual([
[
"Some text
",
"Some other text
"
]
]);
});
test("HTML tag separator", () => {
expect(splitNotes([notes[0]], ""))
.toEqual([
[
"Some text",
"\n",
"Some other text"
]
]);
expect(splitNotes([notes[1]], ""))
.toEqual([
[
"Some paragraph",
"",
"Another paragraph"
]
]);
});
});
describe("Parsing HTML notes", () => {
const notes = ([
{ data: { note: "Note Title
Lorem ipsum
" } },
{ data: { note: "Click here to open a link" } },
{ data: { note: "See there for a link with attributes" } },
{ data: { note: "\n\nSome text\n" } },
{ data: { note: "\n\nA paragraph
" } },
{ data: { note: "Some text
\n\n- \nSome element\n
\n- \nAnother element\n
\n- \nA third element\n
\n
\nSome content
\n" } }
] as const).map(it => mock(it));
it("cleans markup from rich tags", () => {
expect(formatZoteroNotes([notes[0]]))
.toEqual(["**Note Title**Lorem ipsum"]);
});
it("formats links into Markdown", () => {
expect(formatItemNotes([notes[1], notes[2]], ""))
.toEqual([
"Click [here](https://example.com) to open a link",
"See [there](https://example.com) for a link with attributes"
]);
});
it("removes newlines", () => {
expect(formatItemNotes([notes[3]], ""))
.toEqual([
"Some text"
]);
expect(formatItemNotes([notes[4]], ""))
.toEqual([
"Some element\nA paragraph"
]);
});
it("cleans list markup", () => {
expect(formatZoteroNotes([notes[5]]))
.toEqual([
"Some text",
"Some element",
"Another element",
"A third element",
"Some content"
]);
expect(formatItemNotes([notes[5]], ""))
.toEqual([
"Some text",
"Some element\nAnother element\nA third element",
"Some content"
]);
});
});