Skip to content

Commit 7238e31

Browse files
GoBeromsuclaude
andauthored
test: add comprehensive tests for frontmatter index functions (#32)
Add complete test coverage for all exported functions from src/frontmatter/index.ts: - getContentWithoutFrontmatter: 5 test cases covering frontmatter extraction - getFrontmatterSetting: 8 test cases covering success and error scenarios - getFieldValues: 9 test cases with Obsidian MetadataCache mocking - insertToFrontMatter: 11 test cases covering all insertion modes Update __mocks__/obsidian.ts with required Obsidian API mocks: - getFrontMatterInfo for frontmatter parsing - parseFrontMatterStringArray for field value extraction - getAllTags for tag collection - MetadataCache and TFile classes for metadata testing All 32 tests pass successfully, total test suite: 145 passing tests. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 4.5 <[email protected]>
1 parent ac9a7f2 commit 7238e31

File tree

2 files changed

+570
-0
lines changed

2 files changed

+570
-0
lines changed

__mocks__/obsidian.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,80 @@
11
export const requestUrl = jest.fn();
22

3+
// Obsidian API functions
4+
export const getFrontMatterInfo = jest.fn((content: string) => {
5+
const match = content.match(/^---\n[\s\S]*?\n---\n/);
6+
if (match) {
7+
return {
8+
exists: true,
9+
frontmatter: match[0],
10+
contentStart: match[0].length,
11+
};
12+
}
13+
return {
14+
exists: false,
15+
frontmatter: '',
16+
contentStart: 0,
17+
};
18+
});
19+
20+
export const parseFrontMatterStringArray = jest.fn((frontmatter: any, key: string) => {
21+
if (!frontmatter || !frontmatter[key]) {
22+
return null;
23+
}
24+
const value = frontmatter[key];
25+
if (Array.isArray(value)) {
26+
return value;
27+
}
28+
return [value];
29+
});
30+
31+
export const getAllTags = jest.fn((cache: any) => {
32+
if (!cache) return null;
33+
const tags = new Set<string>();
34+
35+
// Add frontmatter tags
36+
if (cache.frontmatter && cache.frontmatter.tags) {
37+
const frontmatterTags = Array.isArray(cache.frontmatter.tags)
38+
? cache.frontmatter.tags
39+
: [cache.frontmatter.tags];
40+
frontmatterTags.forEach((tag: string) => tags.add(tag));
41+
}
42+
43+
// Add inline tags (simulated)
44+
if (cache.tags) {
45+
cache.tags.forEach((tag: any) => tags.add(tag.tag));
46+
}
47+
48+
return tags.size > 0 ? Array.from(tags) : null;
49+
});
50+
51+
// MetadataCache mock
52+
export class MetadataCache {
53+
private fileCaches = new Map<any, any>();
54+
55+
getFileCache = jest.fn((file: any) => {
56+
return this.fileCaches.get(file) || null;
57+
});
58+
59+
// Helper for testing
60+
setFileCache(file: any, cache: any) {
61+
this.fileCaches.set(file, cache);
62+
}
63+
64+
clearCache() {
65+
this.fileCaches.clear();
66+
}
67+
}
68+
69+
// TFile mock
70+
export class TFile {
71+
constructor(public path: string, public basename: string) {}
72+
}
73+
374
// Basic App mock for components relying on Obsidian's App
475
export class App {
576
vault = { getMarkdownFiles: jest.fn().mockReturnValue([]) } as any;
77+
metadataCache = new MetadataCache();
678
}
779

880
export class FuzzySuggestModal<T> {

0 commit comments

Comments
 (0)