2015-05-01 16:41:41 +00:00
|
|
|
describe("Zotero.Item", function () {
|
2015-04-25 07:14:53 +00:00
|
|
|
describe("#getField()", function () {
|
|
|
|
it("should return false for valid unset fields on unsaved items", function* () {
|
|
|
|
var item = new Zotero.Item('book');
|
|
|
|
assert.equal(item.getField('rights'), false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return false for valid unset fields on unsaved items after setting on another field", function* () {
|
|
|
|
var item = new Zotero.Item('book');
|
|
|
|
item.setField('title', 'foo');
|
|
|
|
assert.equal(item.getField('rights'), false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return false for invalid unset fields on unsaved items after setting on another field", function* () {
|
|
|
|
var item = new Zotero.Item('book');
|
|
|
|
item.setField('title', 'foo');
|
|
|
|
assert.equal(item.getField('invalid'), false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-01 16:41:41 +00:00
|
|
|
describe("#setField", function () {
|
2015-05-06 08:16:54 +00:00
|
|
|
it("should throw an error if item type isn't set", function* () {
|
|
|
|
var item = new Zotero.Item;
|
|
|
|
assert.throws(item.setField.bind(item, 'title', 'test'), "Item type must be set before setting field data");
|
|
|
|
})
|
|
|
|
|
2015-05-05 08:45:48 +00:00
|
|
|
it("should mark a field as changed", function () {
|
|
|
|
var item = new Zotero.Item('book');
|
|
|
|
item.setField('title', 'Foo');
|
|
|
|
assert.ok(item._changed.itemData[Zotero.ItemFields.getID('title')]);
|
|
|
|
assert.ok(item.hasChanged());
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should clear an existing field set to a falsy value", function () {
|
|
|
|
var field = 'title';
|
|
|
|
var fieldID = Zotero.ItemFields.getID(field);
|
|
|
|
var item = new Zotero.Item('book');
|
|
|
|
item.setField(field, 'Foo');
|
2015-05-10 08:20:47 +00:00
|
|
|
id = yield item.saveTx();
|
2015-05-05 08:45:48 +00:00
|
|
|
item = yield Zotero.Items.getAsync(id);
|
|
|
|
|
|
|
|
item.setField(field, "");
|
|
|
|
assert.ok(item._changed.itemData[fieldID]);
|
|
|
|
assert.ok(item.hasChanged());
|
|
|
|
yield item.reload();
|
|
|
|
|
|
|
|
assert.isFalse(item.hasChanged());
|
|
|
|
|
|
|
|
item.setField(field, false);
|
|
|
|
assert.ok(item._changed.itemData[fieldID]);
|
|
|
|
assert.ok(item.hasChanged());
|
|
|
|
yield item.reload();
|
|
|
|
|
|
|
|
assert.isFalse(item.hasChanged());
|
|
|
|
|
|
|
|
item.setField(field, null);
|
|
|
|
assert.ok(item._changed.itemData[fieldID]);
|
|
|
|
assert.ok(item.hasChanged());
|
|
|
|
|
2015-05-10 08:20:47 +00:00
|
|
|
yield item.saveTx();
|
2015-05-05 08:45:48 +00:00
|
|
|
assert.isFalse(item.getField(fieldID));
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should not mark an empty field set to an empty string as changed", function () {
|
|
|
|
var item = new Zotero.Item('book');
|
|
|
|
item.setField('url', '');
|
|
|
|
assert.isUndefined(item._changed.itemData);
|
|
|
|
})
|
|
|
|
|
2015-05-01 16:41:41 +00:00
|
|
|
it("should save version as object version", function* () {
|
|
|
|
var item = new Zotero.Item('book');
|
|
|
|
item.setField("version", 1);
|
2015-05-10 08:20:47 +00:00
|
|
|
var id = yield item.saveTx();
|
2015-05-01 16:41:41 +00:00
|
|
|
item = yield Zotero.Items.getAsync(id);
|
|
|
|
assert.equal(item.getField("version"), 1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should save versionNumber for computerProgram", function () {
|
|
|
|
var item = new Zotero.Item('computerProgram');
|
|
|
|
item.setField("versionNumber", "1.0");
|
2015-05-10 08:20:47 +00:00
|
|
|
var id = yield item.saveTx();
|
2015-05-01 16:41:41 +00:00
|
|
|
item = yield Zotero.Items.getAsync(id);
|
|
|
|
assert.equal(item.getField("versionNumber"), "1.0");
|
|
|
|
});
|
|
|
|
})
|
|
|
|
|
2015-05-12 23:55:14 +00:00
|
|
|
describe("#dateAdded", function () {
|
|
|
|
it("should use current time if value was not given for a new item", function* () {
|
|
|
|
var item = new Zotero.Item('book');
|
|
|
|
var id = yield item.saveTx();
|
|
|
|
item = yield Zotero.Items.getAsync(id);
|
|
|
|
|
2015-05-13 00:09:10 +00:00
|
|
|
assert.closeTo(Zotero.Date.sqlToDate(item.dateAdded, true).getTime(), Date.now(), 2000);
|
2015-05-12 23:55:14 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should use given value for a new item", function* () {
|
|
|
|
var dateAdded = "2015-05-05 17:18:12";
|
|
|
|
var item = new Zotero.Item('book');
|
|
|
|
item.dateAdded = dateAdded;
|
|
|
|
var id = yield item.saveTx();
|
|
|
|
item = yield Zotero.Items.getAsync(id);
|
|
|
|
assert.equal(item.dateAdded, dateAdded);
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-05-06 05:07:40 +00:00
|
|
|
describe("#dateModified", function () {
|
2015-05-12 23:55:14 +00:00
|
|
|
it("should use given value for a new item", function* () {
|
2015-05-06 05:07:40 +00:00
|
|
|
var dateModified = "2015-05-05 17:18:12";
|
|
|
|
var item = new Zotero.Item('book');
|
|
|
|
item.dateModified = dateModified;
|
2015-05-10 08:20:47 +00:00
|
|
|
var id = yield item.saveTx();
|
2015-05-06 05:07:40 +00:00
|
|
|
item = yield Zotero.Items.getAsync(id);
|
|
|
|
assert.equal(item.dateModified, dateModified);
|
|
|
|
})
|
|
|
|
|
2015-05-12 23:55:14 +00:00
|
|
|
it("should use given value when skipDateModifiedUpdate is set for a new item", function* () {
|
2015-05-06 05:07:40 +00:00
|
|
|
var dateModified = "2015-05-05 17:18:12";
|
|
|
|
var item = new Zotero.Item('book');
|
|
|
|
item.dateModified = dateModified;
|
2015-05-10 08:20:47 +00:00
|
|
|
var id = yield item.saveTx({
|
2015-05-06 05:07:40 +00:00
|
|
|
skipDateModifiedUpdate: true
|
|
|
|
});
|
|
|
|
item = yield Zotero.Items.getAsync(id);
|
|
|
|
assert.equal(item.dateModified, dateModified);
|
|
|
|
})
|
|
|
|
|
2015-05-12 23:55:14 +00:00
|
|
|
it("should use current time if value was not given for an existing item", function* () {
|
2015-05-06 05:07:40 +00:00
|
|
|
var dateModified = "2015-05-05 17:18:12";
|
|
|
|
var item = new Zotero.Item('book');
|
|
|
|
item.dateModified = dateModified;
|
2015-05-10 08:20:47 +00:00
|
|
|
var id = yield item.saveTx();
|
2015-05-06 05:07:40 +00:00
|
|
|
item = yield Zotero.Items.getAsync(id);
|
|
|
|
|
|
|
|
// Save again without changing Date Modified
|
|
|
|
yield item.loadItemData();
|
|
|
|
item.setField('title', 'Test');
|
2015-05-10 08:20:47 +00:00
|
|
|
yield item.saveTx()
|
2015-05-06 05:07:40 +00:00
|
|
|
|
2015-05-13 00:09:10 +00:00
|
|
|
assert.closeTo(Zotero.Date.sqlToDate(item.dateModified, true).getTime(), Date.now(), 2000);
|
2015-05-06 05:07:40 +00:00
|
|
|
})
|
|
|
|
|
2015-05-12 23:55:14 +00:00
|
|
|
it("should use current time if the existing value was given for an existing item", function* () {
|
2015-05-06 05:07:40 +00:00
|
|
|
var dateModified = "2015-05-05 17:18:12";
|
|
|
|
var item = new Zotero.Item('book');
|
|
|
|
item.dateModified = dateModified;
|
2015-05-10 08:20:47 +00:00
|
|
|
var id = yield item.saveTx();
|
2015-05-06 05:07:40 +00:00
|
|
|
item = yield Zotero.Items.getAsync(id);
|
|
|
|
|
|
|
|
// Set Date Modified to existing value
|
|
|
|
yield item.loadItemData();
|
|
|
|
item.setField('title', 'Test');
|
|
|
|
item.dateModified = dateModified;
|
2015-05-10 08:20:47 +00:00
|
|
|
yield item.saveTx()
|
2015-05-13 00:09:10 +00:00
|
|
|
assert.closeTo(Zotero.Date.sqlToDate(item.dateModified, true).getTime(), Date.now(), 2000);
|
2015-05-06 05:07:40 +00:00
|
|
|
})
|
|
|
|
|
2015-05-12 23:55:14 +00:00
|
|
|
it("should use current time if value is not given when skipDateModifiedUpdate is set for a new item", function* () {
|
2015-05-06 05:07:40 +00:00
|
|
|
var item = new Zotero.Item('book');
|
2015-05-10 08:20:47 +00:00
|
|
|
var id = yield item.saveTx({
|
2015-05-06 05:07:40 +00:00
|
|
|
skipDateModifiedUpdate: true
|
|
|
|
});
|
|
|
|
item = yield Zotero.Items.getAsync(id);
|
2015-05-13 00:09:10 +00:00
|
|
|
assert.closeTo(Zotero.Date.sqlToDate(item.dateModified, true).getTime(), Date.now(), 2000);
|
2015-05-06 05:07:40 +00:00
|
|
|
})
|
|
|
|
|
2015-05-12 23:55:14 +00:00
|
|
|
it("should keep original value when skipDateModifiedUpdate is set for an existing item", function* () {
|
2015-05-06 05:07:40 +00:00
|
|
|
var dateModified = "2015-05-05 17:18:12";
|
|
|
|
var item = new Zotero.Item('book');
|
|
|
|
item.dateModified = dateModified;
|
2015-05-10 08:20:47 +00:00
|
|
|
var id = yield item.saveTx();
|
2015-05-06 05:07:40 +00:00
|
|
|
item = yield Zotero.Items.getAsync(id);
|
|
|
|
|
|
|
|
// Resave with skipDateModifiedUpdate
|
|
|
|
yield item.loadItemData();
|
|
|
|
item.setField('title', 'Test');
|
2015-05-10 08:20:47 +00:00
|
|
|
yield item.saveTx({
|
2015-05-06 05:07:40 +00:00
|
|
|
skipDateModifiedUpdate: true
|
|
|
|
})
|
|
|
|
assert.equal(item.dateModified, dateModified);
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-04-25 07:14:53 +00:00
|
|
|
describe("#parentID", function () {
|
2015-05-05 18:08:28 +00:00
|
|
|
it("should create a child note", function* () {
|
|
|
|
var item = new Zotero.Item('book');
|
2015-05-10 08:20:47 +00:00
|
|
|
var parentItemID = yield item.saveTx();
|
2015-05-05 18:08:28 +00:00
|
|
|
|
|
|
|
item = new Zotero.Item('note');
|
|
|
|
item.parentID = parentItemID;
|
2015-05-10 08:20:47 +00:00
|
|
|
var childItemID = yield item.saveTx();
|
2015-05-05 18:08:28 +00:00
|
|
|
|
|
|
|
item = yield Zotero.Items.getAsync(childItemID);
|
|
|
|
assert.ok(item.parentID);
|
|
|
|
assert.equal(item.parentID, parentItemID);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("#parentKey", function () {
|
|
|
|
it("should be false for an unsaved attachment", function () {
|
|
|
|
var item = new Zotero.Item('attachment');
|
|
|
|
assert.isFalse(item.parentKey);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should be false on an unsaved non-attachment item", function () {
|
|
|
|
var item = new Zotero.Item('book');
|
|
|
|
assert.isFalse(item.parentKey);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not be marked as changed setting to false on an unsaved item", function () {
|
|
|
|
var item = new Zotero.Item('attachment');
|
|
|
|
item.attachmentLinkMode = 'linked_url';
|
|
|
|
item.parentKey = false;
|
|
|
|
assert.isUndefined(item._changed.parentKey);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not mark item as changed if false and no existing parent", function* () {
|
|
|
|
var item = new Zotero.Item('attachment');
|
|
|
|
item.attachmentLinkMode = 'linked_url';
|
|
|
|
item.url = "https://www.zotero.org/";
|
2015-05-10 08:20:47 +00:00
|
|
|
var id = yield item.saveTx();
|
2015-05-05 18:08:28 +00:00
|
|
|
item = yield Zotero.Items.getAsync(id);
|
|
|
|
|
|
|
|
item.parentKey = false;
|
|
|
|
assert.isFalse(item.hasChanged());
|
2015-04-25 07:14:53 +00:00
|
|
|
});
|
2015-05-07 05:00:45 +00:00
|
|
|
|
|
|
|
it("should move a top-level note under another item", function* () {
|
|
|
|
var noteItem = new Zotero.Item('note');
|
2015-05-10 08:20:47 +00:00
|
|
|
var id = yield noteItem.saveTx()
|
2015-05-07 05:00:45 +00:00
|
|
|
noteItem = yield Zotero.Items.getAsync(id);
|
|
|
|
|
|
|
|
var item = new Zotero.Item('book');
|
2015-05-10 08:20:47 +00:00
|
|
|
id = yield item.saveTx();
|
2015-05-07 05:00:45 +00:00
|
|
|
var { libraryID, key } = Zotero.Items.getLibraryAndKeyFromID(id);
|
|
|
|
|
|
|
|
noteItem.parentKey = key;
|
2015-05-10 08:20:47 +00:00
|
|
|
yield noteItem.saveTx();
|
2015-05-07 05:00:45 +00:00
|
|
|
|
|
|
|
assert.isFalse(noteItem.isTopLevelItem());
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should remove top-level item from collections when moving it under another item", function* () {
|
|
|
|
// Create a collection
|
|
|
|
var collection = new Zotero.Collection;
|
|
|
|
collection.name = "Test";
|
2015-05-10 08:20:47 +00:00
|
|
|
var collectionID = yield collection.saveTx();
|
2015-05-07 05:00:45 +00:00
|
|
|
|
|
|
|
// Create a top-level note and add it to a collection
|
|
|
|
var noteItem = new Zotero.Item('note');
|
|
|
|
noteItem.addToCollection(collectionID);
|
2015-05-10 08:20:47 +00:00
|
|
|
var id = yield noteItem.saveTx()
|
2015-05-07 05:00:45 +00:00
|
|
|
noteItem = yield Zotero.Items.getAsync(id);
|
|
|
|
|
|
|
|
var item = new Zotero.Item('book');
|
2015-05-10 08:20:47 +00:00
|
|
|
id = yield item.saveTx();
|
2015-05-07 05:00:45 +00:00
|
|
|
var { libraryID, key } = Zotero.Items.getLibraryAndKeyFromID(id);
|
|
|
|
noteItem.parentKey = key;
|
2015-05-10 08:20:47 +00:00
|
|
|
yield noteItem.saveTx();
|
2015-05-07 05:00:45 +00:00
|
|
|
|
|
|
|
assert.isFalse(noteItem.isTopLevelItem());
|
|
|
|
})
|
2015-04-25 07:14:53 +00:00
|
|
|
});
|
2015-04-29 21:22:31 +00:00
|
|
|
|
2015-05-08 17:26:11 +00:00
|
|
|
describe("#setCreators", function () {
|
|
|
|
it("should accept an array of creators in API JSON format", function* () {
|
|
|
|
var creators = [
|
|
|
|
{
|
|
|
|
firstName: "First",
|
|
|
|
lastName: "Last",
|
|
|
|
creatorType: "author"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test Name",
|
|
|
|
creatorType: "editor"
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
var item = new Zotero.Item("journalArticle");
|
|
|
|
item.setCreators(creators);
|
2015-05-10 08:20:47 +00:00
|
|
|
var id = yield item.saveTx();
|
2015-05-08 17:26:11 +00:00
|
|
|
item = yield Zotero.Items.getAsync(id);
|
|
|
|
yield item.loadCreators();
|
|
|
|
assert.sameDeepMembers(item.getCreatorsJSON(), creators);
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should accept an array of creators in internal format", function* () {
|
|
|
|
var creators = [
|
|
|
|
{
|
|
|
|
firstName: "First",
|
|
|
|
lastName: "Last",
|
|
|
|
fieldMode: 0,
|
|
|
|
creatorTypeID: 1
|
|
|
|
},
|
|
|
|
{
|
|
|
|
firstName: "",
|
|
|
|
lastName: "Test Name",
|
|
|
|
fieldMode: 1,
|
|
|
|
creatorTypeID: 2
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
var item = new Zotero.Item("journalArticle");
|
|
|
|
item.setCreators(creators);
|
2015-05-10 08:20:47 +00:00
|
|
|
var id = yield item.saveTx();
|
2015-05-08 17:26:11 +00:00
|
|
|
item = yield Zotero.Items.getAsync(id);
|
|
|
|
yield item.loadCreators();
|
|
|
|
assert.sameDeepMembers(item.getCreators(), creators);
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-05-06 06:49:16 +00:00
|
|
|
describe("#attachmentCharset", function () {
|
|
|
|
it("should get and set a value", function* () {
|
|
|
|
var charset = 'utf-8';
|
|
|
|
var item = new Zotero.Item("attachment");
|
|
|
|
item.attachmentLinkMode = Zotero.Attachments.LINK_MODE_IMPORTED_FILE;
|
|
|
|
item.attachmentCharset = charset;
|
2015-05-10 08:20:47 +00:00
|
|
|
var itemID = yield item.saveTx();
|
2015-05-06 06:49:16 +00:00
|
|
|
item = yield Zotero.Items.getAsync(itemID);
|
|
|
|
assert.equal(item.attachmentCharset, charset);
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should not be marked as changed if not changed", function* () {
|
|
|
|
var charset = 'utf-8';
|
|
|
|
var item = new Zotero.Item("attachment");
|
|
|
|
item.attachmentLinkMode = Zotero.Attachments.LINK_MODE_IMPORTED_FILE;
|
|
|
|
item.attachmentCharset = charset;
|
2015-05-10 08:20:47 +00:00
|
|
|
var itemID = yield item.saveTx();
|
2015-05-06 06:49:16 +00:00
|
|
|
item = yield Zotero.Items.getAsync(itemID);
|
|
|
|
|
|
|
|
// Set charset to same value
|
|
|
|
item.attachmentCharset = charset
|
|
|
|
assert.isFalse(item.hasChanged());
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-04-29 21:22:31 +00:00
|
|
|
describe("#attachmentFilename", function () {
|
|
|
|
it("should get and set a filename for a stored file", function* () {
|
|
|
|
var filename = "test.txt";
|
|
|
|
|
|
|
|
// Create parent item
|
|
|
|
var item = new Zotero.Item("book");
|
2015-05-10 08:20:47 +00:00
|
|
|
var parentItemID = yield item.saveTx();
|
2015-04-29 21:22:31 +00:00
|
|
|
|
|
|
|
// Create attachment item
|
|
|
|
var item = new Zotero.Item("attachment");
|
|
|
|
item.attachmentLinkMode = Zotero.Attachments.LINK_MODE_IMPORTED_FILE;
|
|
|
|
item.parentID = parentItemID;
|
2015-05-10 08:20:47 +00:00
|
|
|
var itemID = yield item.saveTx();
|
2015-04-29 21:22:31 +00:00
|
|
|
|
|
|
|
// Should be empty when unset
|
|
|
|
assert.equal(item.attachmentFilename, '');
|
|
|
|
|
|
|
|
// Set filename
|
|
|
|
item.attachmentFilename = filename;
|
2015-05-10 08:20:47 +00:00
|
|
|
yield item.saveTx();
|
2015-04-29 21:22:31 +00:00
|
|
|
item = yield Zotero.Items.getAsync(itemID);
|
|
|
|
|
|
|
|
// Check filename
|
|
|
|
assert.equal(item.attachmentFilename, filename);
|
|
|
|
|
|
|
|
// Check full path
|
|
|
|
var file = Zotero.Attachments.getStorageDirectory(item);
|
|
|
|
file.append(filename);
|
|
|
|
assert.equal(item.getFile().path, file.path);
|
|
|
|
});
|
2015-05-06 08:18:24 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
describe("#setTags", function () {
|
|
|
|
it("should save an array of tags in API JSON format", function* () {
|
|
|
|
var tags = [
|
|
|
|
{
|
|
|
|
tag: "A"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: "B"
|
|
|
|
}
|
|
|
|
];
|
|
|
|
var item = new Zotero.Item('journalArticle');
|
|
|
|
item.setTags(tags);
|
2015-05-10 08:20:47 +00:00
|
|
|
var id = yield item.saveTx();
|
2015-05-06 08:18:24 +00:00
|
|
|
item = yield Zotero.Items.getAsync(id);
|
|
|
|
yield item.loadTags();
|
|
|
|
assert.sameDeepMembers(item.getTags(tags), tags);
|
|
|
|
})
|
|
|
|
|
|
|
|
it("shouldn't mark item as changed if tags haven't changed", function* () {
|
|
|
|
var tags = [
|
|
|
|
{
|
|
|
|
tag: "A"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: "B"
|
|
|
|
}
|
|
|
|
];
|
|
|
|
var item = new Zotero.Item('journalArticle');
|
|
|
|
item.setTags(tags);
|
2015-05-10 08:20:47 +00:00
|
|
|
var id = yield item.saveTx();
|
2015-05-06 08:18:24 +00:00
|
|
|
item = yield Zotero.Items.getAsync(id);
|
|
|
|
yield item.loadTags();
|
|
|
|
item.setTags(tags);
|
|
|
|
assert.isFalse(item.hasChanged());
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should remove an existing tag", function* () {
|
|
|
|
var tags = [
|
|
|
|
{
|
|
|
|
tag: "A"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: "B"
|
|
|
|
}
|
|
|
|
];
|
|
|
|
var item = new Zotero.Item('journalArticle');
|
|
|
|
item.setTags(tags);
|
2015-05-10 08:20:47 +00:00
|
|
|
var id = yield item.saveTx();
|
2015-05-06 08:18:24 +00:00
|
|
|
item = yield Zotero.Items.getAsync(id);
|
|
|
|
yield item.loadTags();
|
|
|
|
item.setTags(tags.slice(0));
|
2015-05-10 08:20:47 +00:00
|
|
|
yield item.saveTx();
|
2015-05-06 08:18:24 +00:00
|
|
|
assert.sameDeepMembers(item.getTags(tags), tags.slice(0));
|
|
|
|
})
|
|
|
|
})
|
2015-05-13 00:02:45 +00:00
|
|
|
|
|
|
|
describe("#toJSON()", function () {
|
|
|
|
it("should output only fields with values in default mode", function* () {
|
|
|
|
var itemType = "book";
|
|
|
|
var title = "Test";
|
|
|
|
|
|
|
|
var item = new Zotero.Item(itemType);
|
|
|
|
item.setField("title", title);
|
|
|
|
var id = yield item.save();
|
|
|
|
item = yield Zotero.Items.getAsync(id);
|
|
|
|
var json = yield item.toJSON();
|
|
|
|
|
|
|
|
assert.equal(json.itemType, itemType);
|
|
|
|
assert.equal(json.title, title);
|
|
|
|
assert.isUndefined(json.date);
|
|
|
|
assert.isUndefined(json.numPages);
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should output all fields in 'full' mode", function* () {
|
|
|
|
var itemType = "book";
|
|
|
|
var title = "Test";
|
|
|
|
|
|
|
|
var item = new Zotero.Item(itemType);
|
|
|
|
item.setField("title", title);
|
|
|
|
var id = yield item.save();
|
|
|
|
item = yield Zotero.Items.getAsync(id);
|
|
|
|
var json = yield item.toJSON({ mode: 'full' });
|
|
|
|
assert.equal(json.title, title);
|
|
|
|
assert.equal(json.date, "");
|
|
|
|
assert.equal(json.numPages, "");
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should output only fields that differ in 'patch' mode", function* () {
|
|
|
|
var itemType = "book";
|
|
|
|
var title = "Test";
|
|
|
|
var date = "2015-05-12";
|
|
|
|
|
|
|
|
var item = new Zotero.Item(itemType);
|
|
|
|
item.setField("title", title);
|
|
|
|
var id = yield item.save();
|
|
|
|
item = yield Zotero.Items.getAsync(id);
|
|
|
|
var patchBase = yield item.toJSON();
|
|
|
|
|
|
|
|
item.setField("date", date);
|
|
|
|
yield item.save();
|
|
|
|
var json = yield item.toJSON({
|
|
|
|
patchBase: patchBase
|
|
|
|
})
|
|
|
|
assert.isUndefined(json.itemType);
|
|
|
|
assert.isUndefined(json.title);
|
|
|
|
assert.equal(json.date, date);
|
|
|
|
assert.isUndefined(json.numPages);
|
|
|
|
})
|
|
|
|
})
|
2015-04-25 07:14:53 +00:00
|
|
|
});
|