Skip to content

Commit

Permalink
Pass scoped context to tutorial providers when building tutorials (#2…
Browse files Browse the repository at this point in the history
…2260)

* Pass scoped context to tutorial providers when building tutorials

* only generated scoped context one time

* spelling
  • Loading branch information
nreese committed Sep 5, 2018
1 parent 2938d94 commit b26e2b4
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 7 deletions.
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) };
}, 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');
});
});

});


0 comments on commit b26e2b4

Please sign in to comment.