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

[8.x] [Inventory] Fix Inventory storybook (#197174) #198526

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import type { EntityManagerPublicPluginStart } from '@kbn/entityManager-plugin/p
import type { InferencePublicStart } from '@kbn/inference-plugin/public';
import type { ObservabilitySharedPluginStart } from '@kbn/observability-shared-plugin/public';
import type { UnifiedSearchPublicPluginStart } from '@kbn/unified-search-plugin/public';
import type { SharePluginStart } from '@kbn/share-plugin/public';
import type { LocatorPublic, SharePluginStart } from '@kbn/share-plugin/public';
import type { SpacesPluginStart } from '@kbn/spaces-plugin/public';
import type { HttpStart } from '@kbn/core-http-browser';
import { action } from '@storybook/addon-actions';
import type { InventoryKibanaContext } from '../public/hooks/use_kibana';
import { ITelemetryClient } from '../public/services/telemetry/types';

Expand All @@ -25,7 +27,21 @@ export function getMockInventoryContext(): InventoryKibanaContext {
entityManager: {} as unknown as EntityManagerPublicPluginStart,
observabilityShared: {} as unknown as ObservabilitySharedPluginStart,
inference: {} as unknown as InferencePublicStart,
share: {} as unknown as SharePluginStart,
share: {
url: {
locators: {
get: (_id: string) =>
({
navigate: async () => {
return Promise.resolve();
},
getRedirectUrl: (args: any) => {
action('share.url.locators.getRedirectUrl')(args);
},
} as unknown as LocatorPublic<any>),
},
},
} as unknown as SharePluginStart,
telemetry: {} as unknown as ITelemetryClient,
unifiedSearch: {} as unknown as UnifiedSearchPublicPluginStart,
dataViews: {} as unknown as DataViewsPublicPluginStart,
Expand All @@ -34,6 +50,13 @@ export function getMockInventoryContext(): InventoryKibanaContext {
fetch: jest.fn(),
stream: jest.fn(),
},
http: {
basePath: {
prepend: (_path: string) => {
return '';
},
},
} as unknown as HttpStart,
spaces: {} as unknown as SpacesPluginStart,
kibanaEnvironment: {
isCloudEnv: false,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
* 2.0.
*/

module.exports = require('@kbn/storybook').defaultConfig;
import { defaultConfig } from '@kbn/storybook';

module.exports = defaultConfig;
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
* 2.0.
*/

import { EuiThemeProviderDecorator } from '@kbn/kibana-react-plugin/common';
import { addDecorator } from '@storybook/react';
import * as jest from 'jest-mock';
import { KibanaReactStorybookDecorator } from './storybook_decorator';

// @ts-ignore
window.jest = jest;

export const decorators = [EuiThemeProviderDecorator];
addDecorator(KibanaReactStorybookDecorator);
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import React, { ComponentType, useMemo } from 'react';
import React, { useMemo } from 'react';
import { DecoratorFn } from '@storybook/react';
import { InventoryContextProvider } from '../public/context/inventory_context_provider';
import { getMockInventoryContext } from './get_mock_inventory_context';

export function KibanaReactStorybookDecorator(Story: ComponentType) {
export const KibanaReactStorybookDecorator: DecoratorFn = (story) => {
const context = useMemo(() => getMockInventoryContext(), []);
return (
<InventoryContextProvider context={context}>
<Story />
</InventoryContextProvider>
);
}
return <InventoryContextProvider context={context}>{story()}</InventoryContextProvider>;
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ module.exports = {
'<rootDir>/x-pack/plugins/observability_solution/inventory/common',
'<rootDir>/x-pack/plugins/observability_solution/inventory/server',
],
setupFiles: [
'<rootDir>/x-pack/plugins/observability_solution/inventory/.storybook/jest_setup.js',
],
collectCoverage: true,
collectCoverageFrom: [
'<rootDir>/x-pack/plugins/observability_solution/inventory/{public,common,server}/**/*.{js,ts,tsx}',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,69 @@
* 2.0.
*/

import { EuiDataGridSorting, EuiFlexGroup, EuiFlexItem, EuiLink } from '@elastic/eui';
import { EuiButton, EuiDataGridSorting, EuiFlexGroup, EuiFlexItem } from '@elastic/eui';
import { Meta, Story } from '@storybook/react';
import { orderBy } from 'lodash';
import React, { useMemo, useState } from 'react';
import { ENTITY_LAST_SEEN, ENTITY_TYPE } from '@kbn/observability-shared-plugin/common';
import { useArgs } from '@storybook/addons';
import { EntitiesGrid } from '.';
import { EntityType } from '../../../common/entities';
import { entitiesMock } from './mock/entities_mock';

const stories: Meta<{}> = {
interface EntityGridStoriesArgs {
entityType?: EntityType;
}

const entityTypeOptions: EntityType[] = ['host', 'container', 'service'];

const stories: Meta<EntityGridStoriesArgs> = {
title: 'app/inventory/entities_grid',
component: EntitiesGrid,
argTypes: {
entityType: {
options: entityTypeOptions,
name: 'Entity type',
control: {
type: 'select',
},
},
},
args: { entityType: undefined },
};
export default stories;

export const Example: Story<{}> = () => {
export const Grid: Story<EntityGridStoriesArgs> = (args) => {
const [pageIndex, setPageIndex] = useState(0);
const [{ entityType }, updateArgs] = useArgs();
const [sort, setSort] = useState<EuiDataGridSorting['columns'][0]>({
id: ENTITY_LAST_SEEN,
direction: 'desc',
});
const [selectedEntityType, setSelectedEntityType] = useState<EntityType | undefined>();
const filteredAndSortedItems = useMemo(
() =>
orderBy(
selectedEntityType
? entitiesMock.filter((mock) => mock[ENTITY_TYPE] === selectedEntityType)
: entitiesMock,
entityType ? entitiesMock.filter((mock) => mock[ENTITY_TYPE] === entityType) : entitiesMock,
sort.id,
sort.direction
),
[selectedEntityType, sort.direction, sort.id]
[entityType, sort.direction, sort.id]
);

return (
<EuiFlexGroup direction="column">
<EuiFlexItem grow={false}>
{`Entity filter: ${selectedEntityType || 'N/A'}`}
<EuiLink
disabled={!selectedEntityType}
data-test-subj="inventoryExampleClearFilterButton"
onClick={() => setSelectedEntityType(undefined)}
>
Clear filter
</EuiLink>
<EuiFlexGroup direction="column" alignItems="flexStart">
<EuiFlexItem grow={false}>{`Entity filter: ${entityType || 'N/A'}`}</EuiFlexItem>
<EuiFlexItem grow={false}>
<EuiButton
disabled={!entityType}
data-test-subj="inventoryExampleClearFilterButton"
onClick={() => updateArgs({ entityType: undefined })}
>
Clear filter
</EuiButton>
</EuiFlexItem>
</EuiFlexGroup>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<EntitiesGrid
Expand All @@ -60,14 +78,14 @@ export const Example: Story<{}> = () => {
onChangePage={setPageIndex}
onChangeSort={setSort}
pageIndex={pageIndex}
onFilterByType={setSelectedEntityType}
onFilterByType={(selectedEntityType) => updateArgs({ entityType: selectedEntityType })}
/>
</EuiFlexItem>
</EuiFlexGroup>
);
};

export const EmptyGridExample: Story<{}> = () => {
export const EmptyGrid: Story<EntityGridStoriesArgs> = (args) => {
const [pageIndex, setPageIndex] = useState(0);
const [sort, setSort] = useState<EuiDataGridSorting['columns'][0]>({
id: ENTITY_LAST_SEEN,
Expand All @@ -87,3 +105,5 @@ export const EmptyGridExample: Story<{}> = () => {
/>
);
};

export default stories;
Loading