diff --git a/chrome/content/zotero/xpcom/data/item.js b/chrome/content/zotero/xpcom/data/item.js index c487766128..90d4992d42 100644 --- a/chrome/content/zotero/xpcom/data/item.js +++ b/chrome/content/zotero/xpcom/data/item.js @@ -2744,7 +2744,7 @@ Zotero.Item.prototype._updateAttachmentStates = function (exists) { return; } - if (item._bestAttachmentState?.key && this.key === item._bestAttachmentState.key) { + if (!this.deleted && item._bestAttachmentState?.key && this.key === item._bestAttachmentState.key) { item._bestAttachmentState.exists = exists; } else { diff --git a/test/tests/itemTest.js b/test/tests/itemTest.js index 7bd8dcff97..985d70c331 100644 --- a/test/tests/itemTest.js +++ b/test/tests/itemTest.js @@ -1280,6 +1280,27 @@ describe("Zotero.Item", function () { { type: 'image', exists: true, key: childItem.key } ); }); + + it("should update best attachment state when attachment is trashed", async function () { + var parentItem = await createDataObject('item'); + var file = getTestDataDirectory(); + file.append('test.png'); + var childItem = await Zotero.Attachments.importFromFile({ + file, + parentItemID: parentItem.id + }); + + await parentItem.getBestAttachmentState(); + childItem._updateAttachmentStates(true); + assert.deepEqual( + parentItem.getBestAttachmentStateCached(), + { type: 'image', exists: true, key: childItem.key } + ); + + await Zotero.Items.trashTx([childItem.id]); + childItem._updateAttachmentStates(true); + assert.deepEqual(parentItem.getBestAttachmentStateCached(), { type: null }); + }); });