11import type { App , CachedMetadata , Reference } from 'obsidian' ;
22import type { SimulatedFile } from '../Obsidian/SimulatedFile' ;
3+ import { MockDataLoader } from '../TestingTools/MockDataLoader' ;
34
45export { } ;
56
@@ -132,44 +133,45 @@ function caseInsensitiveSubstringSearch(searchTerm: string, phrase: string): Sea
132133 : null ;
133134}
134135
135- let mockedFileData : any = { } ;
136-
137- export function setCurrentCacheFile ( mockData : SimulatedFile ) {
138- mockedFileData = mockData ;
139- }
140-
141- function reportInconsistentTestData ( functionName : string ) {
142- throw new Error (
143- `Inconsistent test data used in mock ${ functionName } (). Check setCurrentCacheFile() has been called with the correct {@link SimulatedFile} data.` ,
144- ) ;
145- }
146-
147136/**
148137 * Fake implementation of Obsidian's `getAllTags()`.
149138 *
150139 * See https://docs.obsidian.md/Reference/TypeScript+API/getAllTags
151140 *
152- * @param cachedMetadata
141+ * @param cachedMetadata - the CachedMetadata instance from a SimulatedFile that has
142+ * already been loaded via MockDataLoader.get().
143+ * @throws Error if no matching CachedMetadata is found in the MockDataLoader cache.
153144 */
154145export function getAllTags ( cachedMetadata : CachedMetadata ) : string [ ] {
155- if ( cachedMetadata !== mockedFileData . cachedMetadata ) {
156- reportInconsistentTestData ( 'getAllTags' ) ;
157- }
158- return mockedFileData . getAllTags ;
146+ const simulatedFile = MockDataLoader . findCachedMetaData ( cachedMetadata ) ;
147+ return simulatedFile . getAllTags ;
159148}
160149
161150/**
162151 * Fake implementation of Obsidian's `parseFrontMatterTags()`.
163152 *
164153 * See https://docs.obsidian.md/Reference/TypeScript+API/parseFrontMatterTags
165154 *
166- * @param frontmatter
155+ * @example
156+ * This works:
157+ * ```typescript
158+ * const tags = parseFrontMatterTags(tasksFile.cachedMetadata.frontmatter);
159+ * ```
160+ *
161+ * @example
162+ * This does not work:
163+ * ```typescript
164+ * const tags = parseFrontMatterTags(tasksFile.frontmatter);
165+ * ```
166+ *
167+ * @param frontmatter - the raw CachedMetadata.frontmatter instance from a SimulatedFile that has
168+ * already been loaded via MockDataLoader.get().
169+ * @throws Error if no matching frontmatter is found in the MockDataLoader cache,
170+ * or a `tasksFile.frontmatter` was supplied.
167171 */
168172export function parseFrontMatterTags ( frontmatter : any | null ) : string [ ] | null {
169- if ( frontmatter !== mockedFileData . cachedMetadata . frontmatter ) {
170- reportInconsistentTestData ( 'parseFrontMatterTags' ) ;
171- }
172- return mockedFileData . parseFrontMatterTags ;
173+ const simulatedFile = MockDataLoader . findFrontmatter ( frontmatter ) ;
174+ return simulatedFile . parseFrontMatterTags ;
173175}
174176
175177/**
@@ -180,7 +182,8 @@ export function parseFrontMatterTags(frontmatter: any | null): string[] | null {
180182 * See https://docs.obsidian.md/Reference/TypeScript+API/MetadataCache/getFirstLinkpathDest
181183 *
182184 * @param rawLink
183- * @param sourcePath
185+ * @param sourcePath - the path to a Markdown file in the test vault whose SimulatedFile has already
186+ * been loaded via MockDataLoader.get(). For example, 'Test Data/callout.md'
184187 *
185188 * @example
186189 * ```typescript
@@ -192,13 +195,11 @@ export function parseFrontMatterTags(frontmatter: any | null): string[] | null {
192195 * ```
193196 */
194197export function getFirstLinkpathDest ( rawLink : Reference , sourcePath : string ) : string | null {
195- if ( mockedFileData . filePath !== sourcePath ) {
196- reportInconsistentTestData ( 'getFirstLinkpathDest' ) ;
197- }
198- return getFirstLinkpathDestFromData ( mockedFileData , rawLink ) ;
198+ const simulatedFile = MockDataLoader . findDataFromMarkdownPath ( sourcePath ) ;
199+ return getFirstLinkpathDestFromData ( simulatedFile , rawLink ) ;
199200}
200201
201- export function getFirstLinkpathDestFromData ( data : any , rawLink : Reference ) {
202+ export function getFirstLinkpathDestFromData ( data : SimulatedFile , rawLink : Reference ) {
202203 if ( ! ( rawLink . link in data . resolveLinkToPath ) ) {
203204 console . log ( `Cannot find resolved path for ${ rawLink . link } in ${ data . filePath } in mock getFirstLinkpathDest()` ) ;
204205 }
0 commit comments