Commit 0fbe2470 authored by Robert Knight's avatar Robert Knight Committed by GitHub

Merge pull request #447 from hypothesis/fix-openLoginForm-and-openSidebar

Enable reading openLoginForm and openSidebar from the host page
parents cd7d8b27 6f52dbbb
...@@ -14,8 +14,8 @@ function configFrom(window_) { ...@@ -14,8 +14,8 @@ function configFrom(window_) {
app: settings.app, app: settings.app,
query: settings.query, query: settings.query,
annotations: settings.annotations, annotations: settings.annotations,
openLoginForm: settings.hostPageSetting('openLoginForm'), openLoginForm: settings.hostPageSetting('openLoginForm', {allowInBrowserExt: true}),
openSidebar: settings.hostPageSetting('openSidebar'), openSidebar: settings.hostPageSetting('openSidebar', {allowInBrowserExt: true}),
showHighlights: settings.hostPageSetting('showHighlights'), showHighlights: settings.hostPageSetting('showHighlights'),
branding: settings.hostPageSetting('branding'), branding: settings.hostPageSetting('branding'),
assetRoot: settings.hostPageSetting('assetRoot'), assetRoot: settings.hostPageSetting('assetRoot'),
......
...@@ -92,8 +92,10 @@ function settingsFrom(window_) { ...@@ -92,8 +92,10 @@ function settingsFrom(window_) {
return jsonConfigs.query || queryFromURL(); return jsonConfigs.query || queryFromURL();
} }
function hostPageSetting(name) { function hostPageSetting(name, options = {}) {
if (isBrowserExtension(app())) { var allowInBrowserExt = options.allowInBrowserExt || false;
if (!allowInBrowserExt && isBrowserExtension(app())) {
return null; return null;
} }
......
...@@ -61,18 +61,40 @@ describe('annotator.config.index', function() { ...@@ -61,18 +61,40 @@ describe('annotator.config.index', function() {
[ [
'openLoginForm', 'openLoginForm',
'openSidebar', 'openSidebar',
].forEach(function(settingName) {
it('reads ' + settingName + ' from the host page, even when in a browser extension', function() {
configFrom('WINDOW');
assert.calledWithExactly(
fakeSettingsFrom().hostPageSetting,
settingName, {allowInBrowserExt: true}
);
});
});
[
'showHighlights', 'showHighlights',
'branding', 'branding',
'assetRoot', 'assetRoot',
'sidebarAppUrl', 'sidebarAppUrl',
'services', 'services',
].forEach(function(settingName) { ].forEach(function(settingName) {
it('makes a hostPageSetting for ' + settingName, function() { it('reads ' + settingName + ' from the host page only when in an embedded client', function() {
configFrom('WINDOW'); configFrom('WINDOW');
assert.calledWithExactly(fakeSettingsFrom().hostPageSetting, settingName); assert.calledWithExactly(fakeSettingsFrom().hostPageSetting, settingName);
}); });
});
[
'openLoginForm',
'openSidebar',
'showHighlights',
'branding',
'assetRoot',
'sidebarAppUrl',
'services',
].forEach(function(settingName) {
it('returns the ' + settingName + ' value from the host page', function() { it('returns the ' + settingName + ' value from the host page', function() {
var settings = { var settings = {
'openLoginForm': 'OPEN_LOGIN_FORM_SETTING', 'openLoginForm': 'OPEN_LOGIN_FORM_SETTING',
......
...@@ -267,69 +267,97 @@ describe('annotator.config.settingsFrom', function() { ...@@ -267,69 +267,97 @@ describe('annotator.config.settingsFrom', function() {
}); });
describe('#hostPageSetting', function() { describe('#hostPageSetting', function() {
context('when the client is from a browser extension', function() { [
beforeEach('configure a browser extension client', function() { {
fakeIsBrowserExtension.returns(true); when: 'the client is embedded in a web page',
}); specify: 'it returns setting values from window.hypothesisConfig()',
isBrowserExtension: false,
it('always returns null', function() { configFuncSettings: {foo: 'configFuncValue'},
// These settings in the host page should be ignored. jsonSettings: {},
fakeConfigFuncSettingsFrom.returns({foo: 'bar'}); expected: 'configFuncValue',
fakeSharedSettings.jsonConfigsFrom.returns({foo: 'bar'}); },
{
assert.isNull(settingsFrom(fakeWindow()).hostPageSetting('foo')); when: 'the client is embedded in a web page',
}); specify: 'it returns setting values from js-hypothesis-config objects',
}); isBrowserExtension: false,
configFuncSettings: {},
context('when the client is embedded in a web page', function() { jsonSettings: {foo: 'jsonValue'},
beforeEach('configure an embedded client', function() { expected: 'jsonValue',
fakeIsBrowserExtension.returns(false); },
}); {
when: 'the client is embedded in a web page',
it('returns setting values from window.hypothesisConfig()', function() { specify: 'hypothesisConfig() settings override js-hypothesis-config ones',
fakeConfigFuncSettingsFrom.returns({foo: 'bar'}); isBrowserExtension: false,
configFuncSettings: {foo: 'configFuncValue'},
assert.equal(settingsFrom(fakeWindow()).hostPageSetting('foo'), 'bar'); jsonSettings: {foo: 'jsonValue'},
}); expected: 'configFuncValue',
},
it('returns setting values from js-hypothesis-config scripts', function() { {
fakeSharedSettings.jsonConfigsFrom.returns({foo: 'bar'}); when: 'the client is embedded in a web page',
specify: 'even a null from hypothesisConfig() overrides js-hypothesis-config',
assert.equal(settingsFrom(fakeWindow()).hostPageSetting('foo'), 'bar'); isBrowserExtension: false,
}); configFuncSettings: {foo: null},
jsonSettings: {foo: 'jsonValue'},
specify('hypothesisConfig() overrides js-hypothesis-config', function() { expected: null,
fakeConfigFuncSettingsFrom.returns({ },
foo: 'fooFromHypothesisConfig', {
}); when: 'the client is embedded in a web page',
fakeSharedSettings.jsonConfigsFrom.returns({ specify: 'even an undefined from hypothesisConfig() overrides js-hypothesis-config',
foo: 'fooFromJsHypothesisConfigScript', isBrowserExtension: false,
}); configFuncSettings: {foo: undefined},
jsonSettings: {foo: 'jsonValue'},
assert.equal( expected: undefined,
settingsFrom(fakeWindow()).hostPageSetting('foo'), },
'fooFromHypothesisConfig' {
); when: 'the client is embedded in a web page',
}); specify: "it returns undefined if the setting isn't defined anywhere",
isBrowserExtension: false,
[ configFuncSettings: {},
null, jsonSettings: {},
undefined, expected: undefined,
].forEach(function(returnValue) { },
specify('even a ' + returnValue + ' from hypothesisConfig() overrides js-hypothesis-configs', function() { {
fakeConfigFuncSettingsFrom.returns({foo: returnValue}); when: 'the client is in a browser extension',
fakeSharedSettings.jsonConfigsFrom.returns({foo: 'bar'}); specify: 'it always returns null',
isBrowserExtension: true,
assert.equal(settingsFrom(fakeWindow()).hostPageSetting('foo'), returnValue); configFuncSettings: {foo: 'configFuncValue'},
jsonSettings: {foo: 'jsonValue'},
expected: null,
},
{
when: 'the client is in a browser extension and allowInBrowserExt: true is given',
specify: 'it returns settings from window.hypothesisConfig()',
isBrowserExtension: true,
allowInBrowserExt: true,
configFuncSettings: {foo: 'configFuncValue'},
jsonSettings: {},
expected: 'configFuncValue',
},
{
when: 'the client is in a browser extension and allowInBrowserExt: true is given',
specify: 'it returns settings from js-hypothesis-configs',
isBrowserExtension: true,
allowInBrowserExt: true,
configFuncSettings: {},
jsonSettings: {foo: 'jsonValue'},
expected: 'jsonValue',
},
].forEach(function(test) {
context(test.when, function() {
specify(test.specify, function() {
fakeIsBrowserExtension.returns(test.isBrowserExtension);
fakeConfigFuncSettingsFrom.returns(test.configFuncSettings);
fakeSharedSettings.jsonConfigsFrom.returns(test.jsonSettings);
var settings = settingsFrom(fakeWindow());
var setting = settings.hostPageSetting(
'foo',
{allowInBrowserExt: test.allowInBrowserExt || false}
);
assert.equal(setting, test.expected);
}); });
}); });
it("returns undefined if the setting isn't defined anywhere", function() {
fakeConfigFuncSettingsFrom.returns({});
fakeSharedSettings.jsonConfigsFrom.returns({});
assert.isUndefined(settingsFrom(fakeWindow()).hostPageSetting('foo'));
});
}); });
}); });
}); });
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment