@@ -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+ */
1330export 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+ */
1981export 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 ] ) ;
0 commit comments