Skip to content

Commit dc5f865

Browse files
authored
Merge pull request #3655 from obsidian-tasks-group/contrib-mock-date-examples
contrib: Add some examples of using SimulatedFile
2 parents 976b2dd + bda2f17 commit dc5f865

File tree

11 files changed

+134
-16
lines changed

11 files changed

+134
-16
lines changed

contributing/Testing/Using Obsidian API in tests.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,87 @@ So we need a way to access Obsidian-generated data in our tests. This page tries
1919
- `MockDataName` in [AllCacheSampleData.ts](https://github.com/obsidian-tasks-group/obsidian-tasks/blob/main/tests/Obsidian/AllCacheSampleData.ts) shows the names of all the available files, in a type-safe way.
2020
4. See all the [uses of this data so far, in Tasks tests](https://github.com/search?type=code&q=repo%3Aobsidian-tasks-group%2Fobsidian-tasks+%2F%28AllMockDataNames%7Cbuilder.mockData%7CgetMockDataAndReadTasks%7ClistPathAndData%7CMockDataLoader%7CMockDataName%7CreadTasksFromSimulatedFile%7CSimulatedFile%29%2F).
2121

22+
## Examples
23+
24+
- Test data: [as markdown files](https://github.com/obsidian-tasks-group/obsidian-tasks/tree/main/resources/sample_vaults/Tasks-Demo/Test%20Data)
25+
- Test data: [as json files](https://github.com/obsidian-tasks-group/obsidian-tasks/tree/main/tests/Obsidian/__test_data__)
26+
27+
### Using Obsidian's data about an individual test Markdown file
28+
29+
Example of the lowest-level usage, returning a [SimulatedFile](https://github.com/obsidian-tasks-group/obsidian-tasks/blob/main/tests/Obsidian/SimulatedFile.ts) object:
30+
31+
<!-- snippet: MockDataLoader.get -->
32+
```ts
33+
const data1 = MockDataLoader.get('one_task');
34+
```
35+
<!-- endSnippet -->
36+
37+
Create a `TasksFile` object for one of the Markdown files, for writing tests:
38+
39+
<!-- snippet: getTasksFileFromMockData -->
40+
```ts
41+
const tasksFile = getTasksFileFromMockData('no_yaml');
42+
```
43+
<!-- endSnippet -->
44+
45+
Obtain all the `Task` objects from one of the Markdown files:
46+
47+
<!-- snippet: readTasksFromSimulatedFile -->
48+
```ts
49+
const tasks = readTasksFromSimulatedFile('multiple_headings');
50+
```
51+
<!-- endSnippet -->
52+
53+
Obtain both a `TasksFile` object for one of the Markdown files, and the corresponding `Task` objects:
54+
55+
<!-- snippet: getMockDataAndReadTasks -->
56+
```ts
57+
const { data, tasks } = getMockDataAndReadTasks('callout_labelled');
58+
```
59+
<!-- endSnippet -->
60+
61+
### Using Obsidian's data about some or all the test Markdown files
62+
63+
Obtain all the `Task` objects from all the Markdown files:
64+
65+
<!-- snippet: readAllTasksFromAllSimulatedFiles -->
66+
```ts
67+
const allTasks = readAllTasksFromAllSimulatedFiles();
68+
```
69+
<!-- endSnippet -->
70+
71+
Iterate over all the Markdown files:
72+
73+
<!-- snippet: AllMockDataNames -->
74+
```ts
75+
let output = '';
76+
AllMockDataNames.forEach((file) => {
77+
const tasksFile = getTasksFileFromMockData(file);
78+
output += visualiseLinks(tasksFile.outlinksInProperties, file);
79+
});
80+
verifyMarkdown(output);
81+
```
82+
<!-- endSnippet -->
83+
84+
Selectively iterate over data from several of the Markdown files - using `listPathAndData()` to include each test file's `path` in the test name:
85+
86+
<!-- snippet: iterate-over-multiple-SimulatedFiles -->
87+
```ts
88+
it.each(
89+
listPathAndData([
90+
'yaml_custom_number_property', // no tags value in frontmatter
91+
'yaml_tags_field_added_by_obsidian_but_not_populated',
92+
'yaml_tags_had_value_then_was_emptied_by_obsidian',
93+
'yaml_tags_is_empty_list',
94+
'yaml_tags_is_empty',
95+
]),
96+
)('should provide empty list if no tags in frontmatter: "%s"', (_path: string, testDataName: MockDataName) => {
97+
const tasksFile = getTasksFileFromMockData(testDataName);
98+
expect(tasksFile.frontmatter.tags).toEqual([]);
99+
});
100+
```
101+
<!-- endSnippet -->
102+
22103
## Test data creation sequence
23104

24105
If using this on an Obsidian version newer than the one in saved `tests/Obsidian/__test_data__/*.json`, go to Settings → Files and links → Advanced → Rebuild vault cache.

resources/sample_vaults/Tasks-Demo/_meta/templater_scripts/convert_test_data_markdown_to_js.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,19 +135,27 @@ export type MockDataName =
135135
136136
/**
137137
* Names of all the sample data in \`resources/sample_vaults/Tasks-Demo/Test Data\`.
138+
*
139+
* To read all the tasks in all the sample data files,
140+
* use {@link readAllTasksFromAllSimulatedFiles}
141+
*
138142
* Example use:
139143
*
140144
* \`\`\`typescript
141-
* const tasks: Task[] = AllMockDataNames.flatMap((testDataName) => {
142-
* return readTasksFromSimulatedFile(testDataName);
145+
* let output = '';
146+
* AllMockDataNames.forEach((file) => {
147+
* const tasksFile = getTasksFileFromMockData(file);
148+
* output += visualiseLinks(tasksFile.outlinksInProperties, file);
143149
* });
150+
* verifyMarkdown(output);
144151
* \`\`\`
145152
*
146153
* Related code that uses some or all of this data:
147154
* - {@link SimulatedFile}
148155
* - {@link readTasksFromSimulatedFile}
149156
* - {@link getTasksFileFromMockData}
150157
* - {@link listPathAndData}
158+
* - {@link readAllTasksFromAllSimulatedFiles}
151159
*/
152160
export const AllMockDataNames: MockDataName[] = [
153161
'${basenames.join("',\n '")}',

tests/Obsidian/AllCacheSampleData.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,27 @@ export type MockDataName =
9393

9494
/**
9595
* Names of all the sample data in `resources/sample_vaults/Tasks-Demo/Test Data`.
96+
*
97+
* To read all the tasks in all the sample data files,
98+
* use {@link readAllTasksFromAllSimulatedFiles}
99+
*
96100
* Example use:
97101
*
98102
* ```typescript
99-
* const tasks: Task[] = AllMockDataNames.flatMap((testDataName) => {
100-
* return readTasksFromSimulatedFile(testDataName);
103+
* let output = '';
104+
* AllMockDataNames.forEach((file) => {
105+
* const tasksFile = getTasksFileFromMockData(file);
106+
* output += visualiseLinks(tasksFile.outlinksInProperties, file);
101107
* });
108+
* verifyMarkdown(output);
102109
* ```
103110
*
104111
* Related code that uses some or all of this data:
105112
* - {@link SimulatedFile}
106113
* - {@link readTasksFromSimulatedFile}
107114
* - {@link getTasksFileFromMockData}
108115
* - {@link listPathAndData}
116+
* - {@link readAllTasksFromAllSimulatedFiles}
109117
*/
110118
export const AllMockDataNames: MockDataName[] = [
111119
'all_link_types',

tests/Obsidian/Cache.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,9 @@ describe('cache', () => {
633633
});
634634

635635
it('callout_labelled', () => {
636+
// begin-snippet: getMockDataAndReadTasks
636637
const { data, tasks } = getMockDataAndReadTasks('callout_labelled');
638+
// end-snippet
637639
expect(data.fileContents).toMatchInlineSnapshot(`
638640
"# callout_labelled
639641

tests/Obsidian/FileParser.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ describe('FileParser', () => {
77
// I found that intentionally breaking the setting of _sectionIndex was
88
// not caught by any other tests.
99

10+
// begin-snippet: readTasksFromSimulatedFile
1011
const tasks = readTasksFromSimulatedFile('multiple_headings');
12+
// end-snippet
1113
const locationDataExceptTasksFile = tasks.map((task) => task.taskLocation.allFieldsExceptTasksFileForTesting());
1214
expect(locationDataExceptTasksFile).toMatchInlineSnapshot(`
1315
[

tests/Obsidian/SimulatedFile.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { Task } from 'Task/Task';
33
import { logging } from '../../src/lib/logging';
44
import { FileParser } from '../../src/Obsidian/FileParser';
55
import { MockDataLoader } from '../TestingTools/MockDataLoader';
6-
import type { MockDataName } from './AllCacheSampleData';
6+
import { AllMockDataNames, type MockDataName } from './AllCacheSampleData';
77

88
/**
99
* @file This file provides functions for creating {@link Task} objects from data in `tests/Obsidian/__test_data__`.
@@ -51,6 +51,7 @@ export interface SimulatedFile {
5151
* ```
5252
*
5353
* For more info, see https://publish.obsidian.md/tasks-contributing/Testing/Using+Obsidian+API+in+tests.
54+
* @see readAllTasksFromAllSimulatedFiles
5455
*/
5556
export function readTasksFromSimulatedFile(filename: MockDataName): Task[] {
5657
const testData = MockDataLoader.get(filename);
@@ -66,6 +67,18 @@ export function readTasksFromSimulatedFile(filename: MockDataName): Task[] {
6667
return fileParser.parseFileContent();
6768
}
6869

70+
/**
71+
* Read all tasks from Obsidian-specific data read from all JSON files in `tests/Obsidian/__test_data__`.
72+
*
73+
* For more info, see https://publish.obsidian.md/tasks-contributing/Testing/Using+Obsidian+API+in+tests.
74+
* @see readTasksFromSimulatedFile
75+
*/
76+
export function readAllTasksFromAllSimulatedFiles() {
77+
return AllMockDataNames.flatMap((testDataName) => {
78+
return readTasksFromSimulatedFile(testDataName);
79+
});
80+
}
81+
6982
function errorReporter() {
7083
return;
7184
}

tests/Scripting/ScriptingReference/CustomFiltering/CustomFilteringExamples.test.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
import moment from 'moment';
66
import type { Task } from '../../../../src/Task/Task';
7-
import { AllMockDataNames } from '../../../Obsidian/AllCacheSampleData';
8-
import { readTasksFromSimulatedFile } from '../../../Obsidian/SimulatedFile';
7+
import { readAllTasksFromAllSimulatedFiles } from '../../../Obsidian/SimulatedFile';
98
import { fromLine, fromLines } from '../../../TestingTools/TestHelpers';
109
import { SampleTasks } from '../../../TestingTools/SampleTasks';
1110
import type { CustomPropertyDocsTestData, QueryInstructionLineAndDescription } from '../VerifyFunctionFieldSamples';
@@ -357,9 +356,9 @@ describe('file properties', () => {
357356
});
358357

359358
describe('obsidian properties', () => {
360-
const tasks: Task[] = AllMockDataNames.flatMap((testDataName) => {
361-
return readTasksFromSimulatedFile(testDataName);
362-
});
359+
// begin-snippet: readAllTasksFromAllSimulatedFiles
360+
const allTasks = readAllTasksFromAllSimulatedFiles();
361+
// end-snippet
363362

364363
const testData: CustomPropertyDocsTestData[] = [
365364
// ---------------------------------------------------------------------------------
@@ -386,7 +385,7 @@ describe('obsidian properties', () => {
386385
"find tasks in files where the date property 'creation date' includes string '2024'",
387386
],
388387
],
389-
tasks,
388+
allTasks,
390389
],
391390
];
392391

tests/Scripting/ScriptingReference/CustomGrouping/CustomGroupingExamples.test.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
import moment from 'moment';
66

77
import type { Task } from '../../../../src/Task/Task';
8-
import { AllMockDataNames } from '../../../Obsidian/AllCacheSampleData';
9-
import { readTasksFromSimulatedFile } from '../../../Obsidian/SimulatedFile';
8+
import { readAllTasksFromAllSimulatedFiles } from '../../../Obsidian/SimulatedFile';
109
import { SampleTasks } from '../../../TestingTools/SampleTasks';
1110
import {
1211
type CustomPropertyDocsTestData,
@@ -380,9 +379,7 @@ describe('file properties', () => {
380379
});
381380

382381
describe('obsidian properties', () => {
383-
const tasks: Task[] = AllMockDataNames.flatMap((testDataName) => {
384-
return readTasksFromSimulatedFile(testDataName);
385-
});
382+
const tasks = readAllTasksFromAllSimulatedFiles();
386383

387384
const testData: CustomPropertyDocsTestData[] = [
388385
// ---------------------------------------------------------------------------------

tests/Scripting/TasksFile.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ describe('TasksFile - reading tags', () => {
296296
expect(tasksFile.tags).toEqual(['#task']);
297297
});
298298

299+
// begin-snippet: iterate-over-multiple-SimulatedFiles
299300
it.each(
300301
listPathAndData([
301302
'yaml_custom_number_property', // no tags value in frontmatter
@@ -308,6 +309,7 @@ describe('TasksFile - reading tags', () => {
308309
const tasksFile = getTasksFileFromMockData(testDataName);
309310
expect(tasksFile.frontmatter.tags).toEqual([]);
310311
});
312+
// end-snippet
311313

312314
it('should be able to read all tags for any loaded SimulatedFile', () => {
313315
const file1 = getTasksFileFromMockData('yaml_tags_with_one_value_on_new_line');
@@ -384,7 +386,9 @@ describe('TasksFile - properties', () => {
384386
});
385387

386388
it('should treat non-exising properties correctly', () => {
389+
// begin-snippet: getTasksFileFromMockData
387390
const tasksFile = getTasksFileFromMockData('no_yaml');
391+
// end-snippet
388392
expect(tasksFile.hasProperty('appleSauce')).toEqual(false);
389393
});
390394

tests/Task/Link.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,12 +344,14 @@ describe('visualise links', () => {
344344
});
345345

346346
it('properties', () => {
347+
// begin-snippet: AllMockDataNames
347348
let output = '';
348349
AllMockDataNames.forEach((file) => {
349350
const tasksFile = getTasksFileFromMockData(file);
350351
output += visualiseLinks(tasksFile.outlinksInProperties, file);
351352
});
352353
verifyMarkdown(output);
354+
// end-snippet
353355
});
354356

355357
it('outlinks', () => {

0 commit comments

Comments
 (0)