Fix search saving, and add some unit tests

Also:

- Return an object from `Zotero.Search.prototype.getConditions()`
  instead of an array.
- Add support function `getPromiseError(promise)` to return the error
  thrown from a chain of promises, or false if none. (We could make an
  `assert.throwsAsync()`, but this allows testing of various properties
  such as `.name`, which even the built-in `assert.throws()` can't
  test.)
- Clarify some search save errors
This commit is contained in:
Dan Stillman 2015-04-17 00:20:16 -04:00
parent 59773f3f6d
commit d9c32a8e90
4 changed files with 68 additions and 9 deletions

View file

@ -114,6 +114,13 @@ function waitForCallback(cb, interval, timeout) {
return deferred.promise;
}
/**
* Return a promise for the error thrown by a promise, or false if none
*/
function getPromiseError(promise) {
return promise.thenReturn(false).catch(e => e);
}
/**
* Ensures that the PDF tools are installed, or installs them if not.
* Returns a promise.

47
test/tests/searchTests.js Normal file
View file

@ -0,0 +1,47 @@
describe("Zotero.Search", function() {
describe("#save()", function () {
it("should fail without a libraryID", function* () {
var s = new Zotero.Search;
s.name = "Test";
s.addCondition('title', 'is', 'test');
var e = yield getPromiseError(s.save());
assert.ok(e);
assert.equal(e.constructor.name, Error.prototype.constructor.name); // TEMP: Error mismatch
assert.equal(e.message, "libraryID must be set before saving search");
});
it("should fail without a name", function* () {
var s = new Zotero.Search;
s.libraryID = Zotero.Libraries.userLibraryID;
s.addCondition('title', 'is', 'test');
var e = yield getPromiseError(s.save());
assert.ok(e);
assert.equal(e.constructor.name, Error.prototype.constructor.name); // TEMP: Error mismatch
assert.equal(e.message, "Name not provided for saved search");
});
it("should save a new search", function* () {
// Save search
var s = new Zotero.Search;
s.libraryID = Zotero.Libraries.userLibraryID;
s.name = "Test";
s.addCondition('title', 'is', 'test');
var id = yield s.save();
assert.typeOf(id, 'number');
// Check saved search
s = yield Zotero.Searches.getAsync(id);
assert.ok(s);
assert.instanceOf(s, Zotero.Search);
assert.equal(s.name, "Test");
yield s.loadConditions();
var conditions = s.getConditions();
assert.lengthOf(Object.keys(conditions), 1);
assert.property(conditions, "1"); // searchConditionIDs start at 1
var condition = conditions[1];
assert.propertyVal(condition, 'condition', 'title')
assert.propertyVal(condition, 'operator', 'is')
assert.propertyVal(condition, 'value', 'test')
});
});
});