From 258b70b455c3c23743ef1f1014a7d401a1792d57 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Mon, 20 Jul 2015 04:14:51 -0400 Subject: [PATCH] Throw an error saving an empty item with no item type --- chrome/content/zotero/xpcom/data/item.js | 10 +++++++--- test/tests/itemTest.js | 9 +++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/chrome/content/zotero/xpcom/data/item.js b/chrome/content/zotero/xpcom/data/item.js index 4fd4f0fcd..d283c4f1a 100644 --- a/chrome/content/zotero/xpcom/data/item.js +++ b/chrome/content/zotero/xpcom/data/item.js @@ -1082,6 +1082,13 @@ Zotero.Item.prototype.isEditable = function() { return true; } +Zotero.Item.prototype._initSave = Zotero.Promise.coroutine(function* (env) { + if (!this.itemTypeID) { + throw new Error("Item type must be set before saving"); + } + return Zotero.Item._super.prototype._initSave.apply(this, arguments); +}) + Zotero.Item.prototype._saveData = Zotero.Promise.coroutine(function* (env) { Zotero.DB.requireTransaction(); @@ -1090,9 +1097,6 @@ Zotero.Item.prototype._saveData = Zotero.Promise.coroutine(function* (env) { var libraryType = env.libraryType = Zotero.Libraries.getType(env.libraryID); var itemTypeID = this.itemTypeID; - if (!itemTypeID) { - throw new Error("Item type must be set before saving"); - } var reloadParentChildItems = {}; diff --git a/test/tests/itemTest.js b/test/tests/itemTest.js index 069d9e76a..ec55a7977 100644 --- a/test/tests/itemTest.js +++ b/test/tests/itemTest.js @@ -682,6 +682,15 @@ describe("Zotero.Item", function () { }) }) + describe("#save()", function () { + it("should throw an error for an empty item without an item type", function* () { + var item = new Zotero.Item; + var e = yield getPromiseError(item.saveTx()); + assert.ok(e); + assert.equal(e.message, "Item type must be set before saving"); + }) + }) + describe("#clone()", function () { // TODO: Expand to other data it("should copy creators", function* () {