diff --git a/chrome/content/zotero/xpcom/data/item.js b/chrome/content/zotero/xpcom/data/item.js index 4ea868c33..cdd69197a 100644 --- a/chrome/content/zotero/xpcom/data/item.js +++ b/chrome/content/zotero/xpcom/data/item.js @@ -1587,8 +1587,8 @@ Zotero.Item.prototype._saveData = Zotero.Promise.coroutine(function* (env) { } // Tags - if (this._changed.tags && this._previousData.tags) { - let oldTags = this._previousData.tags; + if (this._changed.tags) { + let oldTags = this._previousData.tags || []; let newTags = this._tags; // Convert to individual JSON objects, diff, and convert back diff --git a/test/tests/itemTest.js b/test/tests/itemTest.js index e65979278..f599caae5 100644 --- a/test/tests/itemTest.js +++ b/test/tests/itemTest.js @@ -261,5 +261,61 @@ describe("Zotero.Item", function () { file.append(filename); assert.equal(item.getFile().path, file.path); }); - }); + }) + + describe("#setTags", function () { + it("should save an array of tags in API JSON format", function* () { + var tags = [ + { + tag: "A" + }, + { + tag: "B" + } + ]; + var item = new Zotero.Item('journalArticle'); + item.setTags(tags); + var id = yield item.save(); + item = yield Zotero.Items.getAsync(id); + yield item.loadTags(); + assert.sameDeepMembers(item.getTags(tags), tags); + }) + + it("shouldn't mark item as changed if tags haven't changed", function* () { + var tags = [ + { + tag: "A" + }, + { + tag: "B" + } + ]; + var item = new Zotero.Item('journalArticle'); + item.setTags(tags); + var id = yield item.save(); + item = yield Zotero.Items.getAsync(id); + yield item.loadTags(); + item.setTags(tags); + assert.isFalse(item.hasChanged()); + }) + + it("should remove an existing tag", function* () { + var tags = [ + { + tag: "A" + }, + { + tag: "B" + } + ]; + var item = new Zotero.Item('journalArticle'); + item.setTags(tags); + var id = yield item.save(); + item = yield Zotero.Items.getAsync(id); + yield item.loadTags(); + item.setTags(tags.slice(0)); + yield item.save(); + assert.sameDeepMembers(item.getTags(tags), tags.slice(0)); + }) + }) });