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

Pass scoped context to tutorial providers when building tutorials #22260

Merged
merged 4 commits into from Sep 5, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -23,7 +23,7 @@ export function registerTutorials(server) {
path: '/api/kibana/home/tutorials',
method: ['GET'],
handler: async function (req, reply) {
reply(server.getTutorials());
reply(server.getTutorials(req));
}
});
}
28 changes: 22 additions & 6 deletions src/ui/tutorials_mixin.js
Expand Up @@ -17,24 +17,40 @@
* under the License.
*/

import _ from 'lodash';
import Joi from 'joi';
import { tutorialSchema } from '../core_plugins/kibana/common/tutorials/tutorial_schema';

export function tutorialsMixin(kbnServer, server) {
const tutorials = [];
const tutorialProviders = [];
const scopedTutorialContextFactories = [];

server.decorate('server', 'getTutorials', () => {
return _.cloneDeep(tutorials);
server.decorate('server', 'getTutorials', (request) => {
const initialContext = {};
const scopedContext = scopedTutorialContextFactories.reduce((accumulatedContext, contextFactory) => {
return { ...accumulatedContext, ...contextFactory(request) };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we throw an error if we have two context factories try to set the same values?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no preference.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a strong preference either way, so LGTM!

}, initialContext);

return tutorialProviders.map((tutorialProvider) => {
return tutorialProvider(server, scopedContext);
});
});

server.decorate('server', 'registerTutorial', (specProvider) => {
const { error, value } = Joi.validate(specProvider(server), tutorialSchema);
const emptyContext = {};
const { error } = Joi.validate(specProvider(server, emptyContext), tutorialSchema);

if (error) {
throw new Error(`Unable to register tutorial spec because its invalid. ${error}`);
}

tutorials.push(value);
tutorialProviders.push(specProvider);
});

server.decorate('server', 'addScopedTutorialContextFactory', (scopedTutorialContextFactory) => {
if (typeof scopedTutorialContextFactory !== 'function') {
throw new Error(`Unable to add scoped(request) context factory because you did not provide a function`);
}

scopedTutorialContextFactories.push(scopedTutorialContextFactory);
});
}
86 changes: 86 additions & 0 deletions src/ui/tutorials_mixin.test.js
@@ -0,0 +1,86 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { createServer } from '../test_utils/kbn_server';

const validTutorial = {
id: 'spec1',
category: 'other',
name: 'spec1',
shortDescription: 'short description',
longDescription: 'long description',
onPrem: {
instructionSets: [
{
instructionVariants: [
{
id: 'instructionVariant1',
instructions: [
{}
]
}
]
}
]
}
};

describe('tutorial mixins', () => {

let kbnServer;
beforeEach(async () => {
kbnServer = createServer();
await kbnServer.ready();
});

afterEach(async () => {
await kbnServer.close();
});

describe('scoped context', () => {

const mockRequest = {};
const spacesContextFactory = (request) => {
if (request !== mockRequest) {
throw new Error('context factory not called with request object');
}
return {
spaceId: 'my-space'
};
};
const specProvider = (server, context) => {
const tutorial = { ...validTutorial };
tutorial.shortDescription = `I have been provided with scoped context, spaceId: ${context.spaceId}`;
return tutorial;
};
beforeEach(async () => {
kbnServer.server.addScopedTutorialContextFactory(spacesContextFactory);
kbnServer.server.registerTutorial(specProvider);
});

test('passes scoped context to specProviders', () => {
const tutorials = kbnServer.server.getTutorials(mockRequest);
expect(tutorials.length).toBe(1);
expect(tutorials[0].shortDescription).toBe('I have been provided with scoped context, spaceId: my-space');
});
});

});