Skip to content

Commit 0cd3746

Browse files
committed
test: - Improve jsdoc blocks for test helpers
1 parent a6292b5 commit 0cd3746

File tree

3 files changed

+106
-2
lines changed

3 files changed

+106
-2
lines changed

tests/Obsidian/SimulatedFile.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,22 @@ import { setCurrentCacheFile } from '../__mocks__/obsidian';
1010
* - See also {@link getTasksFileFromMockData} and {@link listPathAndData}.
1111
*/
1212

13+
/**
14+
* Represent Obsidian-specific data read from a JSON file in `tests/Obsidian/__test_data__/`
15+
*
16+
* See the related functions that uses some or all of this data:
17+
* - {@link readTasksFromSimulatedFile}
18+
* - {@link getTasksFileFromMockData}
19+
* - {@link listPathAndData}
20+
*
21+
* @property cachedMetadata - Snapshot of Obsidian's CachedMetadata for the source note.
22+
* @property filePath - The path of the source note in the test vault.
23+
* @property fileContents - The complete content of the source note.
24+
* @property getAllTags - Used for mocking the result of `getAllTags()` in tests.
25+
* @property parseFrontMatterTags - Used for mocking the result of `parseFrontMatterTags()` in tests.
26+
*
27+
* For more info, see https://publish.obsidian.md/tasks-contributing/Testing/Using+Obsidian+API+in+tests.
28+
*/
1329
export interface SimulatedFile {
1430
cachedMetadata: CachedMetadata;
1531
filePath: string;
@@ -19,8 +35,20 @@ export interface SimulatedFile {
1935
}
2036

2137
/**
22-
For explanations on how to test code that is using Obsidian API
23-
refer to https://publish.obsidian.md/tasks-contributing/Testing/Using+Obsidian+API+in+tests
38+
* Read tasks from Obsidian-specific data read from a JSON file in `tests/Obsidian/__test_data__`.
39+
*
40+
* @param {SimulatedFile} testData - Read from a JSON file in `tests/Obsidian/__test_data__`
41+
* @return {ParsedTasks} The parsed tasks extracted from the file content.
42+
*
43+
* Example use:
44+
* ```typescript
45+
* import numbered_list_items_with_paren from './__test_data__/numbered_list_items_with_paren.json';
46+
* ...
47+
* const data = numbered_list_items_with_paren;
48+
* const tasks = readTasksFromSimulatedFile(data);
49+
* ```
50+
*
51+
* For more info, see https://publish.obsidian.md/tasks-contributing/Testing/Using+Obsidian+API+in+tests.
2452
*/
2553
export function readTasksFromSimulatedFile(testData: SimulatedFile) {
2654
const logger = logging.getLogger('testCache');

tests/TestingTools/MockDataHelpers.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,74 @@ import type { SimulatedFile } from '../Obsidian/SimulatedFile';
1010
* - See also {@link SimulatedFile} and {@link readTasksFromSimulatedFile}.
1111
*/
1212

13+
/**
14+
* Retrieve a {@link TasksFile} instance from the provided mock data.
15+
*
16+
* Example use:
17+
*
18+
* ```typescript
19+
* import example_kanban from '../Obsidian/__test_data__/example_kanban.json';
20+
* const data = example_kanban;
21+
* const tasksFile = getTasksFileFromMockData(data);
22+
* ```
23+
*
24+
* @param {any} data - Mock data containing the file information and metadata.
25+
* This will be a JSON file, read from `tests/Obsidian/__test_data__`
26+
* @return {TasksFile} An instance of {@link TasksFile} initialized with the file path and cached metadata.
27+
*
28+
* For more info, see https://publish.obsidian.md/tasks-contributing/Testing/Using+Obsidian+API+in+tests.
29+
*/
1330
export function getTasksFileFromMockData(data: SimulatedFile) {
1431
setCurrentCacheFile(data);
1532
const cachedMetadata = data.cachedMetadata as CachedMetadata;
1633
return new TasksFile(data.filePath, cachedMetadata);
1734
}
1835

36+
/**
37+
* Transform an array of {@link SimulatedFile} objects into an array of tuples containing file paths and data.
38+
*
39+
* This function is used to prepare {@link allCacheSampleData} for use with jest's it.each().
40+
*
41+
* ```typescript
42+
* it.each(listPathAndData(allCacheSampleData()))(
43+
* 'should be able to read tasks from all mock files: "%s"',
44+
* (path: string, file: any) => {
45+
* const tasks = readTasksFromSimulatedFile(file);
46+
* const files_without_tasks = [
47+
* 'Test Data/docs_sample_for_explain_query_file_defaults.md',
48+
* 'Test Data/non_tasks.md',
49+
* ];
50+
* if (files_without_tasks.includes(path)) {
51+
* expect(tasks.length).toEqual(0);
52+
* } else {
53+
* expect(tasks.length).toBeGreaterThan(0);
54+
* }
55+
* },
56+
* );
57+
* ```
58+
*
59+
* It can also be used with specific test files:
60+
*
61+
* ```typescript
62+
* it.each(
63+
* listPathAndData([
64+
* yaml_custom_number_property, // no tags value in frontmatter
65+
* yaml_tags_field_added_by_obsidian_but_not_populated,
66+
* yaml_tags_had_value_then_was_emptied_by_obsidian,
67+
* yaml_tags_is_empty_list,
68+
* yaml_tags_is_empty,
69+
* ]),
70+
* )('should provide empty list if no tags in frontmatter: "%s"', (_path: string, data: any) => {
71+
* const tasksFile = getTasksFileFromMockData(data);
72+
* expect(tasksFile.frontmatter.tags).toEqual([]);
73+
* });
74+
* ```
75+
*
76+
* @param {SimulatedFile[]} inputs - Array of {@link SimulatedFile} objects.
77+
* @returns {[string, SimulatedFile][]} Array of tuples, where each tuple contains [filePath, SimulatedFile]
78+
*
79+
* For more info, see https://publish.obsidian.md/tasks-contributing/Testing/Using+Obsidian+API+in+tests.
80+
*/
1981
export function listPathAndData(inputs: SimulatedFile[]): [string, SimulatedFile][] {
2082
// We use map() to extract the path, to use it as a test name in it.each()
2183
return inputs.map((data) => [data.filePath, data]);

tests/__mocks__/obsidian.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ export function setCurrentCacheFile(mockData: any) {
130130
mockedFileData = mockData;
131131
}
132132

133+
/**
134+
* Fake implementation of Obsidian's `getAllTags()`.
135+
*
136+
* See https://docs.obsidian.md/Reference/TypeScript+API/getAllTags
137+
*
138+
* @param cachedMetadata
139+
*/
133140
export function getAllTags(cachedMetadata: CachedMetadata): string[] {
134141
if (cachedMetadata !== mockedFileData.cachedMetadata) {
135142
throw new Error(
@@ -139,6 +146,13 @@ export function getAllTags(cachedMetadata: CachedMetadata): string[] {
139146
return mockedFileData.getAllTags;
140147
}
141148

149+
/**
150+
* Fake implementation of Obsidian's `parseFrontMatterTags()`.
151+
*
152+
* See https://docs.obsidian.md/Reference/TypeScript+API/parseFrontMatterTags
153+
*
154+
* @param frontmatter
155+
*/
142156
export function parseFrontMatterTags(frontmatter: any | null): string[] | null {
143157
if (frontmatter !== mockedFileData.cachedMetadata.frontmatter) {
144158
throw new Error(

0 commit comments

Comments
 (0)