From 9f874d773df996f3bbd95030c620a321df1bda45 Mon Sep 17 00:00:00 2001 From: Mick Ryan Date: Thu, 27 Feb 2020 16:50:28 -0800 Subject: [PATCH 1/4] feat(print): add check to see if printing is available * Adds helper method to check if printing is available on the preview instance --- src/lib/Preview.js | 14 ++++++++++++-- src/lib/__tests__/Preview-test.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/lib/Preview.js b/src/lib/Preview.js index f91c5a337..efcc33a6c 100644 --- a/src/lib/Preview.js +++ b/src/lib/Preview.js @@ -502,6 +502,16 @@ class Preview extends EventEmitter { } } + /** + * Checks if the file being previewed supports printing. + * + * @public + * @return {boolean} + */ + canPrint() { + return !!canDownload(this.file, this.options) && checkFeature(this.viewer, 'print'); + } + /** * Prints the file being previewed if the viewer supports printing. * @@ -509,7 +519,7 @@ class Preview extends EventEmitter { * @return {void} */ print() { - if (canDownload(this.file, this.options) && checkFeature(this.viewer, 'print')) { + if (this.canPrint()) { this.viewer.print(); } } @@ -1299,7 +1309,7 @@ class Preview extends EventEmitter { if (canDownload(this.file, this.options)) { this.ui.showDownloadButton(this.download); - if (checkFeature(this.viewer, 'print')) { + if (this.canPrint()) { this.ui.showPrintButton(this.print); } } diff --git a/src/lib/__tests__/Preview-test.js b/src/lib/__tests__/Preview-test.js index 8ff708735..168b87bb3 100644 --- a/src/lib/__tests__/Preview-test.js +++ b/src/lib/__tests__/Preview-test.js @@ -795,6 +795,34 @@ describe('lib/Preview', () => { }); }); + describe('canPrint()', () => { + beforeEach(() => { + stubs.canDownload = sandbox.stub(file, 'canDownload'); + stubs.checkFeature = sandbox.stub(file, 'checkFeature'); + }); + + it('should return true is file is downloadable and has printing feature', () => { + stubs.canDownload.returns(true); + stubs.checkFeature.withArgs(sinon.match.any, 'print').returns(true); + + expect(preview.canPrint()).to.equal(true); + }); + + it('should return false is file is not downloadable and has printing feature', () => { + stubs.canDownload.returns(false); + stubs.checkFeature.withArgs(sinon.match.any, 'print').returns(true); + + expect(preview.canPrint()).to.equal(false); + }); + + it('should return false is file is downloadable and but does not have printing feature', () => { + stubs.canDownload.returns(true); + stubs.checkFeature.withArgs(sinon.match.any, 'print').returns(false); + + expect(preview.canPrint()).to.equal(false); + }); + }); + describe('download()', () => { beforeEach(() => { stubs.downloadReachability = new DownloadReachability({}); From cd6931a96976b95f4b311625df032eb791f7ac75 Mon Sep 17 00:00:00 2001 From: Mick Ryan Date: Fri, 28 Feb 2020 16:43:51 -0800 Subject: [PATCH 2/4] feat(print): PR Feedback --- src/lib/Preview.js | 7 +++---- src/lib/__tests__/Preview-test.js | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/lib/Preview.js b/src/lib/Preview.js index efcc33a6c..74ffba9ce 100644 --- a/src/lib/Preview.js +++ b/src/lib/Preview.js @@ -1305,13 +1305,12 @@ class Preview extends EventEmitter { // Log now that loading is finished this.emitLoadMetrics(data); - // Show download and print buttons if user can download if (canDownload(this.file, this.options)) { this.ui.showDownloadButton(this.download); + } - if (this.canPrint()) { - this.ui.showPrintButton(this.print); - } + if (this.canPrint()) { + this.ui.showPrintButton(this.print); } const { error } = data; diff --git a/src/lib/__tests__/Preview-test.js b/src/lib/__tests__/Preview-test.js index 168b87bb3..9e31de792 100644 --- a/src/lib/__tests__/Preview-test.js +++ b/src/lib/__tests__/Preview-test.js @@ -817,7 +817,7 @@ describe('lib/Preview', () => { it('should return false is file is downloadable and but does not have printing feature', () => { stubs.canDownload.returns(true); - stubs.checkFeature.withArgs(sinon.match.any, 'print').returns(false); + stubs.checkFeature.returns(false); expect(preview.canPrint()).to.equal(false); }); From 962c53bd8e847c6b3b918c9f549efc2193cc585c Mon Sep 17 00:00:00 2001 From: Mick Ryan Date: Mon, 2 Mar 2020 10:07:27 -0800 Subject: [PATCH 3/4] feat(print): Remove withArgs from a few tests --- src/lib/__tests__/Preview-test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/__tests__/Preview-test.js b/src/lib/__tests__/Preview-test.js index 9e31de792..9b59b3782 100644 --- a/src/lib/__tests__/Preview-test.js +++ b/src/lib/__tests__/Preview-test.js @@ -803,14 +803,14 @@ describe('lib/Preview', () => { it('should return true is file is downloadable and has printing feature', () => { stubs.canDownload.returns(true); - stubs.checkFeature.withArgs(sinon.match.any, 'print').returns(true); + stubs.checkFeature.returns(true); expect(preview.canPrint()).to.equal(true); }); it('should return false is file is not downloadable and has printing feature', () => { stubs.canDownload.returns(false); - stubs.checkFeature.withArgs(sinon.match.any, 'print').returns(true); + stubs.checkFeature.returns(true); expect(preview.canPrint()).to.equal(false); }); From e88ec5a098a1d28f78c2345946217790b2013e59 Mon Sep 17 00:00:00 2001 From: Mick Ryan Date: Mon, 2 Mar 2020 13:55:33 -0800 Subject: [PATCH 4/4] feat(print): Update E2E test to check for proper error message --- test/integration/document/Thumbnails.e2e.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/document/Thumbnails.e2e.test.js b/test/integration/document/Thumbnails.e2e.test.js index ae99fbee2..13e125e2d 100644 --- a/test/integration/document/Thumbnails.e2e.test.js +++ b/test/integration/document/Thumbnails.e2e.test.js @@ -260,7 +260,7 @@ describe('Preview Document Thumbnails', () => { it('Should not show the thumbnails sidebar when a document preview errors', () => { cy.showPreview(token, badFileId, { enableThumbnailsSidebar: true }); - cy.contains('We’re sorry the preview didn’t load. This file could not be converted.'); + cy.contains('We’re sorry, the preview didn’t load. Please re-upload the file or contact Box support.'); cy.getByTestId('thumbnails-sidebar').should('not.exist'); });