From d363ffb808e23a762f4b66bd733b83aa3630505a Mon Sep 17 00:00:00 2001 From: Stanislav Dolgachov Date: Tue, 22 May 2018 15:17:18 +0300 Subject: [PATCH 1/4] Fix: Preview well-formed file in offline (fixes #793) --- src/lib/Preview.js | 16 ++++++++++++---- src/lib/__tests__/Preview-test.js | 10 ++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/lib/Preview.js b/src/lib/Preview.js index 78386a617..287496064 100644 --- a/src/lib/Preview.js +++ b/src/lib/Preview.js @@ -771,10 +771,15 @@ class Preview extends EventEmitter { this.retryCount = 0; } - // Fetch access tokens before proceeding - getTokens(this.file.id, this.previewOptions.token) - .then(this.handleTokenResponse) - .catch(this.handleFetchError); + const couldPreviewOfflineWithoutToken = typeof fileIdOrFile === 'object' && this.options.skipServerUpdate; + if (couldPreviewOfflineWithoutToken) { + this.handleTokenResponse({}); + } else { + // Fetch access tokens before proceeding + getTokens(this.file.id, this.previewOptions.token) + .then(this.handleTokenResponse) + .catch(this.handleFetchError); + } } /** @@ -798,6 +803,9 @@ class Preview extends EventEmitter { // Load from cache if the current file is valid, otherwise load file info from server if (checkFileValid(this.file)) { + /* Save file in cache. This also adds the 'ORIGINAL' representation. + * It is required to preview files offline */ + cacheFile(this.cache, this.file); this.loadFromCache(); } else { this.loadFromServer(); diff --git a/src/lib/__tests__/Preview-test.js b/src/lib/__tests__/Preview-test.js index 0f36a8edf..a303e0ff6 100644 --- a/src/lib/__tests__/Preview-test.js +++ b/src/lib/__tests__/Preview-test.js @@ -1002,6 +1002,15 @@ describe('lib/Preview', () => { expect(preview.retryTimeout).to.equal(undefined); }); + it('should load preview when a well-formed file object is passed and server update should be skipped', () => { + const previewOptions = { skipServerUpdate: true }; + preview.parseOptions(previewOptions); + + preview.load(stubs.file); + expect(stubs.handleTokenResponse).to.be.calledWith({}); + expect(stubs.getTokens).to.not.be.called; + }); + it('should set the retry count if we are retrying by file ID', () => { preview.retryCount = 0; preview.file.id = '0'; @@ -1114,6 +1123,7 @@ describe('lib/Preview', () => { stubs.checkFileValid.returns(true); preview.handleTokenResponse({}); + expect(stubs.cacheFile).to.be.calledWith(preview.cache, preview.file); expect(stubs.loadFromCache).to.be.called; expect(stubs.loadFromServer).to.not.be.called; }); From 2f855f2aa673f180b4ec06e6b3045cb4b6c295cb Mon Sep 17 00:00:00 2001 From: Stanislav Dolgachov Date: Thu, 31 May 2018 14:07:56 +0300 Subject: [PATCH 2/4] Chore: Rename variable used for detecting offline preview --- src/lib/Preview.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/Preview.js b/src/lib/Preview.js index 287496064..eb2fbe8d8 100644 --- a/src/lib/Preview.js +++ b/src/lib/Preview.js @@ -771,8 +771,8 @@ class Preview extends EventEmitter { this.retryCount = 0; } - const couldPreviewOfflineWithoutToken = typeof fileIdOrFile === 'object' && this.options.skipServerUpdate; - if (couldPreviewOfflineWithoutToken) { + const isPreviewOffline = typeof fileIdOrFile === 'object' && this.options.skipServerUpdate; + if (isPreviewOffline) { this.handleTokenResponse({}); } else { // Fetch access tokens before proceeding From f9deb786c97d2ed99a18fd05b570a741f2f3ea4d Mon Sep 17 00:00:00 2001 From: Stanislav Dolgachov Date: Thu, 31 May 2018 14:23:17 +0300 Subject: [PATCH 3/4] Chore: Change comment style in Preview class --- src/lib/Preview.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/lib/Preview.js b/src/lib/Preview.js index eb2fbe8d8..513c8cd57 100644 --- a/src/lib/Preview.js +++ b/src/lib/Preview.js @@ -803,8 +803,7 @@ class Preview extends EventEmitter { // Load from cache if the current file is valid, otherwise load file info from server if (checkFileValid(this.file)) { - /* Save file in cache. This also adds the 'ORIGINAL' representation. - * It is required to preview files offline */ + // Save file in cache. This also adds the 'ORIGINAL' representation. It is required to preview files offline cacheFile(this.cache, this.file); this.loadFromCache(); } else { From 5bfffadedaa072731cf5a4df11bb0c6ac65705bb Mon Sep 17 00:00:00 2001 From: Stanislav Dolgachov Date: Thu, 31 May 2018 14:28:57 +0300 Subject: [PATCH 4/4] Chore: Set options directly when test offline preview load --- src/lib/__tests__/Preview-test.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/lib/__tests__/Preview-test.js b/src/lib/__tests__/Preview-test.js index a303e0ff6..02ba6fc87 100644 --- a/src/lib/__tests__/Preview-test.js +++ b/src/lib/__tests__/Preview-test.js @@ -1003,8 +1003,7 @@ describe('lib/Preview', () => { }); it('should load preview when a well-formed file object is passed and server update should be skipped', () => { - const previewOptions = { skipServerUpdate: true }; - preview.parseOptions(previewOptions); + preview.options.skipServerUpdate = true; preview.load(stubs.file); expect(stubs.handleTokenResponse).to.be.calledWith({});