Skip to content

Commit

Permalink
[canvas] Fix Storybook service decorator (#104750)
Browse files Browse the repository at this point in the history
  • Loading branch information
clintandrewhall authored Jul 8, 2021
1 parent 85c93b8 commit dc81d13
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
2 changes: 1 addition & 1 deletion x-pack/plugins/canvas/storybook/decorators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ export const addDecorators = () => {

addDecorator(kibanaContextDecorator);
addDecorator(routerContextDecorator);
addDecorator(servicesContextDecorator);
addDecorator(legacyContextDecorator());
addDecorator(servicesContextDecorator());
};
39 changes: 22 additions & 17 deletions x-pack/plugins/canvas/storybook/decorators/services_decorator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,35 @@ import { DecoratorFn } from '@storybook/react';
import { I18nProvider } from '@kbn/i18n/react';

import { PluginServiceRegistry } from '../../../../../src/plugins/presentation_util/public';
import { pluginServices, LegacyServicesProvider } from '../../public/services';
import { CanvasPluginServices } from '../../public/services';
import { pluginServices, CanvasPluginServices } from '../../public/services';
import { pluginServiceProviders, StorybookParams } from '../../public/services/storybook';
import { LegacyServicesProvider } from '../../public/services/legacy';
import { startServices } from '../../public/services/legacy/stubs';

export const servicesContextDecorator: DecoratorFn = (story: Function, storybook) => {
if (process.env.JEST_WORKER_ID !== undefined) {
storybook.args.useStaticData = true;
}

export const servicesContextDecorator = (): DecoratorFn => {
const pluginServiceRegistry = new PluginServiceRegistry<CanvasPluginServices, StorybookParams>(
pluginServiceProviders
);

pluginServices.setRegistry(pluginServiceRegistry.start(storybook.args));
pluginServices.setRegistry(pluginServiceRegistry.start({}));

const ContextProvider = pluginServices.getContextProvider();
return (story: Function, storybook) => {
if (process.env.JEST_WORKER_ID !== undefined) {
storybook.args.useStaticData = true;
}

return (
<I18nProvider>
<ContextProvider>{story()}</ContextProvider>
</I18nProvider>
);
pluginServices.setRegistry(pluginServiceRegistry.start(storybook.args));
const ContextProvider = pluginServices.getContextProvider();

return (
<I18nProvider>
<ContextProvider>{story()}</ContextProvider>
</I18nProvider>
);
};
};

export const legacyContextDecorator = () => (story: Function) => (
<LegacyServicesProvider>{story()}</LegacyServicesProvider>
);
export const legacyContextDecorator = () => {
startServices();
return (story: Function) => <LegacyServicesProvider>{story()}</LegacyServicesProvider>;
};
3 changes: 0 additions & 3 deletions x-pack/plugins/canvas/storybook/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@

import { addParameters } from '@storybook/react';

import { startServices } from '../public/services/stubs';
import { addDecorators } from './decorators';

// Import Canvas CSS
import '../public/style/index.scss';

startServices();

addDecorators();
addParameters({
controls: { hideNoControlsWarning: true },
Expand Down

0 comments on commit dc81d13

Please sign in to comment.