From 2be8036eb75f71fc97321d0fcbfb28d1288bc28e Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Thu, 24 Feb 2022 11:20:27 +0100 Subject: [PATCH] [api-minor] Reduce duplication in the "gets non-existent page" unit-test --- src/display/api.js | 2 +- test/unit/api_spec.js | 47 ++++++++++++++----------------------------- 2 files changed, 16 insertions(+), 33 deletions(-) diff --git a/src/display/api.js b/src/display/api.js index de847bb50..0fd2f71e5 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -2848,7 +2848,7 @@ class WorkerTransport { pageNumber <= 0 || pageNumber > this._numPages ) { - return Promise.reject(new Error("Invalid page request")); + return Promise.reject(new Error("Invalid page request.")); } const pageIndex = pageNumber - 1, diff --git a/test/unit/api_spec.js b/test/unit/api_spec.js index 95cc8461c..54c1ef885 100644 --- a/test/unit/api_spec.js +++ b/test/unit/api_spec.js @@ -815,40 +815,23 @@ describe("api", function () { }); it("gets non-existent page", async function () { - let outOfRangePromise = pdfDocument.getPage(100); - let nonIntegerPromise = pdfDocument.getPage(2.5); - let nonNumberPromise = pdfDocument.getPage("1"); + const pageNumbers = [ + /* outOfRange = */ 100, + /* nonInteger = */ 2.5, + /* nonNumber = */ "1", + ]; - outOfRangePromise = outOfRangePromise.then( - function () { - throw new Error("shall fail for out-of-range pageNumber parameter"); - }, - function (reason) { - expect(reason instanceof Error).toEqual(true); - } - ); - nonIntegerPromise = nonIntegerPromise.then( - function () { - throw new Error("shall fail for non-integer pageNumber parameter"); - }, - function (reason) { - expect(reason instanceof Error).toEqual(true); - } - ); - nonNumberPromise = nonNumberPromise.then( - function () { - throw new Error("shall fail for non-number pageNumber parameter"); - }, - function (reason) { - expect(reason instanceof Error).toEqual(true); - } - ); + for (const pageNumber of pageNumbers) { + try { + await pdfDocument.getPage(pageNumber); - await Promise.all([ - outOfRangePromise, - nonIntegerPromise, - nonNumberPromise, - ]); + // Shouldn't get here. + expect(false).toEqual(true); + } catch (reason) { + expect(reason instanceof Error).toEqual(true); + expect(reason.message).toEqual("Invalid page request."); + } + } }); it("gets page, from /Pages tree with circular reference", async function () {