Skip to content

Commit

Permalink
feat: 🎸 expose .fork() method in plugin contract
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Feb 12, 2020
1 parent 1c49bfc commit 7875f15
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/plugins/expressions/common/service/expressions_services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@ export class ExpressionsService {
context?: ExtraContext
): Promise<Output> => this.executor.run<Input, Output, ExtraContext>(ast, input, context);

/**
* Create a new instance of `ExpressionsService`. The new instance inherits
* all state of the original `ExpressionsService`, including all expression
* types, expression functions and context. Also, all new types and functions
* registered in the original services AFTER the forking event will be
* available in the forked instance. However, all new types and functions
* registered in the forked instances will NOT be available to the original
* service.
*/
public readonly fork = (): ExpressionsService => {
const executor = this.executor.fork();
const renderers = this.renderers;
Expand All @@ -139,7 +148,7 @@ export class ExpressionsService {
};

public setup() {
const { executor, renderers, registerFunction, run } = this;
const { executor, renderers, registerFunction, run, fork } = this;

const getFunction = executor.getFunction.bind(executor);
const getFunctions = executor.getFunctions.bind(executor);
Expand All @@ -151,6 +160,7 @@ export class ExpressionsService {
const registerType = executor.registerType.bind(executor);

return {
fork,
getFunction,
getFunctions,
getRenderer,
Expand Down
1 change: 1 addition & 0 deletions src/plugins/expressions/public/mocks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export type Start = jest.Mocked<ExpressionsStart>;

const createSetupContract = (): Setup => {
const setupContract: Setup = {
fork: jest.fn(),
getFunction: jest.fn(),
getFunctions: jest.fn(),
getRenderer: jest.fn(),
Expand Down
8 changes: 8 additions & 0 deletions src/plugins/expressions/public/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import { expressionsPluginMock } from './mocks';
import { add } from '../common/test_helpers/expression_functions/add';
import { ExpressionsService } from '../common';

describe('ExpressionsPublicPlugin', () => {
test('can instantiate from mocks', async () => {
Expand All @@ -27,6 +28,13 @@ describe('ExpressionsPublicPlugin', () => {
});

describe('setup contract', () => {
test('.fork() method returns ExpressionsService', async () => {
const { setup } = await expressionsPluginMock.createPlugin();
const fork = setup.fork();

expect(fork).toBeInstanceOf(ExpressionsService);
});

describe('.registerFunction()', () => {
test('can register a function', async () => {
const { setup } = await expressionsPluginMock.createPlugin();
Expand Down

0 comments on commit 7875f15

Please sign in to comment.