diff --git a/src/utils/BookmarksCorePluginSignature.spec.ts b/src/utils/BookmarksCorePluginSignature.spec.ts index a83d0eb..251477e 100644 --- a/src/utils/BookmarksCorePluginSignature.spec.ts +++ b/src/utils/BookmarksCorePluginSignature.spec.ts @@ -1063,3 +1063,106 @@ describe('cleanupBookmarkTreeFromTransparentEmptyGroups', () => { })))) }) }) + +describe('findGroupForItemPathInBookmarks', () =>{ + it('should return undefined if the matching container does not exist - case: no sortspec container', () => { + const items = consumeBkMock({}) + const plugin = getBookmarksMock(items) + const parentFolder: BookmarkedParentFolder|undefined = _unitTests.findGroupForItemPathInBookmarks( + 'inbox', + false, + plugin, + 'sortspec' + ) + expect(parentFolder).toBeUndefined() + }) + it('should return undefined if the matching container does not exist - case: no 2nd level container', () => { + const items = consumeBkMock({ + "sortspec": { + "\\\\group l1.1": { + "\\\\group l2.1": { + "\\\\group l3.1": { + "\\\\deepest 1": {} + } + }, + "\\\\group l2.2": { + "\\\\deepest 2": {} + } + }, + "\\\\group l1.2": { + "\\\\group l2.1": { + "\\\\deepest 3": {} + } + } + } + }) + const plugin = getBookmarksMock(items) + const parentFolder: BookmarkedParentFolder|undefined = _unitTests.findGroupForItemPathInBookmarks( + 'group l1.1/group l2.2/group l3.1/deepest 1', + false, + plugin, + 'sortspec' + ) + expect(parentFolder).toBeUndefined() + }) + it('should return the sortspec container for root level items', () => { + const items = consumeBkMock({ + "sortspec": {} + }) + const plugin = getBookmarksMock(items) + const parentFolder: BookmarkedParentFolder|undefined = _unitTests.findGroupForItemPathInBookmarks( + 'inbox', + false, + plugin, + 'sortspec' + ) + const expectedGroup = consumeBkMock({ + "sortspec": {} + }) + const expected = { + group: expectedGroup[0], + items: expectedGroup[0].items, + pathOfGroup: '' + } + expect(JSON.parse(JSON.stringify(parentFolder))).toEqual(JSON.parse(JSON.stringify(expected))) + }) + it('should return the correct 2nd level container', () => { + const items = consumeBkMock({ + "sortspec": { + "\\\\group l1.1": { + "\\\\group l2.1": { + "\\\\group l3.1": { + "\\\\deepest 1": {} + } + }, + "\\\\group l2.2": { + "\\\\deepest 2": {} + } + }, + "\\\\group l1.2": { + "\\\\group l2.1": { + "\\\\deepest 3": {} + } + } + } + }) + const plugin = getBookmarksMock(items) + const parentFolder: BookmarkedParentFolder|undefined = _unitTests.findGroupForItemPathInBookmarks( + 'group l1.2/group l2.1/deepest 3', + false, + plugin, + 'sortspec' + ) + const expectedGroup = consumeBkMock({ + "\\\\group l2.1": { + "\\\\deepest 3": {} + } + }) + const expected = { + group: expectedGroup[0], + items: expectedGroup[0].items, + pathOfGroup: 'group l1.2/group l2.1' + } + expect(JSON.parse(JSON.stringify(parentFolder))).toEqual(JSON.parse(JSON.stringify(expected))) + }) +}) diff --git a/src/utils/BookmarksCorePluginSignature.ts b/src/utils/BookmarksCorePluginSignature.ts index a2fff74..38e9697 100644 --- a/src/utils/BookmarksCorePluginSignature.ts +++ b/src/utils/BookmarksCorePluginSignature.ts @@ -450,27 +450,35 @@ const findGroupForItemPathInBookmarks = (itemPath: string, createIfMissing: bool } let group: BookmarkedGroup|undefined = undefined + let failed: boolean = false + let firstItem: boolean = true - parentPathComponents.forEach((pathSegment, index) => { + for (let pathSegment of parentPathComponents) { group = items.find((it) => it.type === 'group' && groupNameForPath(it.title||'') === pathSegment) as BookmarkedGroup if (!group) { if (createIfMissing) { - const theSortingBookmarksContainerGroup = (bookmarksGroup && index === 0) + const theSortingBookmarksContainerGroup = !!bookmarksGroup && firstItem const groupName: string = theSortingBookmarksContainerGroup ? pathSegment : groupNameTransparentForSorting(pathSegment) group = createBookmarkGroupEntry(groupName) items.push(group) } else { - return undefined + failed = true + break } + firstItem = false } items = group.items - }) + } - return { - items: items, - group: group, - pathOfGroup: parentPath + if (failed) { + return undefined + } else { + return { + items: items, + group: group, + pathOfGroup: parentPath + } } }