commit 3a995d64a47a431be6b95e9a773d86e478eb92db
parent 26e1372f46ca0222ffb7f73a1adb38dfea465292
Author: Dan Stillman <dstillman@zotero.org>
Date: Wed, 6 May 2015 04:18:24 -0400
Fix saving of tags
Diffstat:
2 files changed, 59 insertions(+), 3 deletions(-)
diff --git 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
@@ -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));
+ })
+ })
});