From 1e63d6cb0892f21bbecd9179cbdd2ffb6fd8ea10 Mon Sep 17 00:00:00 2001 From: Mingze Date: Fri, 21 Aug 2020 16:30:25 -0700 Subject: [PATCH] fix(office): Pass viewer options to loaders (#1243) * fix(office): Pass viewer options to loaders * fix(office): Address comments * fix(office): Address comments --- src/lib/Preview.js | 6 ++++-- src/lib/__tests__/Preview-test.js | 7 ++++++- src/lib/viewers/AssetLoader.js | 9 ++++++--- src/lib/viewers/__tests__/AssetLoader-test.js | 8 ++++---- 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/lib/Preview.js b/src/lib/Preview.js index 99fe153f4..0fd02b03a 100644 --- a/src/lib/Preview.js +++ b/src/lib/Preview.js @@ -631,7 +631,7 @@ class Preview extends EventEmitter { try { file = getCachedFile(this.cache, { fileId, fileVersionId }); loader = file ? this.getLoader(file) : null; - viewer = loader ? loader.determineViewer(file) : null; + viewer = loader ? loader.determineViewer(file, undefined, this.options.viewers) : null; if (!viewer) { return; } @@ -1844,7 +1844,9 @@ class Preview extends EventEmitter { * @return {Object|null} Matching loader */ getLoader(file) { - return this.loaders.find(loader => loader.canLoad(file, Object.keys(this.disabledViewers))); + return this.loaders.find(loader => + loader.canLoad(file, Object.keys(this.disabledViewers), this.options.viewers), + ); } /** diff --git a/src/lib/__tests__/Preview-test.js b/src/lib/__tests__/Preview-test.js index 6b4d5e03b..23bc9ce1a 100644 --- a/src/lib/__tests__/Preview-test.js +++ b/src/lib/__tests__/Preview-test.js @@ -542,8 +542,9 @@ describe('lib/Preview', () => { sandbox .mock(loader) .expects('determineViewer') - .withArgs(someFile) + .withArgs(someFile, undefined, { viewer: {} }) .returns(viewer); + preview.options.viewers = { viewer: {} }; preview.prefetch({ fileId, token, sharedLink, sharedLinkPassword }); }); @@ -2750,9 +2751,13 @@ describe('lib/Preview', () => { canLoad: sandbox.stub().returns(true), }, ]; + preview.options.viewers = { viewer: {} }; const loader = preview.getLoader('file'); expect(loader.name).to.equal('csv'); + preview.loaders.forEach(loaderMock => { + expect(loaderMock.canLoad).to.be.calledWith('file', ['Office'], { viewer: {} }); + }); }); }); diff --git a/src/lib/viewers/AssetLoader.js b/src/lib/viewers/AssetLoader.js index e5e69fb24..e7cb46aa4 100644 --- a/src/lib/viewers/AssetLoader.js +++ b/src/lib/viewers/AssetLoader.js @@ -4,10 +4,11 @@ class AssetLoader { * * @param {Object} file - Box file * @param {Array} [disabledViewers] - List of disabled viewers + * @param {Object} viewerOptions - Custom options for viewers * @return {boolean} Is file supported */ - canLoad(file, disabledViewers = []) { - return !!this.determineViewer(file, disabledViewers); + canLoad(file, disabledViewers = [], viewerOptions = {}) { + return !!this.determineViewer(file, disabledViewers, viewerOptions); } /** @@ -24,9 +25,11 @@ class AssetLoader { * * @param {Object} file - Box file * @param {Array} [disabledViewers] - List of disabled viewers + * @param {Object} viewerOptions - Custom options for viewers * @return {Object} The viewer to use */ - determineViewer(file, disabledViewers = []) { + // eslint-disable-next-line no-unused-vars + determineViewer(file, disabledViewers = [], viewerOptions = {}) { return this.viewers.find(viewer => { if (disabledViewers.indexOf(viewer.NAME) > -1) { return false; diff --git a/src/lib/viewers/__tests__/AssetLoader-test.js b/src/lib/viewers/__tests__/AssetLoader-test.js index 20b60b87e..a644f0ab4 100644 --- a/src/lib/viewers/__tests__/AssetLoader-test.js +++ b/src/lib/viewers/__tests__/AssetLoader-test.js @@ -23,15 +23,15 @@ describe('lib/viewers/AssetLoader', () => { it('should return true if loader can find a viewer to match the file', () => { sandbox.stub(loader, 'determineViewer').returns({}); - expect(loader.canLoad({})).to.be.true; - expect(loader.determineViewer).to.be.called; + expect(loader.canLoad({}, [], { viewer: {} })).to.be.true; + expect(loader.determineViewer).to.be.calledWith({}, [], { viewer: {} }); }); it("should return false if loader can't find a viewer to match the file", () => { sandbox.stub(loader, 'determineViewer').returns(null); - expect(loader.canLoad({})).to.be.false; - expect(loader.determineViewer).to.be.called; + expect(loader.canLoad({}, [], { viewer: {} })).to.be.false; + expect(loader.determineViewer).to.be.calledWith({}, [], { viewer: {} }); }); });