2015-05-04 09:59:40 +00:00
|
|
|
"use strict";
|
|
|
|
|
2015-04-26 06:44:29 +00:00
|
|
|
describe("Zotero.Collection", function() {
|
|
|
|
describe("#save()", function () {
|
|
|
|
it("should save a new collection", function* () {
|
|
|
|
var name = "Test";
|
|
|
|
var collection = new Zotero.Collection;
|
|
|
|
collection.name = name;
|
2015-05-10 08:20:47 +00:00
|
|
|
var id = yield collection.saveTx();
|
2015-04-26 06:44:29 +00:00
|
|
|
collection = yield Zotero.Collections.getAsync(id);
|
|
|
|
assert.equal(collection.name, name);
|
|
|
|
});
|
|
|
|
})
|
2015-05-04 09:59:40 +00:00
|
|
|
|
|
|
|
describe("#version", function () {
|
|
|
|
it("should set object version", function* () {
|
|
|
|
var version = 100;
|
|
|
|
var collection = new Zotero.Collection
|
|
|
|
collection.version = version;
|
|
|
|
collection.name = "Test";
|
2015-05-10 08:20:47 +00:00
|
|
|
var id = yield collection.saveTx();
|
2015-05-04 09:59:40 +00:00
|
|
|
collection = yield Zotero.Collections.getAsync(id);
|
|
|
|
assert.equal(collection.version, version);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
|
2015-05-05 18:08:28 +00:00
|
|
|
describe("#parentKey", function () {
|
2015-05-04 09:59:40 +00:00
|
|
|
it("should set parent collection for new collections", function* () {
|
|
|
|
var parentCol = new Zotero.Collection
|
|
|
|
parentCol.name = "Parent";
|
2015-05-10 08:20:47 +00:00
|
|
|
var parentID = yield parentCol.saveTx();
|
2015-05-04 09:59:40 +00:00
|
|
|
var {libraryID, key: parentKey} = Zotero.Collections.getLibraryAndKeyFromID(parentID);
|
|
|
|
|
|
|
|
var col = new Zotero.Collection
|
|
|
|
col.name = "Child";
|
|
|
|
col.parentKey = parentKey;
|
2015-05-10 08:20:47 +00:00
|
|
|
var id = yield col.saveTx();
|
2015-05-04 09:59:40 +00:00
|
|
|
col = yield Zotero.Collections.getAsync(id);
|
|
|
|
assert.equal(col.parentKey, parentKey);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should change parent collection for existing collections", function* () {
|
|
|
|
// Create initial parent collection
|
|
|
|
var parentCol = new Zotero.Collection
|
|
|
|
parentCol.name = "Parent";
|
2015-05-10 08:20:47 +00:00
|
|
|
var parentID = yield parentCol.saveTx();
|
2015-05-04 09:59:40 +00:00
|
|
|
var {libraryID, key: parentKey} = Zotero.Collections.getLibraryAndKeyFromID(parentID);
|
|
|
|
|
|
|
|
// Create subcollection
|
|
|
|
var col = new Zotero.Collection
|
|
|
|
col.name = "Child";
|
|
|
|
col.parentKey = parentKey;
|
2015-05-10 08:20:47 +00:00
|
|
|
var id = yield col.saveTx();
|
2015-05-04 09:59:40 +00:00
|
|
|
col = yield Zotero.Collections.getAsync(id);
|
|
|
|
|
|
|
|
// Create new parent collection
|
|
|
|
var newParentCol = new Zotero.Collection
|
|
|
|
newParentCol.name = "New Parent";
|
2015-05-10 08:20:47 +00:00
|
|
|
var newParentID = yield newParentCol.saveTx();
|
2015-05-04 09:59:40 +00:00
|
|
|
var {libraryID, key: newParentKey} = Zotero.Collections.getLibraryAndKeyFromID(newParentID);
|
|
|
|
|
|
|
|
// Change parent collection
|
|
|
|
col.parentKey = newParentKey;
|
2015-05-10 08:20:47 +00:00
|
|
|
yield col.saveTx();
|
2015-05-04 09:59:40 +00:00
|
|
|
col = yield Zotero.Collections.getAsync(id);
|
|
|
|
assert.equal(col.parentKey, newParentKey);
|
|
|
|
});
|
|
|
|
|
2015-05-05 18:08:28 +00:00
|
|
|
it("should not mark collection as unchanged if set to existing value", function* () {
|
|
|
|
// Create initial parent collection
|
|
|
|
var parentCol = new Zotero.Collection
|
|
|
|
parentCol.name = "Parent";
|
2015-05-10 08:20:47 +00:00
|
|
|
var parentID = yield parentCol.saveTx();
|
2015-05-05 18:08:28 +00:00
|
|
|
var {libraryID, key: parentKey} = Zotero.Collections.getLibraryAndKeyFromID(parentID);
|
|
|
|
|
|
|
|
// Create subcollection
|
|
|
|
var col = new Zotero.Collection
|
|
|
|
col.name = "Child";
|
|
|
|
col.parentKey = parentKey;
|
2015-05-10 08:20:47 +00:00
|
|
|
var id = yield col.saveTx();
|
2015-05-05 18:08:28 +00:00
|
|
|
col = yield Zotero.Collections.getAsync(id);
|
|
|
|
|
|
|
|
// Set to existing parent
|
|
|
|
col.parentKey = parentKey;
|
|
|
|
assert.isFalse(col.hasChanged());
|
|
|
|
});
|
|
|
|
|
2015-05-04 09:59:40 +00:00
|
|
|
it("should not resave a collection with no parent if set to false", function* () {
|
|
|
|
var col = new Zotero.Collection
|
|
|
|
col.name = "Test";
|
2015-05-10 08:20:47 +00:00
|
|
|
var id = yield col.saveTx();
|
2015-05-04 09:59:40 +00:00
|
|
|
col = yield Zotero.Collections.getAsync(id);
|
|
|
|
|
|
|
|
col.parentKey = false;
|
2015-05-10 08:20:47 +00:00
|
|
|
var ret = yield col.saveTx();
|
2015-05-04 09:59:40 +00:00
|
|
|
assert.isFalse(ret);
|
|
|
|
});
|
|
|
|
})
|
2015-04-26 06:44:29 +00:00
|
|
|
})
|