diff --git a/src/lib/Preview.js b/src/lib/Preview.js index d2983f572..8350c5b7e 100644 --- a/src/lib/Preview.js +++ b/src/lib/Preview.js @@ -216,6 +216,9 @@ class Preview extends EventEmitter { // if proper valid file objects were passed in. this.updateCollection(options.collection); + // Parse the preview options + this.parseOptions(this.previewOptions); + // Load the preview this.load(fileIdOrFile); } @@ -750,8 +753,8 @@ class Preview extends EventEmitter { return; } - // Parse the preview options supplied by show() - this.parseOptions(this.previewOptions, tokenMap); + // Set the authorization token + this.options.token = tokenMap[this.file.id]; this.setupUI(); @@ -795,10 +798,9 @@ class Preview extends EventEmitter { * * @private * @param {Object} previewOptions - Options specified by show() - * @param {Object} tokenMap - Map of file ID to access token * @return {void} */ - parseOptions(previewOptions, tokenMap) { + parseOptions(previewOptions) { const options = Object.assign({}, previewOptions); // Reset all options @@ -807,9 +809,6 @@ class Preview extends EventEmitter { // Container for preview this.options.container = options.container; - // Authorization token - this.options.token = tokenMap[this.file.id]; - // Shared link URL this.options.sharedLink = options.sharedLink; diff --git a/src/lib/__tests__/Preview-test.js b/src/lib/__tests__/Preview-test.js index eb3f5e747..ddd9a540c 100644 --- a/src/lib/__tests__/Preview-test.js +++ b/src/lib/__tests__/Preview-test.js @@ -109,6 +109,7 @@ describe('lib/Preview', () => { beforeEach(() => { stubs.load = sandbox.stub(preview, 'load'); stubs.updateCollection = sandbox.stub(preview, 'updateCollection'); + stubs.parseOptions = sandbox.stub(); }); it('should set the preview options with string token', () => { @@ -188,6 +189,20 @@ describe('lib/Preview', () => { expect(e.message).to.equal('Bad access token!'); } }); + + it('should parse the preview options', () => { + preview.retryCount = 0; + preview.parseOptions = stubs.parseOptions; + + const token = 'token'; + const options = { + foo: 'bar' + }; + + preview.show(file, token, options); + + expect(stubs.parseOptions).to.be.calledWith(Object.assign({}, options, { token })); + }); }); describe('hide()', () => { @@ -1007,7 +1022,6 @@ describe('lib/Preview', () => { describe('handleTokenResponse()', () => { beforeEach(() => { stubs.loadFromServer = sandbox.stub(preview, 'loadFromServer'); - stubs.parseOptions = sandbox.stub(preview, 'parseOptions'); stubs.setupUI = sandbox.stub(preview, 'setupUI'); stubs.checkPermission = sandbox.stub(file, 'checkPermission'); stubs.checkFileValid = sandbox.stub(file, 'checkFileValid'); @@ -1020,14 +1034,20 @@ describe('lib/Preview', () => { preview.handleTokenResponse({}); expect(stubs.loadFromServer).to.be.called; - expect(stubs.parseOptions).to.not.be.called; }); - it('should parse the preview options', () => { + it('should set the token option', () => { preview.retryCount = 0; + const TOKEN = 'bar'; + const FILE_ID = '123'; + preview.file = { + id: FILE_ID + }; + preview.handleTokenResponse({ + [FILE_ID]: TOKEN + }); - preview.handleTokenResponse({}); - expect(stubs.parseOptions).to.be.called; + expect(preview.options.token).to.equal(TOKEN); }); it('should setup UI', () => { @@ -1093,9 +1113,6 @@ describe('lib/Preview', () => { stubs.assign = sandbox.spy(Object, 'assign'); stubs.disableViewers = sandbox.stub(preview, 'disableViewers'); stubs.enableViewers = sandbox.stub(preview, 'enableViewers'); - stubs.tokens = { - 0: 'file0' - }; preview.file = { id: 0 @@ -1103,83 +1120,78 @@ describe('lib/Preview', () => { }); it('should use the saved preview options', () => { - preview.parseOptions(preview.previewOptions, stubs.tokens); + preview.parseOptions(preview.previewOptions); expect(stubs.assign).to.be.calledWith(preview.previewOptions); }); it('should set the container', () => { - preview.parseOptions(preview.previewOptions, stubs.tokens); + preview.parseOptions(preview.previewOptions); expect(preview.options.container).to.equal(containerEl); }); - it('should set the token based on the file id', () => { - preview.parseOptions(preview.previewOptions, stubs.tokens); - expect(preview.options.token).to.equal(stubs.tokens[0]); - }); - it('should set shared link and shared link password', () => { - preview.parseOptions(preview.previewOptions, stubs.tokens); + preview.parseOptions(preview.previewOptions); expect(preview.options.sharedLink).to.equal(stubs.sharedLink); expect(preview.options.sharedLinkPassword).to.equal(stubs.sharedLinkPassword); }); it('should save a reference to the api host', () => { - preview.parseOptions(preview.previewOptions, stubs.tokens); + preview.parseOptions(preview.previewOptions); expect(preview.options.apiHost).to.equal('endpoint'); // Check default preview.previewOptions.apiHost = undefined; - preview.parseOptions(preview.previewOptions, stubs.tokens); + preview.parseOptions(preview.previewOptions); expect(preview.options.apiHost).to.equal('https://api.box.com'); }); it('should save a reference to the app host', () => { - preview.parseOptions(preview.previewOptions, stubs.tokens); + preview.parseOptions(preview.previewOptions); expect(preview.options.appHost).to.equal(stubs.appHost); // Check default preview.previewOptions.appHost = undefined; - preview.parseOptions(preview.previewOptions, stubs.tokens); + preview.parseOptions(preview.previewOptions); expect(preview.options.appHost).to.equal('https://app.box.com'); }); it('should set whether to show the header or a custom logo', () => { - preview.parseOptions(preview.previewOptions, stubs.tokens); + preview.parseOptions(preview.previewOptions); expect(preview.options.header).to.equal(stubs.header); expect(preview.options.logoUrl).to.equal(stubs.logoUrl); preview.previewOptions.header = undefined; preview.previewOptions.logoUrl = undefined; - preview.parseOptions(preview.previewOptions, stubs.tokens); + preview.parseOptions(preview.previewOptions); expect(preview.options.header).to.equal('light'); expect(preview.options.logoUrl).to.equal(''); }); it('should set whether to show a download link or annotations', () => { - preview.parseOptions(preview.previewOptions, stubs.tokens); + preview.parseOptions(preview.previewOptions); expect(preview.options.showDownload).to.be.true; expect(preview.options.showAnnotations).to.be.true; }); it('should set whether to skip load from the server and any server updates', () => { - preview.parseOptions(preview.previewOptions, stubs.tokens); + preview.parseOptions(preview.previewOptions); expect(preview.options.skipServerUpdate).to.be.false; preview.previewOptions.skipServerUpdate = true; - preview.parseOptions(preview.previewOptions, stubs.tokens); + preview.parseOptions(preview.previewOptions); expect(preview.options.skipServerUpdate).to.be.true; }); it('should set whether to fix dependencies', () => { - preview.parseOptions(preview.previewOptions, stubs.tokens); + preview.parseOptions(preview.previewOptions); expect(preview.options.fixDependencies).to.be.true; }); it('should add user created loaders before standard loaders', () => { const expectedLoaders = stubs.loaders.concat(loaders); - preview.parseOptions(preview.previewOptions, stubs.tokens); + preview.parseOptions(preview.previewOptions); expect(preview.loaders[0]).to.equal(expectedLoaders[0]); expect(preview.loaders).to.deep.equal(expectedLoaders); }); @@ -1194,7 +1206,7 @@ describe('lib/Preview', () => { } }; - preview.parseOptions(preview.previewOptions, stubs.tokens); + preview.parseOptions(preview.previewOptions); expect(stubs.disableViewers).to.be.calledWith('Office'); expect(stubs.enableViewers).to.be.calledWith('text'); });