diff --git a/chrome/content/zotero/containers/tagSelector.jsx b/chrome/content/zotero/containers/tagSelector.jsx index e6d4197fd6..39b709637c 100644 --- a/chrome/content/zotero/containers/tagSelector.jsx +++ b/chrome/content/zotero/containers/tagSelector.jsx @@ -74,17 +74,21 @@ Zotero.TagSelector = class TagSelectorContainer extends React.Component { if (type == 'item' && (event == 'trash')) { return this.setState({tags: await this.getTags()}); } - + // If a selected tag no longer exists, deselect it - if (type == 'item-tag') { - if (event == 'delete' || event == 'trash' || event == 'modify') { - for (let tag of this.selectedTags) { - if (tag == extraData[ids[0]].old.tag) { - this.selectedTags.delete(tag); - } + if (type == 'tag' && (event == 'modify' || event == 'delete')) { + let changed = false; + for (let id of ids) { + let tag = extraData[id].old.tag; + if (this.selectedTags.has(tag)) { + this.selectedTags.delete(tag); + changed = true; } } - return this.setState({tags: await this.getTags()}); + if (changed && typeof(this.props.onSelection) === 'function') { + this.props.onSelection(this.selectedTags); + } + return; } this.setState({tags: await this.getTags()}); diff --git a/test/tests/tagSelectorTest.js b/test/tests/tagSelectorTest.js index ce56b28694..b75121848d 100644 --- a/test/tests/tagSelectorTest.js +++ b/test/tests/tagSelectorTest.js @@ -189,32 +189,36 @@ describe("Tag Selector", function () { describe("#notify()", function () { - it("should add a tag when added to an item in the library root", function* () { + it("should add a tag when added to an item in the library root", async function () { var promise; if (collectionsView.selection.currentIndex != 0) { promise = waitForTagSelector(win); - yield collectionsView.selectLibrary(); - yield promise; + await collectionsView.selectLibrary(); + await promise; } // Add item with tag to library root - var item = createUnsavedDataObject('item'); + promise = waitForTagSelector(win); + var item = await createDataObject('item'); + await promise + + var tagA = Zotero.Utilities.randomString(); + var tagB = Zotero.Utilities.randomString(); item.setTags([ { - tag: 'A' + tag: tagA }, { - tag: 'B', + tag: tagB, type: 1 } ]); promise = waitForTagSelector(win); - yield item.saveTx(); - yield promise; + await item.saveTx(); + await promise; - // Tag selector should have at least one tag - assert.isAbove(getRegularTags().length, 1); + assert.includeMembers(getRegularTags(), [tagA, tagB]); }); it("should add a tag when an item is added in a collection", function* () { @@ -394,7 +398,7 @@ describe("Tag Selector", function () { yield item.saveTx(); yield promise; - // Tag selector should show the new item's tag + // Tag selector should show the new tag assert.include(getRegularTags(), "A"); // Remove tag from library @@ -404,8 +408,38 @@ describe("Tag Selector", function () { yield tagSelector.openDeletePrompt(); yield promise; - // Tag selector shouldn't show the deleted item's tag + // Tag selector shouldn't show the deleted tag assert.notInclude(getRegularTags(), "A"); + }); + + it("should deselect a tag when deleted from a library", async function () { + var libraryID = Zotero.Libraries.userLibraryID; + await selectLibrary(win); + + var promise = waitForTagSelector(win); + + var item1 = await createDataObject('item', { tags: [{ tag: 'A' }] }); + var item2 = await createDataObject('item', { tags: [{ tag: 'B' }] }); + await promise; + + tagSelector.handleTagSelected('A'); + await waitForTagSelector(win); + + // Tag selector should show the selected tag + assert.include(getRegularTags(), 'A'); + // And not the unselected one + assert.notInclude(getRegularTags(), 'B'); + + // Remove tag from library + promise = waitForTagSelector(win); + await Zotero.Tags.removeFromLibrary(libraryID, Zotero.Tags.getID('A')); + await promise; + + // Deleted tag should no longer be shown or selected + assert.notInclude(getRegularTags(), 'A'); + assert.notInclude(Array.from(tagSelector.getTagSelection()), 'A'); + // Other tags should be shown again + assert.include(getRegularTags(), 'B'); }) })