diff --git a/test/content/support.js b/test/content/support.js index 783a13f7c5..e66ba4cc53 100644 --- a/test/content/support.js +++ b/test/content/support.js @@ -241,4 +241,17 @@ function resetDB() { }).then(function() { return Zotero.Schema.schemaUpdatePromise; }); -} \ No newline at end of file +} + + +/** + * Imports an attachment from a test file. + * @param {string} filename - The filename to import (in data directory) + * @return {Promise} + */ +function importFileAttachment(filename) { + let testfile = getTestDataDirectory(); + filename.split('/').forEach((part) => testfile.append(part)); + return Zotero.Attachments.importFromFile({file: testfile}); +} + diff --git a/test/tests/data/search/foo.html b/test/tests/data/search/foo.html new file mode 100644 index 0000000000..a97e42ef21 --- /dev/null +++ b/test/tests/data/search/foo.html @@ -0,0 +1,9 @@ + + + + hello + + +

foo

+ + diff --git a/test/tests/data/search/foobar.html b/test/tests/data/search/foobar.html new file mode 100644 index 0000000000..f05719144e --- /dev/null +++ b/test/tests/data/search/foobar.html @@ -0,0 +1,9 @@ + + + + hello + + +

foo bar

+ + diff --git a/test/tests/searchTest.js b/test/tests/searchTest.js index 2573c93304..d49dab77d5 100644 --- a/test/tests/searchTest.js +++ b/test/tests/searchTest.js @@ -83,4 +83,68 @@ describe("Zotero.Search", function() { assert.propertyVal(conditions[0], 'value', 'foo') }); }); + + describe("#search()", function () { + let win; + let fooItem; + let foobarItem; + + before(function* () { + // Hidden browser, which requires a browser window, needed for charset detection + // (until we figure out a better way) + win = yield loadBrowserWindow(); + fooItem = yield importFileAttachment("search/foo.html"); + foobarItem = yield importFileAttachment("search/foobar.html"); + }); + + after(function* () { + if (win) { + win.close(); + } + yield fooItem.erase(); + yield foobarItem.erase(); + }); + + it("should return matches with full-text conditions", function* () { + let s = new Zotero.Search(); + s.addCondition('fulltextWord', 'contains', 'foo'); + let matches = yield s.search(); + assert.lengthOf(matches, 2); + assert.sameMembers(matches, [fooItem.id, foobarItem.id]); + }); + + it("should not return non-matches with full-text conditions", function* () { + let s = new Zotero.Search(); + s.addCondition('fulltextWord', 'contains', 'baz'); + let matches = yield s.search(); + assert.lengthOf(matches, 0); + }); + + it("should return matches for full-text conditions in ALL mode", function* () { + let s = new Zotero.Search(); + s.addCondition('joinMode', 'all'); + s.addCondition('fulltextWord', 'contains', 'foo'); + s.addCondition('fulltextWord', 'contains', 'bar'); + let matches = yield s.search(); + assert.deepEqual(matches, [foobarItem.id]); + }); + + it("should not return non-matches for full-text conditions in ALL mode", function* () { + let s = new Zotero.Search(); + s.addCondition('joinMode', 'all'); + s.addCondition('fulltextWord', 'contains', 'mjktkiuewf'); + s.addCondition('fulltextWord', 'contains', 'zijajkvudk'); + let matches = yield s.search(); + assert.lengthOf(matches, 0); + }); + + it("should return a match that satisfies only one of two full-text condition in ANY mode", function* () { + let s = new Zotero.Search(); + s.addCondition('joinMode', 'any'); + s.addCondition('fulltextWord', 'contains', 'bar'); + s.addCondition('fulltextWord', 'contains', 'baz'); + let matches = yield s.search(); + assert.deepEqual(matches, [foobarItem.id]); + }); + }); });