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

Don't throw if hypothesisConfig() isn't a function #428

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
8 changes: 2 additions & 6 deletions src/annotator/config/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,9 @@ function query(url) {
*
* If there's no window.hypothesisConfig() function then return {}.
*
* If there is a window.hypothesisConfig but it isn't a function then throw an
* error.
*
* @param {Window} window_ - The window to search for a hypothesisConfig() function
* @return {Object} - Any config settings returned by hypothesisConfig()
*
* @throws {TypeError} - If window.hypothesisConfig() isn't a function
*
*/
function configFuncSettingsFrom(window_) {
if (!window_.hasOwnProperty('hypothesisConfig')) {
Expand All @@ -100,7 +95,8 @@ function configFuncSettingsFrom(window_) {

if (typeof window_.hypothesisConfig !== 'function') {
var docs = 'https://h.readthedocs.io/projects/client/en/latest/publishers/config/#window.hypothesisConfig';
throw new TypeError('hypothesisConfig must be a function, see: ' + docs);
console.warn('hypothesisConfig must be a function, see: ' + docs);
return {};
}

return window_.hypothesisConfig();
Expand Down
8 changes: 0 additions & 8 deletions src/annotator/config/test/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,6 @@ describe('annotator.config', function() {
});
});

context('when configFuncSettingsFrom() throws an error', function() {
it('throws the same error', function() {
fakeSettings.configFuncSettingsFrom.throws(new TypeError());

assert.throws(function() { configFrom(fakeWindow()); }, TypeError);
});
});

describe('showHighlights', function() {
[
{
Expand Down
31 changes: 25 additions & 6 deletions src/annotator/config/test/settings-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@

var settings = require('../settings');

var sandbox = sinon.sandbox.create();

describe('annotation.config.settings', function() {

afterEach('reset the sandbox', function() {
sandbox.restore();
});

describe('#app', function() {
function appendLinkToDocument(href) {
var link = document.createElement('link');
Expand Down Expand Up @@ -197,13 +204,25 @@ describe('annotation.config.settings', function() {
});

context("when window.hypothesisConfig() isn't a function", function() {
it('throws an error', function() {
var fakeWindow = { hypothesisConfig: 42 };
beforeEach('stub console.warn()', function() {
sandbox.stub(console, 'warn');
});

assert.throws(
function() { settings.configFuncSettingsFrom(fakeWindow); },
TypeError
);
function fakeWindow() {
return {hypothesisConfig: 42};
}

it('returns {}', function() {
assert.deepEqual(settings.configFuncSettingsFrom(fakeWindow()), {});
});

it('logs a warning', function() {
settings.configFuncSettingsFrom(fakeWindow());

assert.calledOnce(console.warn);
assert.isTrue(console.warn.firstCall.args[0].startsWith(
'hypothesisConfig must be a function'
));
});
});

Expand Down