Skip to content

Commit

Permalink
Update: Continuous progress bar through retries (#883)
Browse files Browse the repository at this point in the history
If file fetch returns error, don't re-setup the UI (which re instantiates the progress bar) during the retries
  • Loading branch information
Conrad Chan authored Jan 9, 2019
1 parent 725ca3d commit 6782e47
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/lib/Preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,10 @@ class Preview extends EventEmitter {
// Set the authorization token
this.options.token = tokenMap[this.file.id];

this.setupUI();
// Do nothing if container is already setup and in the middle of retrying
if (!(this.ui.isSetup() && this.retryCount > 0)) {
this.setupUI();
}

// Load from cache if the current file is valid, otherwise load file info from server
if (checkFileValid(this.file)) {
Expand Down
10 changes: 10 additions & 0 deletions src/lib/PreviewUI.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@ class PreviewUI {
return this.container;
}

/**
* Whether the UI has been setup yet
*
* @public
* @return {boolean} Whether the UI has been setup
*/
isSetup() {
return this.container && this.container.innerHTML !== '';
}

/**
* Shows navigation arrows if there is a need
*
Expand Down
21 changes: 21 additions & 0 deletions src/lib/__tests__/Preview-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,7 @@ describe('lib/Preview', () => {
stubs.checkFileValid = sandbox.stub(file, 'checkFileValid');
stubs.loadFromCache = sandbox.stub(preview, 'loadFromCache');
stubs.cacheFile = sandbox.stub(file, 'cacheFile');
stubs.ui = sandbox.stub(preview.ui, 'isSetup');
});

it('should set the token option', () => {
Expand Down Expand Up @@ -1127,6 +1128,26 @@ describe('lib/Preview', () => {
expect(stubs.loadFromCache).to.not.be.called;
expect(stubs.loadFromServer).to.be.called;
});

it('should setup UI if ui is not setup', () => {
stubs.ui.returns(false);
preview.handleTokenResponse({});
expect(stubs.setupUI).to.be.called;
});

it('should setup UI if not retrying', () => {
stubs.ui.returns(true);
preview.retryCount = 0;
preview.handleTokenResponse({});
expect(stubs.setupUI).to.be.called;
});

it('should not setup UI if UI is setup and is retrying', () => {
stubs.ui.returns(true);
preview.retryCount = 1;
preview.handleTokenResponse({});
expect(stubs.setupUI).to.not.be.called;
});
});

describe('setupUI', () => {
Expand Down
17 changes: 17 additions & 0 deletions src/lib/__tests__/PreviewUI-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,4 +378,21 @@ describe('lib/PreviewUI', () => {
expect(customLogoEl.src).to.equal(url);
});
});

describe('isSetup()', () => {
it('should return false if container is falsy', () => {
ui.container = false;
expect(ui.isSetup()).to.be.false;
});

it('should return false if container innerHTML is empty', () => {
ui.container = { innerHTML: '' };
expect(ui.isSetup()).to.be.false;
});

it('should return true if container innerHTML is not empty', () => {
ui.container = { innerHTML: 'foo' };
expect(ui.isSetup()).to.be.true;
});
});
});

0 comments on commit 6782e47

Please sign in to comment.