Commit 98d00948 authored by Kyle Keating's avatar Kyle Keating Committed by Kyle Keating

Move coerce() into configDefinitions

Previously, coerce was passed to the hostPageSetting. Moving the logic up into getConfig allows any setting to have a coerce method, not just settings pulled from hostPageSetting. Eventually, other settings may need coerce logic such as showHighlights.
parent 294e17a9
...@@ -9,7 +9,10 @@ import { toBoolean } from '../../shared/type-coercions'; ...@@ -9,7 +9,10 @@ import { toBoolean } from '../../shared/type-coercions';
* @typedef ConfigDefinition * @typedef ConfigDefinition
* @prop {(settings: SettingsGetters) => any} getValue - * @prop {(settings: SettingsGetters) => any} getValue -
* Method to retrieve the value from the incoming source * Method to retrieve the value from the incoming source
* * @prop {(value: any) => any} [coerce] - Transform a value's type, value or both
*/
/**
* @typedef {Record<string, ConfigDefinition>} ConfigDefinitionMap * @typedef {Record<string, ConfigDefinition>} ConfigDefinitionMap
*/ */
...@@ -109,10 +112,10 @@ const configDefinitions = { ...@@ -109,10 +112,10 @@ const configDefinitions = {
getValue: settings => settings.hostPageSetting('onLayoutChange'), getValue: settings => settings.hostPageSetting('onLayoutChange'),
}, },
openSidebar: { openSidebar: {
coerce: toBoolean,
getValue: settings => getValue: settings =>
settings.hostPageSetting('openSidebar', { settings.hostPageSetting('openSidebar', {
allowInBrowserExt: true, allowInBrowserExt: true,
coerce: toBoolean,
}), }),
}, },
query: { query: {
...@@ -159,8 +162,9 @@ export function getConfig(appContext = 'annotator', window_ = window) { ...@@ -159,8 +162,9 @@ export function getConfig(appContext = 'annotator', window_ = window) {
let filteredKeys = configurationKeys(appContext); let filteredKeys = configurationKeys(appContext);
filteredKeys.forEach(name => { filteredKeys.forEach(name => {
const configDef = configDefinitions[name]; const configDef = configDefinitions[name];
// Get the value from the configuration source const value = configDef.getValue(settings);
config[name] = configDef.getValue(settings); // Get the value from the configuration source and run through an optional coerce method
config[name] = configDef.coerce ? configDef.coerce(value) : value;
}); });
return config; return config;
......
...@@ -77,7 +77,7 @@ describe('annotator/config/index', function () { ...@@ -77,7 +77,7 @@ describe('annotator/config/index', function () {
it(`returns the ${settingName} value from the host page`, function () { it(`returns the ${settingName} value from the host page`, function () {
const settings = { const settings = {
branding: 'BRANDING_SETTING', branding: 'BRANDING_SETTING',
openSidebar: 'OPEN_SIDEBAR_SETTING', openSidebar: true,
requestConfigFromFrame: 'https://embedder.com', requestConfigFromFrame: 'https://embedder.com',
services: 'SERVICES_SETTING', services: 'SERVICES_SETTING',
}; };
...@@ -91,6 +91,18 @@ describe('annotator/config/index', function () { ...@@ -91,6 +91,18 @@ describe('annotator/config/index', function () {
}); });
} }
); );
describe('coerces values', () => {
it('coerces `openSidebar` from a string to a boolean', () => {
fakeSettingsFrom().hostPageSetting = sinon
.stub()
.withArgs('openSidebar')
.returns('false');
const config = getConfig('all', 'WINDOW');
assert.equal(config.openSidebar, false);
});
});
describe('application contexts', () => { describe('application contexts', () => {
[ [
{ {
......
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