zotero/test/tests/attachmentsTest.js
Dan Stillman 3fc09add3a Attachment fixes
Change all attachment functions to take parameter objects, including a
'collections' property to assign collections. (Previously, calling code
assigned collections separately, which required a nested transaction,
which is no longer possible.)

Fixes #723, Can't attach files by dragging
2015-05-23 04:43:37 -04:00

101 lines
3.1 KiB
JavaScript

describe("Zotero.Attachments", function() {
var win;
before(function () {
// Hidden browser, which requires a browser window, needed for charset detection
// (until we figure out a better way)
if (!Zotero.isStandalone) {
return loadBrowserWindow().then(window => win = window);
}
});
after(function () {
if (win) {
win.close();
}
});
describe("#importFromFile()", function () {
it("should create a child attachment from a text file", function* () {
// Create test file
var contents = "Test";
var tmpFile = Zotero.getTempDirectory();
tmpFile.append('test.txt');
yield Zotero.File.putContentsAsync(tmpFile, contents);
// Create parent item
var item = new Zotero.Item('book');
var parentItemID = yield item.saveTx();
// Create attachment and compare content
var itemID = yield Zotero.Attachments.importFromFile({
file: tmpFile,
parentItemID: parentItemID
});
var item = yield Zotero.Items.getAsync(itemID);
var storedFile = item.getFile();
assert.equal((yield Zotero.File.getContentsAsync(storedFile)), contents);
// Clean up
yield Zotero.Items.erase(itemID);
});
it("should create a top-level attachment from a PNG file", function* () {
var file = getTestDataDirectory();
file.append('test.png');
var contents = yield Zotero.File.getBinaryContentsAsync(file);
// Create attachment and compare content
var itemID = yield Zotero.Attachments.importFromFile({
file: file
});
var item = yield Zotero.Items.getAsync(itemID);
var storedFile = item.getFile();
assert.equal((yield Zotero.File.getBinaryContentsAsync(storedFile)), contents);
// Clean up
yield Zotero.Items.erase(itemID);
});
it("should create a top-level attachment from a PNG file in a collection", function* () {
var file = getTestDataDirectory();
file.append('test.png');
var contents = yield Zotero.File.getBinaryContentsAsync(file);
var collection = yield createDataObject('collection');
// Create attachment and compare content
var itemID = yield Zotero.Attachments.importFromFile({
file: file,
collections: [collection.id]
});
var item = yield Zotero.Items.getAsync(itemID);
var storedFile = item.getFile();
assert.equal((yield Zotero.File.getBinaryContentsAsync(storedFile)), contents);
// Clean up
yield Zotero.Items.erase(itemID);
});
it("should create a child attachment from a PNG file", function* () {
var file = getTestDataDirectory();
file.append('test.png');
var contents = yield Zotero.File.getBinaryContentsAsync(file);
// Create parent item
var item = new Zotero.Item('book');
var parentItemID = yield item.saveTx();
// Create attachment and compare content
var itemID = yield Zotero.Attachments.importFromFile({
file: file,
parentItemID: parentItemID
});
var item = yield Zotero.Items.getAsync(itemID);
var storedFile = item.getFile();
assert.equal((yield Zotero.File.getBinaryContentsAsync(storedFile)), contents);
// Clean up
yield Zotero.Items.erase(itemID);
});
})
})