From 4a19c531cf88d655a929565132fcb914ab6cd0a9 Mon Sep 17 00:00:00 2001 From: Mick Ryan Date: Mon, 2 Mar 2020 15:26:05 -0800 Subject: [PATCH] feat(print): add check to see if printing is available (#1177) * feat(print): add check to see if printing is available * Adds helper method to check if printing is available on the preview instance * feat(print): Update E2E test to check for proper error message --- src/lib/Preview.js | 19 +++++++++---- src/lib/__tests__/Preview-test.js | 28 +++++++++++++++++++ .../document/Thumbnails.e2e.test.js | 2 +- 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/lib/Preview.js b/src/lib/Preview.js index f91c5a337..74ffba9ce 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(); } } @@ -1295,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 (checkFeature(this.viewer, 'print')) { - 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 8ff708735..9b59b3782 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.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.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.returns(false); + + expect(preview.canPrint()).to.equal(false); + }); + }); + describe('download()', () => { beforeEach(() => { stubs.downloadReachability = new DownloadReachability({}); 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'); });