Skip to content

Commit

Permalink
Fix: Preview well-formed file in offline (fixes #793) (#799)
Browse files Browse the repository at this point in the history
* Fix: Preview well-formed file in offline (fixes #793)
* Chore: Rename variable used for detecting offline preview
* Chore: Change comment style in Preview class
* Chore: Set options directly when test offline preview load
  • Loading branch information
dolgachio authored and pramodsum committed May 31, 2018
1 parent bab0c98 commit fac6e9c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/lib/Preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 isPreviewOffline = typeof fileIdOrFile === 'object' && this.options.skipServerUpdate;
if (isPreviewOffline) {
this.handleTokenResponse({});
} else {
// Fetch access tokens before proceeding
getTokens(this.file.id, this.previewOptions.token)
.then(this.handleTokenResponse)
.catch(this.handleFetchError);
}
}

/**
Expand All @@ -798,6 +803,8 @@ 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();
Expand Down
9 changes: 9 additions & 0 deletions src/lib/__tests__/Preview-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,14 @@ 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', () => {
preview.options.skipServerUpdate = true;

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';
Expand Down Expand Up @@ -1114,6 +1122,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;
});
Expand Down

0 comments on commit fac6e9c

Please sign in to comment.