Fix "Add Item by Identifier" (broken by 78b1d2ee3)

And make lookup tests run when not on Travis, though we should still mock the
HTTP requests.
This commit is contained in:
Dan Stillman 2016-12-13 05:23:21 -05:00
parent 69430d74b2
commit 7fc352b9b7
2 changed files with 74 additions and 63 deletions

View file

@ -202,6 +202,7 @@ Zotero.Translate.Sandbox = {
// TODO: This used to only be used for some modes. Since it's now used for everything with // TODO: This used to only be used for some modes. Since it's now used for everything with
// async saving, there's probably a bunch of code for the non-queued mode that can be removed. // async saving, there's probably a bunch of code for the non-queued mode that can be removed.
translate.saveQueue.push(item); translate.saveQueue.push(item);
translate._savingItems++;
}, },
/** /**
@ -1539,71 +1540,74 @@ Zotero.Translate.Base.prototype = {
* Saves items to the database, taking care to defer attachmentProgress notifications * Saves items to the database, taking care to defer attachmentProgress notifications
* until after save * until after save
*/ */
_saveItems: function (items) { _saveItems: Zotero.Promise.coroutine(function* (items) {
var itemDoneEventsDispatched = false; var itemDoneEventsDispatched = false;
var deferredProgress = []; var deferredProgress = [];
var attachmentsWithProgress = []; var attachmentsWithProgress = [];
this._savingItems++; try {
return this._itemSaver.saveItems( var newItems = yield this._itemSaver.saveItems(
items.slice(), items.slice(),
function (attachment, progress, error) { function (attachment, progress, error) {
var attachmentIndex = this._savingAttachments.indexOf(attachment); var attachmentIndex = this._savingAttachments.indexOf(attachment);
if(progress === false || progress === 100) { if(progress === false || progress === 100) {
if(attachmentIndex !== -1) { if(attachmentIndex !== -1) {
this._savingAttachments.splice(attachmentIndex, 1); this._savingAttachments.splice(attachmentIndex, 1);
}
} else if(attachmentIndex === -1) {
this._savingAttachments.push(attachment);
} }
} else if(attachmentIndex === -1) {
this._savingAttachments.push(attachment); if(itemDoneEventsDispatched) {
} // itemDone event has already fired, so we can fire attachmentProgress
// notifications
if(itemDoneEventsDispatched) { this._runHandler("attachmentProgress", attachment, progress, error);
// itemDone event has already fired, so we can fire attachmentProgress this._checkIfDone();
// notifications } else {
this._runHandler("attachmentProgress", attachment, progress, error); // Defer until after we fire the itemDone event
this._checkIfDone(); deferredProgress.push([attachment, progress, error]);
} else { attachmentsWithProgress.push(attachment);
// Defer until after we fire the itemDone event
deferredProgress.push([attachment, progress, error]);
attachmentsWithProgress.push(attachment);
}
}.bind(this)
)
.then(function (newItems) {
// Remove attachments not being saved from item.attachments
for(var i=0; i<items.length; i++) {
var item = items[i];
for(var j=0; j<item.attachments.length; j++) {
if(attachmentsWithProgress.indexOf(item.attachments[j]) === -1) {
item.attachments.splice(j--, 1);
} }
} }.bind(this)
} )
}
// Trigger itemDone events catch (e) {
for(var i=0, nItems = items.length; i<nItems; i++) { this._savingItems -= items.length;
this._runHandler("itemDone", newItems[i], items[i]); Zotero.debug("REDUCING SAVING ITEMS ERROR TO " + this._savingItems);
}
// Specify that itemDone event was dispatched, so that we don't defer
// attachmentProgress notifications anymore
itemDoneEventsDispatched = true;
// Run deferred attachmentProgress notifications
for(var i=0; i<deferredProgress.length; i++) {
this._runHandler("attachmentProgress", deferredProgress[i][0],
deferredProgress[i][1], deferredProgress[i][2]);
}
this.newItems = this.newItems.concat(newItems);
this._savingItems--;
this._checkIfDone();
}.bind(this))
.catch(function (e) {
Zotero.logError(e); Zotero.logError(e);
this.complete(false, e); this.complete(false, e);
}.bind(this)); return;
}, }
// Remove attachments not being saved from item.attachments
for(var i=0; i<items.length; i++) {
var item = items[i];
for(var j=0; j<item.attachments.length; j++) {
if(attachmentsWithProgress.indexOf(item.attachments[j]) === -1) {
item.attachments.splice(j--, 1);
}
}
}
// Trigger itemDone events
for(var i=0, nItems = items.length; i<nItems; i++) {
this._runHandler("itemDone", newItems[i], items[i]);
}
// Specify that itemDone event was dispatched, so that we don't defer
// attachmentProgress notifications anymore
itemDoneEventsDispatched = true;
// Run deferred attachmentProgress notifications
for(var i=0; i<deferredProgress.length; i++) {
this._runHandler("attachmentProgress", deferredProgress[i][0],
deferredProgress[i][1], deferredProgress[i][2]);
}
this._savingItems -= items.length;
this.newItems = this.newItems.concat(newItems);
this._checkIfDone();
}),
/** /**
* Checks if saving done, and if so, fires done event * Checks if saving done, and if so, fires done event

View file

@ -1,19 +1,26 @@
function lookupIdentifier(win, identifier) { var lookupIdentifier = Zotero.Promise.coroutine(function* (win, identifier) {
var textbox = win.document.getElementById("zotero-lookup-textbox"); var textbox = win.document.getElementById("zotero-lookup-textbox");
textbox.value = identifier; textbox.value = identifier;
win.Zotero_Lookup.accept(textbox); var promise = waitForItemEvent("add");
return waitForItemEvent("add"); yield win.Zotero_Lookup.accept(textbox);
} return promise;
});
describe.skip("Add Item by Identifier", function() { describe("Add Item by Identifier", function() {
var win; var win;
before(function* () { before(function* () {
if (Zotero.automatedTest) {
this.skip();
return;
}
win = yield loadZoteroPane(); win = yield loadZoteroPane();
}); });
after(function() { after(function() {
win.close(); if (win) {
win.close();
}
}); });
// TODO: mock external services: https://github.com/zotero/zotero/issues/699 // TODO: mock external services: https://github.com/zotero/zotero/issues/699