Skip to content

Commit

Permalink
chore(mediaviewer): fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingze Xiao committed Oct 24, 2019
1 parent a91af2a commit 66ee34b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/lib/viewers/media/MediaBaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ class MediaBaseViewer extends BaseViewer {
return;
}

// If it's already loaded, this handler should be triggered by refreshing token,
// so we want to continue playing from the previous time, and don't need to load UI again.
if (this.loaded) {
this.play(this.currentTime);
return;
Expand Down Expand Up @@ -273,9 +275,13 @@ class MediaBaseViewer extends BaseViewer {
* @private
* @return {Promise<Object>}
*/
refreshToken = async () => {
const tokenMap = await getTokens(this.options.file.id, this.options.tokenGenerator);
return tokenMap[this.options.file.id];
refreshToken = () => {
return getTokens(this.options.file.id, this.options.tokenGenerator).then(tokenOrTokenMap => {
if (!tokenOrTokenMap || typeof tokenOrTokenMap === 'string') {
return tokenOrTokenMap;
}
return tokenOrTokenMap[this.options.file.id];
});
};

/**
Expand All @@ -295,7 +301,11 @@ class MediaBaseViewer extends BaseViewer {
const errorDetails = errorCode ? { error_code: errorCode, error_message: errorMessage } : {};

// refresh the token if token expired
if (errorCode === MediaError.MEDIA_ERR_NETWORK && errorMessage.includes(MEDIA_TOKEN_EXPIRE_ERROR)) {
if (
errorCode === MediaError.MEDIA_ERR_NETWORK &&
errorMessage.includes(MEDIA_TOKEN_EXPIRE_ERROR) &&
typeof this.options.tokenGenerator === 'function'
) {
this.refreshToken().then(newToken => {
const { currentTime } = this.mediaEl;
this.currentTime = currentTime;
Expand Down
6 changes: 6 additions & 0 deletions src/lib/viewers/media/__tests__/MediaBaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,12 @@ describe('lib/viewers/media/MediaBaseViewer', () => {
});

describe('errorHandler()', () => {
before(() => {
window.MediaError = {
MEDIA_ERR_NETWORK: 2,
};
});

it('should handle download error if the viewer was not yet loaded', () => {
media.mediaUrl = 'foo';
sandbox.stub(media, 'isLoaded').returns(false);
Expand Down

0 comments on commit 66ee34b

Please sign in to comment.