Commit 64ad7b38 authored by Lyza Danger Gardner's avatar Lyza Danger Gardner

Refine util for determining whether to auto-display tutorial

parent 160b8923
'use strict';
const serviceConfig = require('../service-config');
/**
* Returns true if the sidebar tutorial has to be shown to a user for a given session.
* @deprecated To be removed once preact help/tutorial panel is in place
*/
function shouldShowSidebarTutorial(sessionState) {
if (sessionState.preferences.show_sidebar_tutorial) {
......@@ -10,6 +13,35 @@ function shouldShowSidebarTutorial(sessionState) {
return false;
}
/**
* The following things must all be true for the tutorial component to auto-display
* on app launch:
* - The app must be operating within the "sidebar" (i.e. not single-annotation
* or stream mode); AND
* - No configuration is present in `settings.services` indicating
* that the host wants to handle its own help requests (i.e. no event handler
* is provided to intercept the default help panel), AND
* - A user profile is loaded in the current state that indicates a `true` value
* for the `show_sidebar_tutorial` preference (i.e. the tutorial has not been
* dismissed by this user yet). This implies the presence of a profile, which
* in turn implies that there is an authenticated user.
*
* @param {boolean} isSidebar - is the app currently displayed in a sidebar?
* @param {Object} sessionState - `session` state from the store
* @param {Object} settings - app configuration/settings
* @return {boolean} - Tutorial panel should be displayed automatically
*/
function shouldAutoDisplayTutorial(isSidebar, sessionState, settings) {
const shouldShowBasedOnProfile =
typeof sessionState.preferences === 'object' &&
!!sessionState.preferences.show_sidebar_tutorial;
const service = serviceConfig(settings) || {};
return (
isSidebar && !service.onHelpRequestProvided && shouldShowBasedOnProfile
);
}
module.exports = {
shouldShowSidebarTutorial: shouldShowSidebarTutorial,
shouldAutoDisplayTutorial: shouldAutoDisplayTutorial,
};
......@@ -2,7 +2,8 @@
const sessionUtil = require('../session-util');
describe('sessionUtil.shouldShowSidebarTutorial', function() {
describe('sidebar.util.session-util', () => {
describe('#shouldShowSidebarTutorial', () => {
it('shows sidebar tutorial if the settings object has the show_sidebar_tutorial key set', function() {
const sessionState = {
preferences: {
......@@ -22,4 +23,73 @@ describe('sessionUtil.shouldShowSidebarTutorial', function() {
assert.isFalse(sessionUtil.shouldShowSidebarTutorial(sessionState));
});
});
describe('#shouldAutoDisplayTutorial', () => {
[
{
// The only "true" state
description: 'in sidebar with loaded user preference to show tutorial',
isSidebar: true,
sessionState: { preferences: { show_sidebar_tutorial: true } },
settings: {},
expected: true,
},
{
description: 'in sidebar with no loaded user profile',
isSidebar: true,
sessionState: {},
settings: {},
expected: false,
},
{
description: 'not in sidebar with no loaded user profile',
isSidebar: false,
sessionState: {},
settings: {},
expected: false,
},
{
description:
'in sidebar with loaded user preference not to show tutorial',
isSidebar: true,
sessionState: { preferences: { show_sidebar_tutorial: false } },
settings: {},
expected: false,
},
{
description:
'in sidebar with loaded user preference to show tutorial and configured help service',
isSidebar: true,
sessionState: { preferences: { show_sidebar_tutorial: true } },
settings: { services: [{ onHelpRequestProvided: true }] },
expected: false,
},
{
description:
'not in sidebar with loaded user preference to show tutorial and configured help service',
isSidebar: false,
sessionState: { preferences: { show_sidebar_tutorial: true } },
settings: { services: [{ onHelpRequestProvided: true }] },
expected: false,
},
{
description:
'not in sidebar with no loaded user profile and configured help service',
isSidebar: false,
sessionState: {},
settings: { services: [{ onHelpRequestProvided: true }] },
expected: false,
},
].forEach(fixture => {
it(`should calculate auto-display to be ${fixture.expected} when ${fixture.description}`, () => {
const shouldDisplay = sessionUtil.shouldAutoDisplayTutorial(
fixture.isSidebar,
fixture.sessionState,
fixture.settings
);
assert.equal(shouldDisplay, fixture.expected);
});
});
});
});
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