Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Preview well-formed file in offline (fixes #793) #799

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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