Skip to content

Commit

Permalink
[8.x] [Inventory] Fix Inventory storybook (#197174) (#198532)
Browse files Browse the repository at this point in the history
# Backport

This will backport the following commits from `main` to `8.x`:
- [[Inventory] Fix Inventory storybook
(#197174)](#197174)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Carlos
Crespo","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-10-22T11:17:31Z","message":"[Inventory]
Fix Inventory storybook (#197174)\n\npart of
[#196142](https://github.com/elastic/kibana/issues/196142)\r\n\r\n##
Summary\r\n\r\nThis PR fixes the Inventory storybook\r\n\r\n<img
width=\"600\"
alt=\"image\"\r\nsrc=\"https://github.com/user-attachments/assets/1e16b100-5836-4b48-a7b3-9aa4ab24f475\">\r\n\r\n\r\n###
How to test\r\n- Run `yarn storybook
inventory`\r\n\r\n---------\r\n\r\nCo-authored-by: kibanamachine
<[email protected]>","sha":"85cf23b8922488139dc46e65b64f400bee660847","branchLabelMapping":{"^v9.0.0$":"main","^v8.17.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","backport:prev-minor","ci:project-deploy-observability","Team:obs-ux-infra_services"],"title":"[Inventory]
Fix Inventory
storybook","number":197174,"url":"https://github.com/elastic/kibana/pull/197174","mergeCommit":{"message":"[Inventory]
Fix Inventory storybook (#197174)\n\npart of
[#196142](https://github.com/elastic/kibana/issues/196142)\r\n\r\n##
Summary\r\n\r\nThis PR fixes the Inventory storybook\r\n\r\n<img
width=\"600\"
alt=\"image\"\r\nsrc=\"https://github.com/user-attachments/assets/1e16b100-5836-4b48-a7b3-9aa4ab24f475\">\r\n\r\n\r\n###
How to test\r\n- Run `yarn storybook
inventory`\r\n\r\n---------\r\n\r\nCo-authored-by: kibanamachine
<[email protected]>","sha":"85cf23b8922488139dc46e65b64f400bee660847"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/197174","number":197174,"mergeCommit":{"message":"[Inventory]
Fix Inventory storybook (#197174)\n\npart of
[#196142](https://github.com/elastic/kibana/issues/196142)\r\n\r\n##
Summary\r\n\r\nThis PR fixes the Inventory storybook\r\n\r\n<img
width=\"600\"
alt=\"image\"\r\nsrc=\"https://github.com/user-attachments/assets/1e16b100-5836-4b48-a7b3-9aa4ab24f475\">\r\n\r\n\r\n###
How to test\r\n- Run `yarn storybook
inventory`\r\n\r\n---------\r\n\r\nCo-authored-by: kibanamachine
<[email protected]>","sha":"85cf23b8922488139dc46e65b64f400bee660847"}},{"url":"https://github.com/elastic/kibana/pull/198526","number":198526,"branch":"8.x","state":"OPEN"}]}]
BACKPORT-->

Co-authored-by: Carlos Crespo <[email protected]>
  • Loading branch information
kibanamachine and crespocarlos authored Oct 31, 2024
1 parent 0a56187 commit a16b026
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 3,045 deletions.
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

0 comments on commit a16b026

Please sign in to comment.