2015-04-30 21:06:38 +00:00
|
|
|
describe("Zotero.ItemTreeView", function() {
|
2015-06-09 05:29:06 +00:00
|
|
|
var win, zp, itemsView, existingItemID;
|
2015-04-30 21:06:38 +00:00
|
|
|
|
|
|
|
// Load Zotero pane and select library
|
|
|
|
before(function* () {
|
|
|
|
win = yield loadZoteroPane();
|
2015-06-09 05:29:06 +00:00
|
|
|
zp = win.ZoteroPane;
|
2015-04-30 21:06:38 +00:00
|
|
|
|
|
|
|
var item = new Zotero.Item('book');
|
2015-05-10 08:20:47 +00:00
|
|
|
existingItemID = yield item.saveTx();
|
2015-04-30 21:06:38 +00:00
|
|
|
});
|
2015-06-09 05:29:06 +00:00
|
|
|
beforeEach(function* () {
|
|
|
|
yield zp.collectionsView.selectLibrary();
|
|
|
|
yield waitForItemsLoad(win)
|
|
|
|
itemsView = zp.itemsView;
|
|
|
|
})
|
2015-04-30 21:06:38 +00:00
|
|
|
after(function () {
|
2015-05-04 06:00:52 +00:00
|
|
|
win.close();
|
2015-04-30 21:06:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("#selectItem()", function () {
|
|
|
|
/**
|
|
|
|
* Make sure that selectItem() doesn't hang if the pane's item-select handler is never
|
|
|
|
* triggered due to the item already being selected
|
|
|
|
*/
|
|
|
|
it("should return if item is already selected", function* () {
|
|
|
|
yield itemsView.selectItem(existingItemID);
|
2015-05-05 06:40:01 +00:00
|
|
|
var selected = itemsView.getSelectedItems(true);
|
2015-04-30 21:06:38 +00:00
|
|
|
assert.lengthOf(selected, 1);
|
2015-05-05 06:40:01 +00:00
|
|
|
assert.equal(selected[0], existingItemID);
|
2015-04-30 21:06:38 +00:00
|
|
|
yield itemsView.selectItem(existingItemID);
|
2015-05-05 06:40:01 +00:00
|
|
|
selected = itemsView.getSelectedItems(true);
|
2015-04-30 21:06:38 +00:00
|
|
|
assert.lengthOf(selected, 1);
|
2015-05-05 06:40:01 +00:00
|
|
|
assert.equal(selected[0], existingItemID);
|
2015-04-30 21:06:38 +00:00
|
|
|
});
|
|
|
|
})
|
|
|
|
|
|
|
|
describe("#notify()", function () {
|
2015-05-07 19:05:37 +00:00
|
|
|
beforeEach(function () {
|
|
|
|
sinon.spy(win.ZoteroPane, "itemSelected");
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
win.ZoteroPane.itemSelected.restore();
|
|
|
|
})
|
|
|
|
|
2015-04-30 21:06:38 +00:00
|
|
|
it("should select a new item", function* () {
|
|
|
|
itemsView.selection.clearSelection();
|
|
|
|
assert.lengthOf(itemsView.getSelectedItems(), 0);
|
|
|
|
|
2015-05-07 19:05:37 +00:00
|
|
|
assert.equal(win.ZoteroPane.itemSelected.callCount, 1);
|
|
|
|
|
2015-04-30 21:06:38 +00:00
|
|
|
// Create item
|
|
|
|
var item = new Zotero.Item('book');
|
2015-05-10 08:20:47 +00:00
|
|
|
var id = yield item.saveTx();
|
2015-04-30 21:06:38 +00:00
|
|
|
|
|
|
|
// New item should be selected
|
|
|
|
var selected = itemsView.getSelectedItems();
|
|
|
|
assert.lengthOf(selected, 1);
|
|
|
|
assert.equal(selected[0].id, id);
|
2015-05-07 19:05:37 +00:00
|
|
|
|
|
|
|
// Item should have been selected once
|
|
|
|
assert.equal(win.ZoteroPane.itemSelected.callCount, 2);
|
|
|
|
assert.ok(win.ZoteroPane.itemSelected.returnValues[1].value());
|
2015-04-30 21:06:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("shouldn't select a new item if skipNotifier is passed", function* () {
|
|
|
|
// Select existing item
|
|
|
|
yield itemsView.selectItem(existingItemID);
|
2015-05-05 06:40:01 +00:00
|
|
|
var selected = itemsView.getSelectedItems(true);
|
2015-04-30 21:06:38 +00:00
|
|
|
assert.lengthOf(selected, 1);
|
2015-05-05 06:40:01 +00:00
|
|
|
assert.equal(selected[0], existingItemID);
|
2015-04-30 21:06:38 +00:00
|
|
|
|
2015-05-07 19:05:37 +00:00
|
|
|
// Reset call count on spy
|
|
|
|
win.ZoteroPane.itemSelected.reset();
|
|
|
|
|
2015-04-30 21:06:38 +00:00
|
|
|
// Create item with skipNotifier flag
|
|
|
|
var item = new Zotero.Item('book');
|
2015-05-10 08:20:47 +00:00
|
|
|
var id = yield item.saveTx({
|
2015-04-30 21:06:38 +00:00
|
|
|
skipNotifier: true
|
|
|
|
});
|
|
|
|
|
2015-05-07 19:05:37 +00:00
|
|
|
// No select events should have occurred
|
|
|
|
assert.equal(win.ZoteroPane.itemSelected.callCount, 0);
|
|
|
|
|
2015-04-30 21:06:38 +00:00
|
|
|
// Existing item should still be selected
|
2015-05-05 06:40:01 +00:00
|
|
|
selected = itemsView.getSelectedItems(true);
|
2015-04-30 21:06:38 +00:00
|
|
|
assert.lengthOf(selected, 1);
|
2015-05-05 06:40:01 +00:00
|
|
|
assert.equal(selected[0], existingItemID);
|
2015-04-30 21:06:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("shouldn't select a new item if skipSelect is passed", function* () {
|
|
|
|
// Select existing item
|
|
|
|
yield itemsView.selectItem(existingItemID);
|
2015-05-05 06:40:01 +00:00
|
|
|
var selected = itemsView.getSelectedItems(true);
|
2015-04-30 21:06:38 +00:00
|
|
|
assert.lengthOf(selected, 1);
|
2015-05-05 06:40:01 +00:00
|
|
|
assert.equal(selected[0], existingItemID);
|
2015-04-30 21:06:38 +00:00
|
|
|
|
2015-05-07 19:05:37 +00:00
|
|
|
// Reset call count on spy
|
|
|
|
win.ZoteroPane.itemSelected.reset();
|
|
|
|
|
2015-04-30 21:06:38 +00:00
|
|
|
// Create item with skipSelect flag
|
|
|
|
var item = new Zotero.Item('book');
|
2015-05-10 08:20:47 +00:00
|
|
|
var id = yield item.saveTx({
|
2015-04-30 21:06:38 +00:00
|
|
|
skipSelect: true
|
|
|
|
});
|
|
|
|
|
2015-05-07 19:05:37 +00:00
|
|
|
// itemSelected should have been called once (from 'selectEventsSuppressed = false'
|
|
|
|
// in notify()) as a no-op
|
|
|
|
assert.equal(win.ZoteroPane.itemSelected.callCount, 1);
|
|
|
|
assert.isFalse(win.ZoteroPane.itemSelected.returnValues[0].value());
|
|
|
|
|
2015-04-30 21:06:38 +00:00
|
|
|
// Existing item should still be selected
|
|
|
|
selected = itemsView.getSelectedItems(true);
|
|
|
|
assert.lengthOf(selected, 1);
|
|
|
|
assert.equal(selected[0], existingItemID);
|
|
|
|
});
|
|
|
|
|
2015-06-23 20:56:10 +00:00
|
|
|
it("shouldn't change selection outside of trash if new trashed item is created with skipSelect", function* () {
|
|
|
|
yield selectLibrary(win);
|
|
|
|
yield waitForItemsLoad(win);
|
|
|
|
|
|
|
|
itemsView.selection.clearSelection();
|
|
|
|
|
|
|
|
var item = createUnsavedDataObject('item');
|
|
|
|
item.deleted = true;
|
|
|
|
var id = yield item.saveTx({
|
|
|
|
skipSelect: true
|
|
|
|
});
|
|
|
|
|
|
|
|
// Nothing should be selected
|
|
|
|
selected = itemsView.getSelectedItems(true);
|
|
|
|
assert.lengthOf(selected, 0);
|
|
|
|
})
|
|
|
|
|
2015-04-30 21:06:38 +00:00
|
|
|
it("shouldn't select a modified item", function* () {
|
|
|
|
// Create item
|
|
|
|
var item = new Zotero.Item('book');
|
2015-05-10 08:20:47 +00:00
|
|
|
var id = yield item.saveTx();
|
2015-04-30 21:06:38 +00:00
|
|
|
|
|
|
|
itemsView.selection.clearSelection();
|
|
|
|
assert.lengthOf(itemsView.getSelectedItems(), 0);
|
2015-05-07 19:05:37 +00:00
|
|
|
// Reset call count on spy
|
|
|
|
win.ZoteroPane.itemSelected.reset();
|
2015-04-30 21:06:38 +00:00
|
|
|
|
2015-05-07 19:05:37 +00:00
|
|
|
// Modify item
|
2015-04-30 21:06:38 +00:00
|
|
|
item.setField('title', 'no select on modify');
|
2015-05-10 08:20:47 +00:00
|
|
|
yield item.saveTx();
|
2015-04-30 21:06:38 +00:00
|
|
|
|
2015-05-07 19:05:37 +00:00
|
|
|
// itemSelected should have been called once (from 'selectEventsSuppressed = false'
|
|
|
|
// in notify()) as a no-op
|
|
|
|
assert.equal(win.ZoteroPane.itemSelected.callCount, 1);
|
|
|
|
assert.isFalse(win.ZoteroPane.itemSelected.returnValues[0].value());
|
|
|
|
|
2015-04-30 21:06:38 +00:00
|
|
|
// Modified item should not be selected
|
|
|
|
assert.lengthOf(itemsView.getSelectedItems(), 0);
|
|
|
|
});
|
|
|
|
|
2015-05-07 19:05:37 +00:00
|
|
|
it("should maintain selection on a selected modified item", function* () {
|
2015-04-30 21:06:38 +00:00
|
|
|
// Create item
|
|
|
|
var item = new Zotero.Item('book');
|
2015-05-10 08:20:47 +00:00
|
|
|
var id = yield item.saveTx();
|
2015-04-30 21:06:38 +00:00
|
|
|
|
|
|
|
yield itemsView.selectItem(id);
|
|
|
|
var selected = itemsView.getSelectedItems(true);
|
|
|
|
assert.lengthOf(selected, 1);
|
|
|
|
assert.equal(selected[0], id);
|
|
|
|
|
2015-05-07 19:05:37 +00:00
|
|
|
// Reset call count on spy
|
|
|
|
win.ZoteroPane.itemSelected.reset();
|
|
|
|
|
|
|
|
// Modify item
|
|
|
|
item.setField('title', 'maintain selection on modify');
|
2015-05-10 08:20:47 +00:00
|
|
|
yield item.saveTx();
|
2015-04-30 21:06:38 +00:00
|
|
|
|
2015-05-07 19:05:37 +00:00
|
|
|
// itemSelected should have been called once (from 'selectEventsSuppressed = false'
|
|
|
|
// in notify()) as a no-op
|
|
|
|
assert.equal(win.ZoteroPane.itemSelected.callCount, 1);
|
|
|
|
assert.isFalse(win.ZoteroPane.itemSelected.returnValues[0].value());
|
|
|
|
|
2015-04-30 21:06:38 +00:00
|
|
|
// Modified item should still be selected
|
|
|
|
selected = itemsView.getSelectedItems(true);
|
|
|
|
assert.lengthOf(selected, 1);
|
|
|
|
assert.equal(selected[0], id);
|
|
|
|
});
|
2015-06-09 05:29:06 +00:00
|
|
|
|
|
|
|
it("should reselect the same row when an item is removed", function* () {
|
|
|
|
var collection = yield createDataObject('collection');
|
|
|
|
yield waitForItemsLoad(win);
|
|
|
|
itemsView = zp.itemsView;
|
|
|
|
|
|
|
|
var items = [];
|
|
|
|
var num = 10;
|
|
|
|
for (let i = 0; i < num; i++) {
|
|
|
|
let item = createUnsavedDataObject('item');
|
|
|
|
item.addToCollection(collection.id);
|
|
|
|
yield item.saveTx();
|
|
|
|
items.push(item);
|
|
|
|
}
|
|
|
|
yield Zotero.Promise.delay(2000);
|
|
|
|
assert.equal(itemsView.rowCount, num);
|
|
|
|
|
|
|
|
// Select the third item in the list
|
|
|
|
itemsView.selection.select(2);
|
|
|
|
var treeRow = itemsView.getRow(2);
|
|
|
|
yield treeRow.ref.eraseTx();
|
|
|
|
|
|
|
|
// Selection should stay on third row
|
|
|
|
assert.equal(itemsView.selection.currentIndex, 2);
|
|
|
|
|
|
|
|
yield Zotero.Items.erase(items.map(item => item.id));
|
|
|
|
})
|
2015-04-30 21:06:38 +00:00
|
|
|
})
|
2015-05-23 08:25:47 +00:00
|
|
|
|
|
|
|
describe("#drop()", function () {
|
|
|
|
it("should create a top-level attachment when a file is dragged", function* () {
|
|
|
|
var file = getTestDataDirectory();
|
|
|
|
file.append('test.png');
|
|
|
|
|
|
|
|
var deferred = Zotero.Promise.defer();
|
|
|
|
itemsView.addEventListener('select', () => deferred.resolve());
|
|
|
|
|
|
|
|
itemsView.drop(0, -1, {
|
|
|
|
dropEffect: 'copy',
|
|
|
|
effectAllowed: 'copy',
|
|
|
|
types: {
|
|
|
|
contains: function (type) {
|
|
|
|
return type == 'application/x-moz-file';
|
|
|
|
}
|
|
|
|
},
|
|
|
|
mozItemCount: 1,
|
|
|
|
mozGetDataAt: function (type, i) {
|
|
|
|
if (type == 'application/x-moz-file' && i == 0) {
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
yield deferred.promise;
|
|
|
|
var items = itemsView.getSelectedItems();
|
|
|
|
var path = yield items[0].getFilePathAsync();
|
|
|
|
assert.equal(
|
|
|
|
(yield Zotero.File.getBinaryContentsAsync(path)),
|
|
|
|
(yield Zotero.File.getBinaryContentsAsync(file))
|
|
|
|
);
|
|
|
|
})
|
|
|
|
});
|
2015-04-30 21:06:38 +00:00
|
|
|
})
|