Skip to content

Commit

Permalink
feat(print): add check to see if printing is available (#1177)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mickr authored Mar 2, 2020
1 parent d57ac68 commit 4a19c53
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
19 changes: 14 additions & 5 deletions src/lib/Preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,14 +502,24 @@ 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.
*
* @public
* @return {void}
*/
print() {
if (canDownload(this.file, this.options) && checkFeature(this.viewer, 'print')) {
if (this.canPrint()) {
this.viewer.print();
}
}
Expand Down Expand Up @@ -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;
Expand Down
28 changes: 28 additions & 0 deletions src/lib/__tests__/Preview-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({});
Expand Down
2 changes: 1 addition & 1 deletion test/integration/document/Thumbnails.e2e.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});

Expand Down

0 comments on commit 4a19c53

Please sign in to comment.