Skip to content

Commit ba9b932

Browse files
authored
Merge pull request #3631 from obsidian-tasks-group/refactor-test-data-loading
internal: Refactor loading of test data, to simplify code
2 parents 0e70176 + 99f7bf6 commit ba9b932

27 files changed

+581
-521
lines changed

contributing/Testing/Using Obsidian API in tests.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@ The Tasks plugin uses data created by the Obsidian API. The Obsidian API does no
1313
So we need a way to access Obsidian-generated data in our tests. This page tries to describe this mechanism.
1414

1515
1. [resources/sample_vaults/Tasks-Demo/Test Data](https://github.com/obsidian-tasks-group/obsidian-tasks/tree/main/resources/sample_vaults/Tasks-Demo/Test%20Data) contains representative samples Markdown files for different scenarios.
16-
2. The process described in [[#Test data creation sequence]] converts these files to matching JSON files in [tests/Obsidian/\_\_test_data\_\_](https://github.com/obsidian-tasks-group/obsidian-tasks/tree/main/tests/Obsidian/__test_data__).
17-
3. See all the [uses of this data so far, in Tasks tests](https://github.com/search?q=repo%3Aobsidian-tasks-group%2Fobsidian-tasks+__test_data__+language%3ATypeScript&type=code&l=TypeScript).
16+
2. The process described in [[#Test data creation sequence]] below converts these files to matching JSON files in [tests/Obsidian/\_\_test_data\_\_](https://github.com/obsidian-tasks-group/obsidian-tasks/tree/main/tests/Obsidian/__test_data__).
17+
3. In tests:
18+
- use [MockDataLoader.get()](https://github.com/obsidian-tasks-group/obsidian-tasks/blob/main/tests/TestingTools/MockDataLoader.ts) and some related functions, to read in the saved JSON files to memory
19+
- `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.
20+
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).
1821

1922
## Test data creation sequence
20-
23+
2124
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.
22-
25+
2326
- Create a sample markdown file in Tasks demo vault (root/Test Data/) with the simplest content to represent your test case. Choose a meaningful file name in snake case. See example in `Test Data/one_task.md`.
2427
- There is a Templater template that may help with creating a new file, for single-tasks cases: `resources/sample_vaults/Tasks-Demo/_meta/templates/Test Data file.md`.
2528
- Open any other note in the vault, just so that Templater will run.

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,30 +125,33 @@ async function convertMarkdownFileToTestFunction(filePath, tp) {
125125
async function writeListOfAllTestFunctions(files) {
126126
const basenames = files.map((file) => getBasename(file));
127127

128-
const imports = basenames.map((filename) => `import ${filename} from './__test_data__/${filename}.json';`);
129-
const functions = basenames.map((filename) => ` ${filename},`);
130-
131128
const content = `// DO NOT EDIT!
132129
// This file is machine-generated in the test vault, by convert_test_data_markdown_to_js.js.
133130
134131
import type { SimulatedFile } from './SimulatedFile';
135132
136-
${imports.join('\n')}
133+
export type MockDataName =
134+
| '${basenames.join("'\n | '")}';
137135
138136
/**
139-
* All the sample data in \`resources/sample_vaults/Tasks-Demo/Test Data\`.
137+
* Names of all the sample data in \`resources/sample_vaults/Tasks-Demo/Test Data\`.
138+
* Example use:
139+
*
140+
* \`\`\`typescript
141+
* const tasks: Task[] = AllMockDataNames.flatMap((testDataName) => {
142+
* return readTasksFromSimulatedFile(testDataName);
143+
* });
144+
* \`\`\`
140145
*
141146
* Related code that uses some or all of this data:
142147
* - {@link SimulatedFile}
143148
* - {@link readTasksFromSimulatedFile}
144149
* - {@link getTasksFileFromMockData}
145150
* - {@link listPathAndData}
146151
*/
147-
export function allCacheSampleData(): SimulatedFile[] {
148-
return [
149-
${functions.join('\n')}
150-
];
151-
}
152+
export const AllMockDataNames: MockDataName[] = [
153+
'${basenames.join("',\n '")}',
154+
];
152155
`;
153156

154157
const testSourceFile = getOutputFilePath('AllCacheSampleData.ts');

tests/DocumentationSamples/DefaultsDocs/DocsSamplesForDefaults.test.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@ import type { Pos } from 'obsidian';
22

33
import { verify, verifyAsJson } from 'approvals/lib/Providers/Jest/JestApprovals';
44
import { getTasksFileFromMockData } from '../../TestingTools/MockDataHelpers';
5-
import docs_sample_for_task_properties_reference from '../../Obsidian/__test_data__/docs_sample_for_task_properties_reference.json';
6-
import query_file_defaults_all_options_null from '../../Obsidian/__test_data__/query_file_defaults_all_options_null.json';
7-
import query_file_defaults_all_options_true from '../../Obsidian/__test_data__/query_file_defaults_all_options_true.json';
8-
import query_file_defaults_short_mode from '../../Obsidian/__test_data__/query_file_defaults_short_mode.json';
95
import { verifyWithFileExtension } from '../../TestingTools/ApprovalTestHelpers';
106
import { verifyMarkdown, verifyMarkdownForDocs } from '../../TestingTools/VerifyMarkdown';
117
import { QueryFileDefaults } from '../../../src/Query/QueryFileDefaults';
8+
import { MockDataLoader } from '../../TestingTools/MockDataLoader';
9+
import type { MockDataName } from '../../Obsidian/AllCacheSampleData';
1210

13-
function extractFrontmatter(data: any) {
14-
const queryFile = getTasksFileFromMockData(data);
11+
function extractFrontmatter(testDataName: MockDataName) {
12+
const data = MockDataLoader.get(testDataName);
13+
const queryFile = getTasksFileFromMockData(testDataName);
1514
const pos: Pos | undefined = queryFile.cachedMetadata.frontmatterPosition;
1615
return data.fileContents.slice(pos?.start.offset ?? 0, pos?.end.offset ?? 0);
1716
}
1817

1918
describe('DocsSamplesForDefaults', () => {
2019
it('supported-properties-empty', () => {
21-
const frontmatter = extractFrontmatter(query_file_defaults_all_options_null);
20+
const testDataName = 'query_file_defaults_all_options_null';
21+
const frontmatter = extractFrontmatter(testDataName);
2222

2323
// Make sure that any trailing spaces have been removed from the
2424
// properties in query_file_defaults_all_options_null.md, to avoid
@@ -30,25 +30,24 @@ describe('DocsSamplesForDefaults', () => {
3030
});
3131

3232
it('supported-properties-full', () => {
33-
verifyWithFileExtension(extractFrontmatter(query_file_defaults_all_options_true), '.yaml');
33+
verifyWithFileExtension(extractFrontmatter('query_file_defaults_all_options_true'), '.yaml');
3434
});
3535

3636
it('interpret_properties', () => {
37-
verifyWithFileExtension(extractFrontmatter(docs_sample_for_task_properties_reference), '.yaml');
37+
verifyWithFileExtension(extractFrontmatter('docs_sample_for_task_properties_reference'), '.yaml');
3838
});
3939

4040
describe('demo-short-mode', () => {
41-
// Load query_file_defaults_short_mode.json
42-
const data = query_file_defaults_short_mode;
41+
const testDataName = 'query_file_defaults_short_mode';
4342

4443
it('yaml', () => {
4544
// Extract the frontmatter to a file, for docs
46-
verifyWithFileExtension(extractFrontmatter(data), '.yaml');
45+
verifyWithFileExtension(extractFrontmatter(testDataName), '.yaml');
4746
});
4847

4948
it('instructions', () => {
5049
// Create the instruction from it, for docs
51-
const tasksFile = getTasksFileFromMockData(data);
50+
const tasksFile = getTasksFileFromMockData(testDataName);
5251
const generatedSource = new QueryFileDefaults().source(tasksFile);
5352
verify(generatedSource);
5453
});

0 commit comments

Comments
 (0)