From dfca0e7547efef014348a4e16720fb082a79e429 Mon Sep 17 00:00:00 2001 From: Simon Clement Date: Sat, 24 Sep 2022 11:26:47 +0300 Subject: [PATCH] Add initial collector tests --- tests/suggestionCollector.test.ts | 87 +++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/tests/suggestionCollector.test.ts b/tests/suggestionCollector.test.ts index 1d9dd85..3fd15ad 100644 --- a/tests/suggestionCollector.test.ts +++ b/tests/suggestionCollector.test.ts @@ -202,3 +202,90 @@ test('Notes and folders are included in the same set of suggestions', () => { const observedSuggestions = collector.getSuggestions(suggestionContext) expect(observedSuggestions).toIncludeSameMembers(expectedSuggestions) }) + +describe('When using relative search', function () { + describe('and the active file is in the root folder', function () { + test('all files and folders in the vault are returned as suggestions', () => { + const folderPaths = [ + "", + "folder1", + "folder1/folder1.2", + "folder2" + ] + + const noteLinks = [ + Fake.LinkToExistingNote('folder1/folder1.2/note1'), + Fake.LinkToExistingNote('folder2/note2'), + Fake.LinkToExistingNote('my note'), + ] + + const interOp = Fake.Interop + .withMetadataCollection(Fake.MetaDataCollection.withLinkSuggestions(noteLinks)) + .withFileSystem(Fake.FileSystem.withFolders(folderPaths)) + + const settings = Fake.Settings + settings.includeFoldersInSuggestions = true + settings.folderSuggestionSettings = {folderSuggestionMode: FolderSuggestionMode.Always, folderSuggestionTrigger: "/"} + + const collector = new SuggestionCollector(interOp, settings) + + const query = '.' + + const expectedSuggestions: ISuggestion[] = [ + new FolderSuggestion(new ObsidianFolderPath("folder1")), + new FolderSuggestion(new ObsidianFolderPath("folder1/folder1.2")), + new FolderSuggestion(new ObsidianFolderPath("folder2")), + new ExistingNoteSuggestion('folder1/folder1.2/note1'), + new ExistingNoteSuggestion('folder2/note2'), + new ExistingNoteSuggestion('my note'), + ] + + const suggestionContext = Fake.EditorSuggestionContext(query).withFile(Fake.File('my note')) + + const observedSuggestions = collector.getSuggestions(suggestionContext) + expect(observedSuggestions).toIncludeSameMembers(expectedSuggestions) + }) + }) + + describe('and the active file is in a sub folder', function () { + test('only files and folders in the sub folder tree are returned as suggestions when', () => { + const folderPaths = [ + "", + "folder1", + "folder1/folder1.2", + "folder2" + ] + + const noteLinks = [ + Fake.LinkToExistingNote('folder1/folder1.2/note1'), + Fake.LinkToExistingNote('folder1/my other note'), + Fake.LinkToExistingNote('folder2/note2'), + Fake.LinkToExistingNote('my note'), + ] + + const interOp = Fake.Interop + .withMetadataCollection(Fake.MetaDataCollection.withLinkSuggestions(noteLinks)) + .withFileSystem(Fake.FileSystem.withFolders(folderPaths)) + + const settings = Fake.Settings + settings.includeFoldersInSuggestions = true + settings.folderSuggestionSettings = {folderSuggestionMode: FolderSuggestionMode.Always, folderSuggestionTrigger: "/"} + + const collector = new SuggestionCollector(interOp, settings) + + const query = '.' + + const expectedSuggestions: ISuggestion[] = [ + new FolderSuggestion(new ObsidianFolderPath("folder1")), + new FolderSuggestion(new ObsidianFolderPath("folder1/folder1.2")), + new ExistingNoteSuggestion('folder1/folder1.2/note1'), + new ExistingNoteSuggestion('folder1/my other note'), + ] + + const suggestionContext = Fake.EditorSuggestionContext(query).withFile(Fake.File('folder1/my other note')) + + const observedSuggestions = collector.getSuggestions(suggestionContext) + expect(observedSuggestions).toIncludeSameMembers(expectedSuggestions) + }) + }) +})