zotero/test/tests/notifierTest.js
Dan Stillman f1af54236e Add Zotero.Notifier.Queue to keep event groups separate, and use for sync
A queue can be created and passed as an option to data layer methods, which
will then queue events on that queue instead of the main internal queue. A
queue or an array of queues can then be passed to Zotero.Notifier.commit() to
commit those events.

Some auxiliary functions don't yet take a queue, so those events will still get
run on DB transaction commit.

Sync data processing now processes notifier events in batches to reduce
repaints, even though individual objects are processed within their own
transactions (so that failures don't roll back other objects' data).

Also remove some unused notifier code
2016-04-22 22:48:58 -04:00

60 lines
1.7 KiB
JavaScript

"use strict";
describe("Zotero.Notifier", function () {
describe("#trigger()", function () {
it("should trigger add events before modify events", function* () {
var deferred = Zotero.Promise.defer();
var events = [];
var observer = {
notify: (action, type, ids) => {
events.push(action);
if (events.length == 2) {
deferred.resolve();
}
}
};
var id = Zotero.Notifier.registerObserver(observer, null, 'test_trigger');
yield Zotero.DB.executeTransaction(function* () {
var item = new Zotero.Item('book');
item.setField('title', 'A');
yield item.save();
item.setField('title', 'B');
yield item.save();
Zotero.Notifier.queue('unknown', 'item', item.id);
});
assert.isTrue(deferred.promise.isResolved());
assert.lengthOf(events, 3);
assert.equal(events[0], 'add');
assert.equal(events[1], 'modify');
assert.equal(events[2], 'unknown');
Zotero.Notifier.unregisterObserver(id);
});
it("should add events to passed queue", function* () {
var collection = yield createDataObject('collection');
var deferred = Zotero.Promise.defer();
var observer = {
notify: () => deferred.resolve()
};
var id = Zotero.Notifier.registerObserver(observer, null, 'test_trigger');
var queue = new Zotero.Notifier.Queue;
var item = createUnsavedDataObject('item');
item.setCollections([collection.id]);
yield item.saveTx({
notifierQueue: queue
});
assert.isTrue(deferred.promise.isPending());
assert.equal(queue.size, 2);
yield Zotero.Notifier.commit(queue);
assert.isTrue(deferred.promise.isResolved());
Zotero.Notifier.unregisterObserver(id);
});
});
});