Skip to content

Commit 17ed1fd

Browse files
committed
test: - Add exploratory tests for accessing links
1 parent 1e06f18 commit 17ed1fd

File tree

10 files changed

+729
-1
lines changed

10 files changed

+729
-1
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# link_in_task_html
2+
3+
- [ ] #task Task in 'link_in_task_html' - see the [Tasks Documentation](https://publish.obsidian.md/tasks/Introduction)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# link_in_task_markdown_link
2+
3+
- [ ] #task Task in 'link_in_task_markdown_link' [jason_properties](jason_properties) [multiple_headings](multiple_headings)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# link_in_task_wikilink
2+
3+
- [ ] #task Task in 'link_in_task_wikilink' [[link_in_task_wikilink]] [[multiple_headings]]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
link-in-frontmatter: "[[link_in_yaml]]"
3+
---
4+
# links_everywhere
5+
6+
A link in the file body: [[link_in_file_body]]
7+
8+
## A link in a [[link_in_heading]]
9+
10+
- [ ] #task Task in 'links_everywhere' - a link on the task: [[link_in_task_wikilink]]

tests/Obsidian/AllCacheSampleData.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,12 @@ import jason_properties from './__test_data__/jason_properties.json';
3636
import link_in_file_body from './__test_data__/link_in_file_body.json';
3737
import link_in_file_body_with_custom_display_text from './__test_data__/link_in_file_body_with_custom_display_text.json';
3838
import link_in_heading from './__test_data__/link_in_heading.json';
39+
import link_in_task_html from './__test_data__/link_in_task_html.json';
40+
import link_in_task_markdown_link from './__test_data__/link_in_task_markdown_link.json';
41+
import link_in_task_wikilink from './__test_data__/link_in_task_wikilink.json';
3942
import link_in_yaml from './__test_data__/link_in_yaml.json';
4043
import link_is_broken from './__test_data__/link_is_broken.json';
44+
import links_everywhere from './__test_data__/links_everywhere.json';
4145
import list_statuses from './__test_data__/list_statuses.json';
4246
import list_styles from './__test_data__/list_styles.json';
4347
import multi_line_task_and_list_item from './__test_data__/multi_line_task_and_list_item.json';
@@ -101,8 +105,12 @@ export function allCacheSampleData() {
101105
link_in_file_body,
102106
link_in_file_body_with_custom_display_text,
103107
link_in_heading,
108+
link_in_task_html,
109+
link_in_task_markdown_link,
110+
link_in_task_wikilink,
104111
link_in_yaml,
105112
link_is_broken,
113+
links_everywhere,
106114
list_statuses,
107115
list_styles,
108116
multi_line_task_and_list_item,

tests/Obsidian/Cache.test.ts

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* @jest-environment jsdom
33
*/
44
import moment from 'moment/moment';
5+
import type { CachedMetadata } from 'obsidian';
56
import type { ListItem } from '../../src/Task/ListItem';
67
import { getTasksFileFromMockData, listPathAndData } from '../TestingTools/MockDataHelpers';
78
import inheritance_1parent1child from './__test_data__/inheritance_1parent1child.json';
@@ -29,8 +30,9 @@ import callout from './__test_data__/callout.json';
2930
import callout_labelled from './__test_data__/callout_labelled.json';
3031
import callout_custom from './__test_data__/callout_custom.json';
3132
import callouts_nested_issue_2890_unlabelled from './__test_data__/callouts_nested_issue_2890_unlabelled.json';
33+
import links_everywhere from './__test_data__/links_everywhere.json';
3234
import { allCacheSampleData } from './AllCacheSampleData';
33-
import { readTasksFromSimulatedFile } from './SimulatedFile';
35+
import { type SimulatedFile, readTasksFromSimulatedFile } from './SimulatedFile';
3436

3537
window.moment = moment;
3638

@@ -604,6 +606,112 @@ describe('cache', () => {
604606
});
605607
});
606608

