Skip to content

Commit

Permalink
test(jest): Handle console errors and promise rejections
Browse files Browse the repository at this point in the history
  • Loading branch information
jstoffan committed Sep 24, 2020
1 parent b865540 commit 911e5df
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 84 deletions.
11 changes: 6 additions & 5 deletions src/lib/__tests__/DownloadReachability-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import DownloadReachability from '../DownloadReachability';
const DOWNLOAD_NOTIFICATION_SHOWN_KEY = 'download_host_notification_shown';
const DOWNLOAD_HOST_FALLBACK_KEY = 'download_host_fallback';

describe('lib/DownloadReachability', () => {
jest.mock('../util', () => ({
isLocalStorageAvailable: jest.fn(() => true),
openUrlInsideIframe: jest.fn(),
}));

describe.only('lib/DownloadReachability', () => {
beforeEach(() => {
jest.spyOn(DownloadReachability, 'isStorageAvailable').mockReturnValue(true);

Expand Down Expand Up @@ -144,7 +149,6 @@ describe('lib/DownloadReachability', () => {

test('should download with default host if download host is blocked', () => {
jest.spyOn(DownloadReachability, 'isDownloadHostBlocked').mockReturnValue(true);
jest.spyOn(util, 'openUrlInsideIframe');

const downloadUrl = 'https://custom.boxcloud.com/blah';
const expected = 'https://dl.boxcloud.com/blah';
Expand All @@ -157,7 +161,6 @@ describe('lib/DownloadReachability', () => {
test('should download with default host if download host is already default', () => {
jest.spyOn(DownloadReachability, 'isDownloadHostBlocked').mockReturnValue(false);
jest.spyOn(DownloadReachability, 'isCustomDownloadHost').mockReturnValue(false);
jest.spyOn(util, 'openUrlInsideIframe');

const downloadUrl = 'https://dl.boxcloud.com/blah';

Expand All @@ -169,7 +172,6 @@ describe('lib/DownloadReachability', () => {
test('should download with the custom download host if host is not blocked', () => {
jest.spyOn(DownloadReachability, 'isDownloadHostBlocked').mockReturnValue(false);
jest.spyOn(DownloadReachability, 'isCustomDownloadHost').mockReturnValue(true);
jest.spyOn(util, 'openUrlInsideIframe');

const downloadUrl = 'https://custom.boxcloud.com/blah';

Expand All @@ -182,7 +184,6 @@ describe('lib/DownloadReachability', () => {
jest.spyOn(DownloadReachability, 'isDownloadHostBlocked').mockReturnValue(false);
jest.spyOn(DownloadReachability, 'isCustomDownloadHost').mockReturnValue(true);
jest.spyOn(downloadReachability, 'setDownloadReachability').mockResolvedValue(false);
jest.spyOn(util, 'openUrlInsideIframe');

const downloadUrl = 'https://custom.boxcloud.com/blah';

Expand Down
15 changes: 7 additions & 8 deletions src/lib/__tests__/Preview-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ describe('lib/Preview', () => {
beforeEach(() => {
stubs.checkFileValid = jest.spyOn(file, 'checkFileValid').mockImplementation();
stubs.cacheFile = jest.spyOn(file, 'cacheFile').mockImplementation();
stubs.error = jest.spyOn(console, 'error').mockImplementation();
stubs.consoleError = jest.spyOn(console, 'error').mockImplementation();
stubs.emitPreviewError = jest.spyOn(preview, 'emitPreviewError').mockImplementation();
});

Expand Down Expand Up @@ -441,7 +441,7 @@ describe('lib/Preview', () => {

preview.updateFileCache(files);
expect(stubs.cacheFile).toBeCalledTimes(1);
expect(stubs.error).toBeCalledTimes(1);
expect(stubs.consoleError).toBeCalledTimes(1);
expect(stubs.emitPreviewError).toBeCalledTimes(1);
});

Expand All @@ -457,7 +457,7 @@ describe('lib/Preview', () => {

preview.updateFileCache(files);
expect(stubs.cacheFile).not.toBeCalled();
expect(stubs.error).not.toBeCalled();
expect(stubs.consoleError).not.toBeCalled();
});
});

Expand Down Expand Up @@ -2055,8 +2055,7 @@ describe('lib/Preview', () => {
});

test('should reset the log retry count if the post fails and retry limit has been reached', () => {
const promiseReject = Promise.reject({}); // eslint-disable-line prefer-promise-reject-errors
jest.spyOn(stubs.api, 'post').mockReturnValue(promiseReject);
jest.spyOn(stubs.api, 'post').mockRejectedValue({});
preview.logRetryCount = 3;
preview.logRetryTimeout = true;

Expand Down Expand Up @@ -2084,9 +2083,9 @@ describe('lib/Preview', () => {
beforeEach(() => {
jest.useFakeTimers();

stubs.uncacheFile = jest.spyOn(file, 'uncacheFile');
stubs.triggerError = jest.spyOn(preview, 'triggerError');
stubs.load = jest.spyOn(preview, 'load');
stubs.uncacheFile = jest.spyOn(file, 'uncacheFile').mockImplementation();
stubs.triggerError = jest.spyOn(preview, 'triggerError').mockImplementation();
stubs.load = jest.spyOn(preview, 'load').mockImplementation();
stubs.error = {
response: {
status: 400,
Expand Down
64 changes: 33 additions & 31 deletions src/lib/__tests__/RepStatus-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,64 +103,66 @@ describe('lib/RepStatus', () => {

describe('updateStatus()', () => {
const state = 'success';

beforeEach(() => {
jest.spyOn(repStatus, 'handleResponse');
jest.spyOn(repStatus, 'handleResponse').mockImplementation();
});

test('should fetch latest status', () => {
sandbox
.mock(repStatus.api)
.expects('get')
.returns(
Promise.resolve({
status: {
state,
},
}),
);
jest.spyOn(repStatus.api, 'get').mockResolvedValue({
info: {},
status: {
state,
},
});

return repStatus.updateStatus().then(() => {
expect(repStatus.api.get).toBeCalled();
expect(repStatus.representation.status.state).toBe(state);
expect(repStatus.handleResponse).toBeCalled();
});
});

test('should update provided metadata', () => {
sandbox
.mock(repStatus.api)
.expects('get')
.returns(
Promise.resolve({
status: {
state,
},
metadata: {
pages: 10,
},
}),
);
jest.spyOn(repStatus.api, 'get').mockResolvedValue({
info: {},
metadata: {
pages: 10,
},
status: {
state,
},
});

return repStatus.updateStatus().then(() => {
expect(repStatus.representation.status.state).toBe(state);
expect(repStatus.api.get).toBeCalled();
expect(repStatus.handleResponse).toBeCalled();
expect(repStatus.representation.status.state).toBe(state);
expect(repStatus.representation.metadata.pages).toBe(10);
});
});

test('should return a resolved promise if there is no info url', () => {
sandbox
.mock(Api.prototype)
.expects('get')
.never();
jest.spyOn(repStatus.api, 'get');

repStatus.infoUrl = '';
expect(repStatus.updateStatus()).toBeInstanceOf(Promise);
expect(repStatus.api.get).not.toBeCalled();
});

test('should start a convert time Timer', () => {
jest.spyOn(repStatus.api, 'get').mockResolvedValue({
info: {},
status: {
state,
},
});

const tag = Timer.createTag(fileId, LOAD_METRIC.convertTime);
repStatus.updateStatus();

expect(Timer.get(tag)).toBeDefined();
return repStatus.updateStatus().then(() => {
expect(Timer.get(tag)).toBeDefined();
});
});
});

Expand Down
1 change: 1 addition & 0 deletions src/lib/__tests__/ThumbnailsSidebar-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe('ThumbnailsSidebar', () => {
beforeEach(() => {
fixture.load('__tests__/ThumbnailsSidebar-test.html');

stubs.consoleError = jest.spyOn(console, 'error').mockImplementation();
stubs.raf = jest.spyOn(window, 'requestAnimationFrame').mockImplementation(callback => callback());

stubs.getViewport = jest.fn();
Expand Down
2 changes: 2 additions & 0 deletions src/lib/__tests__/VirtualScroller-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ describe('VirtualScroller', () => {
test('should still render the item even if renderItemFn throws an error', () => {
const renderedThumbnail = document.createElement('button');
renderedThumbnail.className = 'rendered-thumbnail';

stubs.consoleError = jest.spyOn(console, 'error').mockImplementation();
stubs.renderItemFn = jest.fn(() => {
throw new Error();
});
Expand Down
6 changes: 3 additions & 3 deletions src/lib/__tests__/metadataAPI-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ describe('metadataAPI', () => {
beforeEach(() => {
stubs = {};
stubs.metaDataAPI = new MetadataAPI(new Api());
stubs.get = jest.spyOn(Api.prototype, 'get');
stubs.get = jest.spyOn(Api.prototype, 'get').mockImplementation();
});

describe('getXrefsMetadata()', () => {
test('Should reject the promise if id is not provided on the file', () => {
return stubs.metaDataAPI.getXrefsMetadata(null, 'autocad').catch(err => {
expect(stubs.get).not.toBeCalled();
expect(err instanceof Error).toBe(true);
expect(err).toBeInstanceOf(Error);
});
});

test('Should reject the promise if template is not provided on the file', () => {
return stubs.metaDataAPI.getXrefsMetadata('123').catch(err => {
expect(stubs.get).not.toBeCalled();
expect(err instanceof Error).toBe(true);
expect(err).toBeInstanceOf(Error);
});
});

Expand Down
16 changes: 0 additions & 16 deletions src/lib/__tests__/util-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,22 +259,6 @@ describe('lib/util', () => {
expect(head.querySelector('script[src="foo"]') instanceof HTMLScriptElement).toBeTruthy();
expect(head.querySelector('script[src="bar"]') instanceof HTMLScriptElement).toBeTruthy();
});

test.skip('should disable AMD until scripts are loaded or fail to load', () => {
/* eslint-disable require-jsdoc */
const defineFunc = () => {};
/* eslint-enable require-jsdoc */

defineFunc.amd = { jquery: '' };
window.define = defineFunc;

const promise = util.loadScripts(['foo', 'bar'], true);
expect(window.define).toBeUndefined();

return promise.then(() => {
expect(window.define).toBe(defineFunc);
});
});
});

describe('findScriptLocation()', () => {
Expand Down
3 changes: 3 additions & 0 deletions src/lib/viewers/doc/__tests__/DocBaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1325,6 +1325,7 @@ describe('src/lib/viewers/doc/DocBaseViewer', () => {
url: 'url',
};

stubs.consoleError = jest.spyOn(console, 'error').mockImplementation();
stubs.handleDownloadError = jest.spyOn(docBase, 'handleDownloadError').mockImplementation();
stubs.getDocument.mockReturnValue({ promise: Promise.reject(doc) });
docBase.options.location = {
Expand Down Expand Up @@ -2243,6 +2244,8 @@ describe('src/lib/viewers/doc/DocBaseViewer', () => {
});

test('should return undefined if an invalid unit is passed', () => {
jest.spyOn(console, 'error').mockImplementation();

const startAt = {
value: 3,
unit: 'foo',
Expand Down
Loading

0 comments on commit 911e5df

Please sign in to comment.