Skip to content

Commit

Permalink
Write more unit tests for the find controller
Browse files Browse the repository at this point in the history
Fixes #7356.
  • Loading branch information
timvandermeij committed Jan 6, 2019
1 parent e4d2a16 commit 42be612
Showing 1 changed file with 73 additions and 5 deletions.
78 changes: 73 additions & 5 deletions test/unit/pdf_find_controller_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,23 @@ describe('pdf_find_controller', function() {
pdfFindController = null;
});

it('performs a basic search', function(done) {
pdfFindController.executeCommand('find', { query: 'Dynamic', });
function testSearch({ parameters, matchesPerPage, callback, }) {
pdfFindController.executeCommand('find', parameters);

// The `updatefindmatchescount` event is only emitted if the page contains
// at least one result for the query, so the last non-zero item in the
// matches per page array corresponds to the page for which the final
// `updatefindmatchescount` event is emitted. If this happens, we know
// that any subsequent pages won't trigger the event anymore and we
// can start comparing the matches per page.
let totalPages = matchesPerPage.length;
for (let i = totalPages - 1; i >= 0; i--) {
if (matchesPerPage[i] > 0) {
totalPages = i + 1;
break;
}
}

const matchesPerPage = [11, 5, 0, 3, 0, 0, 0, 1, 1, 1, 0, 3, 4, 4];
const totalPages = matchesPerPage.length;
const totalMatches = matchesPerPage.reduce((a, b) => {
return a + b;
});
Expand All @@ -92,7 +104,63 @@ describe('pdf_find_controller', function() {
expect(pdfFindController.pageMatches[i].length)
.toEqual(matchesPerPage[i]);
}
done();
callback();
});
}

it('performs a normal search', function(done) {
testSearch({
parameters: {
query: 'Dynamic',
caseSensitive: false,
entireWord: false,
phraseSearch: true,
},
matchesPerPage: [11, 5, 0, 3, 0, 0, 0, 1, 1, 1, 0, 3, 4, 4],
callback: done,
});
});

it('performs a case sensitive search', function(done) {
testSearch({
parameters: {
query: 'Dynamic',
caseSensitive: true,
entireWord: false,
phraseSearch: true,
},
matchesPerPage: [3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3],
callback: done,
});
});

it('performs an entire word search', function(done) {
// Page 13 contains both 'Government' and 'Governmental', so the latter
// should not be found with entire word search.
testSearch({
parameters: {
query: 'Government',
caseSensitive: false,
entireWord: true,
phraseSearch: true,
},
matchesPerPage: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
callback: done,
});
});

it('performs a multiple term (no phrase) search', function(done) {
// Page 9 contains 'alternate' and pages 6 and 9 contain 'solution'.
// Both should be found for multiple term (no phrase) search.
testSearch({
parameters: {
query: 'alternate solution',
caseSensitive: false,
entireWord: false,
phraseSearch: false,
},
matchesPerPage: [0, 0, 0, 0, 0, 1, 0, 0, 4, 0, 0, 0, 0, 0],
callback: done,
});
});
});

0 comments on commit 42be612

Please sign in to comment.