609+
describe('accessing links in file', function () {
610+
describe('explore accessing links in file "links_everywhere.md"', () => {
611+
const data = links_everywhere as unknown as SimulatedFile;
612+
613+
const tasks = readTasksFromSimulatedFile(data);
614+
expect(tasks.length).toEqual(1);
615+
const task = tasks[0];
616+
617+
const cachedMetadata: CachedMetadata = task.file.cachedMetadata;
618+
619+
it('see source', () => {
620+
expect(data.fileContents).toMatchInlineSnapshot(`
621+
"---
622+
link-in-frontmatter: "[[link_in_yaml]]"
623+
---
624+
# links_everywhere
625+
626+
A link in the file body: [[link_in_file_body]]
627+
628+
## A link in a [[link_in_heading]]
629+
630+
- [ ] #task Task in 'links_everywhere' - a link on the task: [[link_in_task_wikilink]]
631+
"
632+
`);
633+
});
634+
635+
it('should access links in frontmatter', () => {
636+
// @ts-expect-error TS2551: Property frontmatterLinks does not exist on type CachedMetadata
637+
const frontMatterLinks = cachedMetadata['frontmatterLinks'];
638+
639+
const firstFrontMatterLink = frontMatterLinks[0];
640+
expect(firstFrontMatterLink.original).toEqual('[[link_in_yaml]]');
641+
expect(firstFrontMatterLink).toMatchInlineSnapshot(`
642+
{
643+
"displayText": "link_in_yaml",
644+
"key": "link-in-frontmatter",
645+
"link": "link_in_yaml",
646+
"original": "[[link_in_yaml]]",
647+
}
648+
`);
649+
});
650+
651+
it('should access links in file body', () => {
652+
const fileBodyLinks = cachedMetadata.links;
653+
654+
const originalLinkText = fileBodyLinks?.map((link) => link.original).join('\n');
655+
expect(originalLinkText).toMatchInlineSnapshot(`
656+
"[[link_in_file_body]]
657+
[[link_in_heading]]
658+
[[link_in_task_wikilink]]"
659+
`);
660+
661+
const firstFileBodyLink = fileBodyLinks![0];
662+
expect(firstFileBodyLink).toMatchInlineSnapshot(`
663+
{
664+
"displayText": "link_in_file_body",
665+
"link": "link_in_file_body",
666+
"original": "[[link_in_file_body]]",
667+
"position": {
668+
"end": {
669+
"col": 46,
670+
"line": 5,
671+
"offset": 114,
672+
},
673+
"start": {
674+
"col": 25,
675+
"line": 5,
676+
"offset": 93,
677+
},
678+
},
679+
}
680+
`);
681+
});
682+
683+
it('should access links in task line', () => {
684+
const fileBodyLinks = cachedMetadata.links;
685+
const linksOnTask = fileBodyLinks?.filter((link) => link.position.start.line === task.lineNumber);
686+
687+
expect(linksOnTask).toBeDefined();
688+
expect(linksOnTask?.length).toEqual(1);
689+
690+
const firstLinkOnTask = linksOnTask![0];
691+
expect(firstLinkOnTask.original).toEqual('[[link_in_task_wikilink]]');
692+
expect(firstLinkOnTask).toMatchInlineSnapshot(`
693+
{
694+
"displayText": "link_in_task_wikilink",
695+
"link": "link_in_task_wikilink",
696+
"original": "[[link_in_task_wikilink]]",
697+
"position": {
698+
"end": {
699+
"col": 86,
700+
"line": 9,
701+
"offset": 238,
702+
},
703+
"start": {
704+
"col": 61,
705+
"line": 9,
706+
"offset": 213,
707+
},
708+
},
709+
}
710+
`);
711+
});
712+
});
713+
});
714+
607715
describe('all mock files', () => {
608716
const files: any = allCacheSampleData();
609717

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
{
2+
"cachedMetadata": {
3+
"headings": [
4+
{
5+
"heading": "link_in_task_html",
6+
"level": 1,
7+
"position": {
8+
"end": {
9+
"col": 19,
10+
"line": 0,
11+
"offset": 19
12+
},
13+
"start": {
14+
"col": 0,
15+
"line": 0,
16+
"offset": 0
17+
}
18+
}
19+
}
20+
],
21+
"listItems": [
22+
{
23+
"parent": -2,
24+
"position": {
25+
"end": {
26+
"col": 119,
27+
"line": 2,
28+
"offset": 140
29+
},
30+
"start": {
31+
"col": 0,
32+
"line": 2,
33+
"offset": 21
34+
}
35+
},
36+
"task": " "
37+
}
38+
],
39+
"sections": [
40+
{
41+
"position": {
42+
"end": {
43+
"col": 19,
44+
"line": 0,
45+
"offset": 19
46+
},
47+
"start": {
48+
"col": 0,
49+
"line": 0,
50+
"offset": 0
51+
}
52+
},
53+
"type": "heading"
54+
},
55+
{
56+
"position": {
57+
"end": {
58+
"col": 119,
59+
"line": 2,
60+
"offset": 140
61+
},
62+
"start": {
63+
"col": 0,
64+
"line": 2,
65+
"offset": 21
66+
}
67+
},
68+
"type": "list"
69+
}
70+
],
71+
"tags": [
72+
{
73+
"position": {
74+
"end": {
75+
"col": 11,
76+
"line": 2,
77+
"offset": 32
78+
},
79+
"start": {
80+
"col": 6,
81+
"line": 2,
82+
"offset": 27
83+
}
84+
},
85+
"tag": "#task"
86+
}
87+
]
88+
},
89+
"fileContents": "# link_in_task_html\n\n- [ ] #task Task in 'link_in_task_html' - see the [Tasks Documentation](https://publish.obsidian.md/tasks/Introduction)\n",
90+
"filePath": "Test Data/link_in_task_html.md",
91+
"getAllTags": [
92+
"#task"
93+
],
94+
"obsidianApiVersion": "1.7.7",
95+
"parseFrontMatterTags": null
96+
}

0 commit comments

Comments
 (0)