Skip to content

Commit

Permalink
Add more tests and fix storybook
Browse files Browse the repository at this point in the history
  • Loading branch information
machadoum committed Jan 2, 2024
1 parent 5d29c5d commit a46a57e
Show file tree
Hide file tree
Showing 13 changed files with 280 additions and 180 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { mockGlobalState } from './global_state';
import { SUB_PLUGINS_REDUCER } from './utils';
import { createSecuritySolutionStorageMock } from './mock_local_storage';
import type { StartServices } from '../../types';
import { ReactQueryClientProvider } from '../containers/query_client/query_client_provider';

export const kibanaObservable = new BehaviorSubject({} as unknown as StartServices);

Expand Down Expand Up @@ -106,13 +107,15 @@ export const StorybookProviders: React.FC = ({ children }) => {
<I18nProvider>
<KibanaReactContext.Provider>
<NavigationProvider core={coreMock}>
<CellActionsProvider getTriggerCompatibleActions={() => Promise.resolve([])}>
<ReduxStoreProvider store={store}>
<ThemeProvider theme={() => ({ eui: euiLightVars, darkMode: false })}>
{children}
</ThemeProvider>
</ReduxStoreProvider>
</CellActionsProvider>
<ReactQueryClientProvider>
<CellActionsProvider getTriggerCompatibleActions={() => Promise.resolve([])}>
<ReduxStoreProvider store={store}>
<ThemeProvider theme={() => ({ eui: euiLightVars, darkMode: false })}>
{children}
</ThemeProvider>
</ReduxStoreProvider>
</CellActionsProvider>
</ReactQueryClientProvider>
</NavigationProvider>
</KibanaReactContext.Provider>
</I18nProvider>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { RISK_INPUTS_TAB_TEST_ID } from '../../../entity_analytics/components/entity_details_flyout';
import { render } from '@testing-library/react';
import React from 'react';
import { HostDetailsPanel } from '.';
import { TestProviders } from '../../../common/mock';

describe('HostDetailsPanel', () => {
it('render risk inputs panel', () => {
const { getByTestId } = render(
<HostDetailsPanel
riskInputs={{
alertIds: ['test-id-1', 'test-id-2'],
}}
/>,
{ wrapper: TestProviders }
);
expect(getByTestId(RISK_INPUTS_TAB_TEST_ID)).toBeInTheDocument();
});

it("doesn't render risk inputs panel when no alerts ids are provided", () => {
const { queryByTestId } = render(
<HostDetailsPanel
riskInputs={{
alertIds: [],
}}
/>,
{ wrapper: TestProviders }
);
expect(queryByTestId(RISK_INPUTS_TAB_TEST_ID)).not.toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@ import { EuiFlyout } from '@elastic/eui';
import type { ExpandableFlyoutContextValue } from '@kbn/expandable-flyout/src/context';
import { ExpandableFlyoutContext } from '@kbn/expandable-flyout/src/context';
import { StorybookProviders } from '../../../common/mock/storybook_providers';
import {
mockManagedUserData,
mockObservedUser,
mockRiskScoreState,
} from '../../../timelines/components/side_panel/new_user_detail/__mocks__';
import { UserPanelContent } from './content';
import { mockRiskScoreState } from '../../../timelines/components/side_panel/new_user_detail/__mocks__';
import { HostPanelContent } from './content';
import { mockObservedHostData } from '../mocks';

const flyoutContextValue = {
openLeftPanel: () => window.alert('openLeftPanel called'),
Expand All @@ -25,7 +22,7 @@ const flyoutContextValue = {

const riskScoreData = { ...mockRiskScoreState, data: [] };

storiesOf('Components/UserPanelContent', module)
storiesOf('Components/HostPanelContent', module)
.addDecorator((storyFn) => (
<StorybookProviders>
<ExpandableFlyoutContext.Provider value={flyoutContextValue}>
Expand All @@ -36,60 +33,18 @@ storiesOf('Components/UserPanelContent', module)
</StorybookProviders>
))
.add('default', () => (
<UserPanelContent
managedUser={mockManagedUserData}
observedUser={mockObservedUser}
riskScoreState={riskScoreData}
contextID={'test-user-details'}
scopeId={'test-scopeId'}
isDraggable={false}
/>
))
.add('integration disabled', () => (
<UserPanelContent
managedUser={{
data: undefined,
isLoading: false,
isIntegrationEnabled: false,
}}
observedUser={mockObservedUser}
riskScoreState={riskScoreData}
contextID={'test-user-details'}
scopeId={'test-scopeId'}
isDraggable={false}
/>
))
.add('no managed data', () => (
<UserPanelContent
managedUser={{
data: undefined,
isLoading: false,
isIntegrationEnabled: true,
}}
observedUser={mockObservedUser}
<HostPanelContent
observedHost={mockObservedHostData}
riskScoreState={riskScoreData}
contextID={'test-user-details'}
scopeId={'test-scopeId'}
isDraggable={false}
/>
))
.add('no observed data', () => (
<UserPanelContent
managedUser={mockManagedUserData}
observedUser={{
details: {
user: {
id: [],
domain: [],
},
host: {
ip: [],
os: {
name: [],
family: [],
},
},
},
<HostPanelContent
observedHost={{
details: {},
isLoading: false,
firstSeen: {
isLoading: false,
Expand All @@ -108,26 +63,9 @@ storiesOf('Components/UserPanelContent', module)
/>
))
.add('loading', () => (
<UserPanelContent
managedUser={{
data: undefined,
isLoading: true,
isIntegrationEnabled: true,
}}
observedUser={{
details: {
user: {
id: [],
domain: [],
},
host: {
ip: [],
os: {
name: [],
family: [],
},
},
},
<HostPanelContent
observedHost={{
details: {},
isLoading: true,
firstSeen: {
isLoading: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { TestProviders } from '../../../../common/mock';
import { render } from '@testing-library/react';
import React from 'react';
import { mockObservedHostData } from '../../mocks';
import { policyFields } from './endpoint_policy_fields';

const TestWrapper = ({ el }: { el: JSX.Element | undefined }) => <>{el}</>;

jest.mock(
'../../../../management/hooks/response_actions/use_get_endpoint_pending_actions_summary',
() => {
const original = jest.requireActual(
'../../../../management/hooks/response_actions/use_get_endpoint_pending_actions_summary'
);
return {
...original,
useGetEndpointPendingActionsSummary: () => ({
pendingActions: [],
isLoading: false,
isError: false,
isTimeout: false,
fetch: jest.fn(),
}),
};
}
);

describe('Endpoint Policy Fields', () => {
it('renders policy name', () => {
const policyName = policyFields[0];

const { container } = render(<TestWrapper el={policyName.render?.(mockObservedHostData)} />);

expect(container).toHaveTextContent('policy-name');
});

it('renders policy status', () => {
const policyStatus = policyFields[1];

const { container } = render(<TestWrapper el={policyStatus.render?.(mockObservedHostData)} />);

expect(container).toHaveTextContent('failure');
});

it('renders agent status', () => {
const agentStatus = policyFields[3];

const { container } = render(<TestWrapper el={agentStatus.render?.(mockObservedHostData)} />, {
wrapper: TestProviders,
});

expect(container).toHaveTextContent('Healthy');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,6 @@ export const LAST_SEEN = i18n.translate(
}
);

export const HOST_RISK_SCORE = i18n.translate(
'xpack.securitySolution.flyout.entityDetails.host.hostRiskScoreTitle',
{
defaultMessage: 'Host risk score',
}
);

export const HOST_RISK_LEVEL = i18n.translate(
'xpack.securitySolution.flyout.entityDetails.host.hostRiskLevel',
{
defaultMessage: 'Host risk level',
}
);

export const IP_ADDRESSES = i18n.translate(
'xpack.securitySolution.flyout.entityDetails.host.ipAddressesTitle',
{
Expand Down
Loading

0 comments on commit a46a57e

Please sign in to comment.