dotwee_obsidian-raindropio-.../tests/block-parser.test.ts
Lukas Wolfsteiner 877b75ddc5 feat: add testing framework and initial test cases
- Introduce Jest as the testing framework and configure it for the project.
- Add initial test cases for block and note parsing functionalities.
- Update ESLint configuration to include test files and adjust global settings.
- Modify package.json to include test scripts and dependencies for Jest.
- Enhance GitHub workflows to run tests during linting and release processes.
2026-05-02 01:33:06 +02:00

66 lines
1.6 KiB
TypeScript

import { extractFirstRaindropBlock, parseRaindropBlock } from "block-parser";
describe("parseRaindropBlock", () => {
it("parses supported key-value options", () => {
const parsed = parseRaindropBlock(`
# Favorite references
collection: 42
search: obsidian plugin
tag: read later
sort: -created
limit: 25
`);
expect(parsed).toEqual({
options: {
collectionId: 42,
search: "obsidian plugin",
tag: "read later",
sort: "-created",
limit: 25,
},
warnings: [],
});
});
it("ignores invalid collections and warns", () => {
const parsed = parseRaindropBlock("collection: -1");
expect(parsed.options.collectionId).toBeUndefined();
expect(parsed.warnings).toEqual(["Invalid `collection`; using the default collection."]);
});
it("clamps valid numeric limits", () => {
expect(parseRaindropBlock("limit: 250").options.limit).toBe(100);
expect(parseRaindropBlock("limit: 0").options.limit).toBe(1);
});
it("warns and falls back when the limit is not numeric", () => {
const parsed = parseRaindropBlock("limit: many");
expect(parsed.options.limit).toBe(1);
expect(parsed.warnings).toEqual(["Invalid `limit`; using the plugin default."]);
});
});
describe("extractFirstRaindropBlock", () => {
it("returns the first raindrop code block body", () => {
const block = extractFirstRaindropBlock(`
Before
\`\`\`raindrop
tag: docs
\`\`\`
\`\`\`raindrop
tag: later
\`\`\`
`);
expect(block).toBe("tag: docs\n");
});
it("returns null when no raindrop block exists", () => {
expect(extractFirstRaindropBlock("```ts\nconsole.log('nope');\n```")).toBeNull();
});
});