Skip to content

Commit

Permalink
feat(perf): Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jstoffan committed Sep 8, 2020
1 parent 7021398 commit 0d0f17c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
7 changes: 7 additions & 0 deletions src/lib/__tests__/Timer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ describe('lib/Timer', () => {
Timer.stop(tag);
expect(Timer.get(tag).elapsed).to.equal(2); // 5 - 3.5 = 1.5, rounded = 2
});

it('should stop and return the value at the given tag', () => {
sandbox.stub(global.performance, 'now').returns(5);
Timer.times[tag] = { start: 3.5 };

expect(Timer.stop(tag).elapsed).to.equal(2);
});
});

describe('get()', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/viewers/BaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -758,10 +758,10 @@ class BaseViewer extends EventEmitter {
* @param {any} [value] - to return if undefined
* @return {any} Value of a viewer option
*/
getViewerOption(option, value = undefined) {
getViewerOption(option) {
const { viewers, viewer } = this.options;
const viewerName = getProp(viewer, 'NAME');
return getProp(viewers, `${viewerName}.${option}`, value);
return getProp(viewers, `${viewerName}.${option}`);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/lib/viewers/doc/DocBaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -665,16 +665,16 @@ class DocBaseViewer extends BaseViewer {
const disableFontFace = Browser.hasFontIssue() || this.getViewerOption('disableFontFace');

// Disable range requests for files smaller than minimum range request size
const isRangeSupported = size >= this.getViewerOption('rangeMinSize', RANGE_REQUEST_MINIMUM_SIZE);
const isRangeSupported = size >= (this.getViewerOption('rangeMinSize') || RANGE_REQUEST_MINIMUM_SIZE);
const isWatermarked = watermarkInfo && watermarkInfo.is_watermarked;
const disableRange = isWatermarked || !isRangeSupported;

// Use larger chunk sizes because we assume that en-US users have better connections to Box's servers
const rangeChunkSizeDefault = location.locale === 'en-US' ? RANGE_CHUNK_SIZE_US : RANGE_CHUNK_SIZE_NON_US;
const rangeChunkSize = this.getViewerOption('rangeChunkSize', rangeChunkSizeDefault);
const rangeChunkSize = this.getViewerOption('rangeChunkSize') || rangeChunkSizeDefault;

// Disable streaming by default unless it is explicitly enabled via options
const disableStream = this.getViewerOption('disableStream', true);
const disableStream = this.getViewerOption('disableStream') !== false;

// If range requests and streaming are disabled, request the gzip compressed version of the representation
this.encoding = disableRange && disableStream ? ENCODING_TYPES.GZIP : undefined;
Expand Down
44 changes: 42 additions & 2 deletions src/lib/viewers/doc/__tests__/DocBaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
ICON_SEARCH,
ICON_THUMBNAILS_TOGGLE,
} from '../../../icons/icons';
import { VIEWER_EVENT, LOAD_METRIC, USER_DOCUMENT_THUMBNAIL_EVENTS } from '../../../events';
import { LOAD_METRIC, RENDER_EVENT, USER_DOCUMENT_THUMBNAIL_EVENTS, VIEWER_EVENT } from '../../../events';
import Timer from '../../../Timer';

const LOAD_TIMEOUT_MS = 180000; // 3 min timeout
Expand Down Expand Up @@ -1145,12 +1145,20 @@ describe('src/lib/viewers/doc/DocBaseViewer', () => {
});
});

it('should not disable streaming', () => {
it('should disable streaming by default', () => {
return docBase.initViewer('').then(() => {
expect(stubs.getDocument).to.be.calledWith(sinon.match({ disableStream: true }));
});
});

it('should enable streaming if the proper option is provided', () => {
stubs.getViewerOption.returns(false);

return docBase.initViewer('').then(() => {
expect(stubs.getDocument).to.be.calledWith(sinon.match({ disableStream: false }));
});
});

it('should enable range requests if the file is greater than 25MB', () => {
docBase.options.file.size = 26500000;

Expand All @@ -1177,6 +1185,16 @@ describe('src/lib/viewers/doc/DocBaseViewer', () => {
});
});

it('should enable range requests if the file is smaller than the provided minimum size', () => {
stubs.getViewerOption.returns(2097152); // 2 MB minimum

docBase.options.file.size = 5242880; // 5 MB file size

return docBase.initViewer('').then(() => {
expect(stubs.getDocument).to.be.calledWith(sinon.match({ disableRange: false }));
});
});

it('should set disableCreateObjectURL to false', () => {
return docBase.initViewer('').then(() => {
expect(stubs.getDocument).to.be.calledWith(sinon.match({ disableCreateObjectURL: false }));
Expand Down Expand Up @@ -1798,10 +1816,13 @@ describe('src/lib/viewers/doc/DocBaseViewer', () => {
};

docBase.somePageRendered = false;
docBase.startPageRendered = false;
stubs.emit = sandbox.stub(docBase, 'emit');
stubs.emitMetric = sandbox.stub(docBase, 'emitMetric');
stubs.initThumbnails = sandbox.stub(docBase, 'initThumbnails');
stubs.hidePreload = sandbox.stub(docBase, 'hidePreload');
stubs.resize = sandbox.stub(docBase, 'resize');
stubs.stop = sandbox.stub(Timer, 'stop').returns({ elapsed: 1000 });
});

it('should emit the pagerender event', () => {
Expand All @@ -1817,6 +1838,25 @@ describe('src/lib/viewers/doc/DocBaseViewer', () => {
expect(docBase.zoomControls.setCurrentScale).to.be.calledWith(docBase.pdfViewer.currentScale);
});

it('should emit render metric event for start page if not already emitted', () => {
docBase.pagerenderedHandler(docBase.event);
expect(stubs.emitMetric).to.be.calledWith({
name: RENDER_EVENT,
data: 1000,
});
});

it('should not emit render metric event if it was already emitted', () => {
docBase.startPageRendered = true;
docBase.pagerenderedHandler(docBase.event);
expect(stubs.emitMetric).not.to.be.called;
});

it('should not emit render metric event if rendered page is not start page', () => {
docBase.pagerenderedHandler({ pageNumber: 5 });
expect(stubs.emitMetric).not.to.be.called;
});

it('should hide the preload and init thumbnails if no pages were previously rendered', () => {
docBase.options.enableThumbnailsSidebar = true;
docBase.pagerenderedHandler(docBase.event);
Expand Down

0 comments on commit 0d0f17c

Please sign in to comment.