generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1771 from obsidian-tasks-group/mock_tests-for-tog…
…gling test: Create mock tests for File.replaceTaskWithTasks()
- Loading branch information
Showing
10 changed files
with
501 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 7 additions & 15 deletions
22
...ults/Tasks-Demo/Manual Testing/Task Toggling Scenarios/Embed Task in to Note.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,24 @@ | ||
# Embed Task in to Note | ||
|
||
See [When a block-referenced task is made complete, it is completed after rewriting the contents of another task. · Issue #688](https://github.com/obsidian-tasks-group/obsidian-tasks/issues/688) | ||
## Scenario: Embedding individual tasks | ||
|
||
- **Given** a bullet list which embeds a task via a block reference, in the same file or another file | ||
- **When** the embedded task is toggled | ||
- **Then** there is an error message saying that the task with the block reference could not be found, to be toggled | ||
- See [Issue #688](https://github.com/obsidian-tasks-group/obsidian-tasks/issues/688) | ||
|
||
## Category 1 | ||
|
||
- [ ] #task task1a | ||
- [ ] #task task1b | ||
- [ ] #task task1c | ||
|
||
## Category 2 | ||
|
||
- [ ] #task task2a | ||
- [ ] #task task2b ^ca47c7 | ||
- [ ] #task task2c | ||
|
||
## Scenario 3: Embedding individual tasks | ||
|
||
- **Given** A bullet list which embeds a task via a block reference, in the same file or another file | ||
- **When** When the embedded task is toggled | ||
- **Then** There is an error message saying that the task with the block reference could not be found, to be toggled | ||
- See [Issue #688](https://github.com/obsidian-tasks-group/obsidian-tasks/issues/688) | ||
|
||
## The embedded tasks | ||
|
||
--- | ||
To reproduce issue 688, click on the following checkbox in Reading mode: | ||
|
||
- ref task2 ![[#^ca47c7]] | ||
|
||
--- | ||
|
||
- ref task4 ![[Embed File in to Note - File to Embed#^fromseparatefile]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import type { ListItemCache, Pos } from 'obsidian'; | ||
import type { Task } from '../Task'; | ||
|
||
type MockTaskLocation = { | ||
path: string; | ||
lineNumber: number; | ||
sectionStart: number; | ||
sectionIndex: number; | ||
precedingHeader: string | null; | ||
}; | ||
|
||
/** a mock for {@link ListItemCache.task} */ | ||
type MockListItemCacheTask = string | undefined; | ||
|
||
/** a mock for {@link Pos} */ | ||
type MockPos = Pos; | ||
|
||
/** a mock for {@link ListItemCache} */ | ||
export type MockListItemCache = { task: string | undefined; position: Pos }; | ||
type MockListItemCaches = MockListItemCache[]; | ||
|
||
/** a mock for {@link Task} */ | ||
export type MockTask = { | ||
originalMarkdown: string; | ||
taskLocation: MockTaskLocation; | ||
}; | ||
|
||
/** All the data required to call {@link findLineNumberOfTaskToToggle} */ | ||
export type MockTogglingDataForTesting = { | ||
cacheData: { listItemsCache: MockListItemCache[] }; | ||
fileData: { fileLines: string[] }; | ||
taskData: MockTask; | ||
}; | ||
|
||
/** | ||
* This function can be used to save data that is used | ||
* when finding which line to toggle in a file. | ||
* @param originalTask | ||
* @param fileLines | ||
* @param listItemsCache | ||
* | ||
* @see saveMockDataForTesting | ||
*/ | ||
export function getMockDataForTesting( | ||
originalTask: Task, | ||
fileLines: string[], | ||
listItemsCache: ListItemCache[], | ||
): MockTogglingDataForTesting { | ||
const allDataFromListItemCache: MockListItemCaches = []; | ||
for (const listItemCache of listItemsCache) { | ||
const pos: MockPos = listItemCache.position; | ||
const task: MockListItemCacheTask = listItemCache.task; | ||
const dataFromListItemCache: MockListItemCache = { | ||
position: pos, | ||
task: task, | ||
}; | ||
allDataFromListItemCache.push(dataFromListItemCache); | ||
} | ||
const mockTaskLocation: MockTaskLocation = { | ||
path: originalTask.taskLocation.path, | ||
lineNumber: originalTask.taskLocation.lineNumber, | ||
sectionStart: originalTask.taskLocation.sectionStart, | ||
sectionIndex: originalTask.taskLocation.sectionIndex, | ||
precedingHeader: originalTask.taskLocation.precedingHeader, | ||
}; | ||
return { | ||
taskData: { | ||
originalMarkdown: originalTask.originalMarkdown, | ||
taskLocation: mockTaskLocation, | ||
}, | ||
fileData: { | ||
fileLines: fileLines, | ||
}, | ||
cacheData: { | ||
listItemsCache: allDataFromListItemCache, | ||
}, | ||
}; | ||
} | ||
|
||
/** | ||
* Write the supplied data to the console, so it can be saved for use in testing. | ||
* | ||
* @param originalTask | ||
* @param fileLines | ||
* @param listItemsCache | ||
*/ | ||
export function saveMockDataForTesting(originalTask: Task, fileLines: string[], listItemsCache: ListItemCache[]) { | ||
const everything = getMockDataForTesting(originalTask, fileLines, listItemsCache); | ||
console.error(`Inconsistent lines: SAVE THE OUTPUT | ||
data: | ||
${JSON.stringify(everything)} | ||
`); | ||
} |
5 changes: 5 additions & 0 deletions
5
...est.replaceTaskWithTasks_issue 688 - block referenced task correct behaviour.approved.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Tasks: Unable to find task in file Manual Testing/Task Toggling Scenarios/Embed Task in to Note.md. | ||
Expected task: | ||
- [ ] #task task2b ^ca47c7 | ||
Found task: | ||
- [ ] #task task1a |
5 changes: 5 additions & 0 deletions
5
...eTaskWithTasks_issue 688 - block referenced task current incorrect behaviour.approved.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Tasks: Unable to find task in file Manual Testing/Task Toggling Scenarios/Embed Task in to Note.md. | ||
Expected task: | ||
- [ ] #task task2b ^ca47c7 | ||
Found task: | ||
- [ ] #task task1a |
1 change: 1 addition & 0 deletions
1
tests/File.test.replaceTaskWithTasks_valid 2-task test.approved.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Success. Line found OK. No error reported. |
Oops, something went wrong.