Commit cc47a5c8 authored by Robert Knight's avatar Robert Knight

Rename `createSidebarConfig` to `createAppConfig` and add tests

Rename the function that prepares configuration to be passed from the
annotator to the sidebar and notebook apps, since it is no longer used
exclusively by the sidebar, and add tests.
parent 4365a4d2
...@@ -3,7 +3,7 @@ import { useEffect, useRef, useState } from 'preact/hooks'; ...@@ -3,7 +3,7 @@ import { useEffect, useRef, useState } from 'preact/hooks';
import classnames from 'classnames'; import classnames from 'classnames';
import { addConfigFragment } from '../../shared/config-fragment'; import { addConfigFragment } from '../../shared/config-fragment';
import { createSidebarConfig } from '../config/sidebar'; import { createAppConfig } from '../config/app';
/** /**
* @typedef NotebookIframeProps * @typedef NotebookIframeProps
...@@ -18,7 +18,7 @@ import { createSidebarConfig } from '../config/sidebar'; ...@@ -18,7 +18,7 @@ import { createSidebarConfig } from '../config/sidebar';
*/ */
function NotebookIframe({ config, groupId }) { function NotebookIframe({ config, groupId }) {
const notebookAppSrc = addConfigFragment(config.notebookAppUrl, { const notebookAppSrc = addConfigFragment(config.notebookAppUrl, {
...createSidebarConfig(config), ...createAppConfig(config),
// Explicity set the "focused" group // Explicity set the "focused" group
group: groupId, group: groupId,
......
/** /**
* Create the JSON-serializable subset of annotator configuration that should * Create the JSON-serializable subset of annotator configuration that should
* be passed to the sidebar application. * be passed to the sidebar or notebook applications.
* *
* @param {Record<string, any>} config * @param {Record<string, any>} config
* @return {object}
*/ */
export function createSidebarConfig(config) { export function createAppConfig(config) {
const sidebarConfig = { ...config }; const appConfig = { ...config };
// Some config settings are not JSON-stringifiable (e.g. JavaScript // Some config settings are not JSON-stringifiable (e.g. JavaScript
// functions) and will be omitted when the config is JSON-stringified. // functions) and will be omitted when the config is JSON-stringified.
// Add a JSON-stringifiable option for each of these so that the sidebar can // Add a JSON-stringifiable option for each of these so that the sidebar can
// at least know whether the callback functions were provided or not. // at least know whether the callback functions were provided or not.
if (sidebarConfig.services?.length > 0) { if (appConfig.services?.length > 0) {
const service = sidebarConfig.services[0]; const service = appConfig.services[0];
if (service.onLoginRequest) { if (service.onLoginRequest) {
service.onLoginRequestProvided = true; service.onLoginRequestProvided = true;
} }
...@@ -35,7 +36,7 @@ export function createSidebarConfig(config) { ...@@ -35,7 +36,7 @@ export function createSidebarConfig(config) {
// nb. We don't currently strip all the annotator-only properties here. // nb. We don't currently strip all the annotator-only properties here.
// That's OK because validation / filtering happens in the sidebar app itself. // That's OK because validation / filtering happens in the sidebar app itself.
// It just results in unnecessary content in the sidebar iframe's URL string. // It just results in unnecessary content in the sidebar iframe's URL string.
['notebookAppUrl', 'sidebarAppUrl'].forEach(key => delete sidebarConfig[key]); ['notebookAppUrl', 'sidebarAppUrl'].forEach(key => delete appConfig[key]);
return sidebarConfig; return appConfig;
} }
import { createAppConfig } from '../app';
describe('createAppConfig', () => {
it('copies config values', () => {
const config = {
showHighlights: true,
appType: 'via',
};
const sidebarConfig = createAppConfig(config);
assert.deepEqual(sidebarConfig, {
showHighlights: true,
appType: 'via',
});
});
it('does not copy "notebookAppUrl" or "sidebarAppUrl" properties', () => {
const config = {
showHighlights: true,
notebookAppUrl: 'https://hypothes.is/notebook.html',
sidebarAppUrl: 'https://hypothes.is/app.html',
};
const sidebarConfig = createAppConfig(config);
assert.deepEqual(sidebarConfig, {
showHighlights: true,
});
});
it('sets boolean properties to indicate presence of callbacks', () => {
const callbacks = [
'onLoginRequest',
'onLogoutRequest',
'onSignupRequest',
'onProfileRequest',
'onHelpRequest',
];
const service = {};
for (let callback of callbacks) {
service[callback] = sinon.stub();
}
const config = {
services: [service],
};
const sidebarConfig = createAppConfig(config);
const sidebarServiceConfig = sidebarConfig.services[0];
for (let callback of callbacks) {
assert.equal(sidebarServiceConfig[callback + 'Provided'], true);
}
});
});
...@@ -6,7 +6,7 @@ import { ListenerCollection } from '../shared/listener-collection'; ...@@ -6,7 +6,7 @@ import { ListenerCollection } from '../shared/listener-collection';
import { annotationCounts } from './annotation-counts'; import { annotationCounts } from './annotation-counts';
import BucketBar from './bucket-bar'; import BucketBar from './bucket-bar';
import { createSidebarConfig } from './config/sidebar'; import { createAppConfig } from './config/app';
import { features } from './features'; import { features } from './features';
import sidebarTrigger from './sidebar-trigger'; import sidebarTrigger from './sidebar-trigger';
import { ToolbarController } from './toolbar'; import { ToolbarController } from './toolbar';
...@@ -31,7 +31,7 @@ export const MIN_RESIZE = 280; ...@@ -31,7 +31,7 @@ export const MIN_RESIZE = 280;
function createSidebarIframe(config) { function createSidebarIframe(config) {
const sidebarAppSrc = addConfigFragment( const sidebarAppSrc = addConfigFragment(
config.sidebarAppUrl, config.sidebarAppUrl,
createSidebarConfig(config) createAppConfig(config)
); );
const sidebarFrame = document.createElement('iframe'); const sidebarFrame = document.createElement('iframe');
......
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