Fix race conditions in tag selector tests

And take an optional second parameter in waitForTagSelector() to
indicate how many updates to wait for, since certain operations trigger
two updates, one from notify() and the other from onItemViewChanged().
This commit is contained in:
Dan Stillman 2019-03-18 04:53:30 -04:00
parent 67febb2f45
commit a92b29cebf
2 changed files with 30 additions and 17 deletions

View file

@ -209,15 +209,26 @@ var waitForItemsLoad = Zotero.Promise.coroutine(function* (win, collectionRowToS
yield zp.itemsView.waitForLoad();
});
var waitForTagSelector = function (win) {
/**
* Return a promise that resolves once the tag selector has updated
*
* Some operations result in two tag selector updates, one from the notify() and another from
* onItemViewChanged(). Pass 2 for numUpdates to wait for both.
*/
var waitForTagSelector = function (win, numUpdates = 1) {
var updates = 0;
var zp = win.ZoteroPane;
var deferred = Zotero.Promise.defer();
if (zp.tagSelectorShown()) {
let tagSelector = zp.tagSelector;
let componentDidUpdate = tagSelector.componentDidUpdate;
tagSelector.componentDidUpdate = function() {
deferred.resolve();
tagSelector.componentDidUpdate = componentDidUpdate;
updates++;
if (updates == numUpdates) {
deferred.resolve();
tagSelector.componentDidUpdate = componentDidUpdate;
}
if (typeof componentDidUpdate == 'function') {
componentDidUpdate.call(this, arguments);
}