Skip to content

Commit

Permalink
[Cases] Export getRelatedCases API from cases client (#127065)
Browse files Browse the repository at this point in the history
Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
cnasikas and kibanamachine authored Mar 10, 2022
1 parent 74659af commit 60d5a99
Show file tree
Hide file tree
Showing 41 changed files with 310 additions and 200 deletions.
140 changes: 90 additions & 50 deletions x-pack/plugins/cases/README.md

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions x-pack/plugins/cases/public/client/api/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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 { httpServiceMock } from '../../../../../../src/core/public/mocks';
import { createClientAPI } from '.';

describe('createClientAPI', () => {
const http = httpServiceMock.createStartContract({ basePath: '' });
const api = createClientAPI({ http });

beforeEach(() => {
jest.clearAllMocks();
});

describe('getRelatedCases', () => {
const res = [
{
id: 'test-id',
title: 'test',
},
];
http.get.mockResolvedValue(res);

it('should return the correct response', async () => {
expect(await api.getRelatedCases('alert-id')).toEqual(res);
});

it('should have been called with the correct path', async () => {
await api.getRelatedCases('alert-id');
expect(http.get).toHaveBeenCalledWith('/api/cases/alerts/alert-id');
});
});
});
16 changes: 16 additions & 0 deletions x-pack/plugins/cases/public/client/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* 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 { HttpStart } from 'kibana/public';
import { CasesByAlertId, getCasesFromAlertsUrl } from '../../../common/api';

export const createClientAPI = ({ http }: { http: HttpStart }) => {
return {
getRelatedCases: async (alertId: string): Promise<CasesByAlertId> =>
http.get<CasesByAlertId>(getCasesFromAlertsUrl(alertId)),
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import type { ApplicationStart } from 'kibana/public';
import { OBSERVABILITY_OWNER, SECURITY_SOLUTION_OWNER } from '../../common/constants';
import { OBSERVABILITY_OWNER, SECURITY_SOLUTION_OWNER } from '../../../common/constants';

export type CasesOwners = typeof SECURITY_SOLUTION_OWNER | typeof OBSERVABILITY_OWNER;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { ALERT_RULE_NAME, ALERT_RULE_UUID } from '@kbn/rule-data-utils';
import { get } from 'lodash/fp';
import { Ecs } from '../../common';
import { Ecs } from '../../../common';

type Maybe<T> = T | null;
interface Event {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

import React, { lazy, Suspense } from 'react';
import { EuiLoadingSpinner } from '@elastic/eui';
import { AllCasesSelectorModalProps } from '../components/all_cases/selector_modal';
import { CasesProvider, CasesContextProps } from '../components/cases_context';
import { AllCasesSelectorModalProps } from '../../components/all_cases/selector_modal';
import { CasesProvider, CasesContextProps } from '../../components/cases_context';

export type GetAllCasesSelectorModalProps = AllCasesSelectorModalProps & CasesContextProps;

const AllCasesSelectorModalLazy: React.FC<AllCasesSelectorModalProps> = lazy(
() => import('../components/all_cases/selector_modal')
() => import('../../components/all_cases/selector_modal')
);
export const getAllCasesSelectorModalLazy = ({
owner,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

import { EuiLoadingSpinner } from '@elastic/eui';
import React, { lazy, Suspense } from 'react';
import type { CasesProps } from '../components/app';
import { CasesProvider, CasesContextProps } from '../components/cases_context';
import type { CasesProps } from '../../components/app';
import { CasesProvider, CasesContextProps } from '../../components/cases_context';

export type GetCasesProps = CasesProps & CasesContextProps;

const CasesRoutesLazy: React.FC<CasesProps> = lazy(() => import('../components/app/routes'));
const CasesRoutesLazy: React.FC<CasesProps> = lazy(() => import('../../components/app/routes'));

export const getCasesLazy = ({
owner,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

import { EuiLoadingSpinner } from '@elastic/eui';
import React, { lazy, ReactNode, Suspense } from 'react';
import { CasesContextProps } from '../components/cases_context';
import { CasesContextProps } from '../../components/cases_context';

export type GetCasesContextProps = CasesContextProps;

const CasesProviderLazy: React.FC<{ value: GetCasesContextProps }> = lazy(
() => import('../components/cases_context')
() => import('../../components/cases_context')
);

const CasesProviderLazyWrapper = ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

import React, { lazy, Suspense } from 'react';
import { EuiLoadingSpinner } from '@elastic/eui';
import type { CreateCaseFlyoutProps } from '../components/create/flyout';
import { CasesProvider, CasesContextProps } from '../components/cases_context';
import type { CreateCaseFlyoutProps } from '../../components/create/flyout';
import { CasesProvider, CasesContextProps } from '../../components/cases_context';

export type GetCreateCaseFlyoutProps = CreateCaseFlyoutProps & CasesContextProps;

export const CreateCaseFlyoutLazy: React.FC<CreateCaseFlyoutProps> = lazy(
() => import('../components/create/flyout')
() => import('../../components/create/flyout')
);
export const getCreateCaseFlyoutLazy = ({
owner,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

import { EuiLoadingSpinner } from '@elastic/eui';
import React, { lazy, Suspense } from 'react';
import { CasesProvider, CasesContextProps } from '../components/cases_context';
import { RecentCasesProps } from '../components/recent_cases';
import { CasesProvider, CasesContextProps } from '../../components/cases_context';
import { RecentCasesProps } from '../../components/recent_cases';

export type GetRecentCasesProps = RecentCasesProps & CasesContextProps;

const RecentCasesLazy: React.FC<RecentCasesProps> = lazy(
() => import('../components/recent_cases')
() => import('../../components/recent_cases')
);
export const getRecentCasesLazy = ({ owner, userCanCrud, maxCasesToShow }: GetRecentCasesProps) => (
<CasesProvider value={{ owner, userCanCrud }}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ import { StatusContextMenu } from '../case_action_bar/status_context_menu';
import { TruncatedText } from '../truncated_text';
import { getConnectorIcon } from '../utils';
import { PostComment } from '../../containers/use_post_comment';
import type { CasesOwners } from '../../methods/can_use_cases';
import { CaseAttachments } from '../../types';
import type { CasesOwners } from '../../client/helpers/can_use_cases';

export type CasesColumns =
| EuiTableActionsColumnType<Case>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { TestProviders } from '../../../common/mock';
import { AllCasesList } from '../all_cases_list';
import { SECURITY_SOLUTION_OWNER } from '../../../../common/constants';

jest.mock('../../../methods');
jest.mock('../all_cases_list');

const onRowClick = jest.fn();
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/cases/public/components/app/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

import React from 'react';
import { APP_OWNER } from '../../../common/constants';
import { getCasesLazy } from '../../client/ui/get_cases';
import { useApplicationCapabilities } from '../../common/lib/kibana';

import { getCasesLazy } from '../../methods';
import { Wrapper } from '../wrappers';
import { CasesRoutesProps } from './types';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
*/

import React from 'react';
import { getAllCasesSelectorModalNoProviderLazy } from '../../client/ui/get_all_cases_selector_modal';
import { getCreateCaseFlyoutLazyNoProvider } from '../../client/ui/get_create_case_flyout';
import { AppMockRenderer, createAppMockRenderer } from '../../common/mock';
import {
getAllCasesSelectorModalNoProviderLazy,
getCreateCaseFlyoutLazyNoProvider,
} from '../../methods';
import { getInitialCasesContextState } from './cases_context_reducer';
import { CasesGlobalComponents } from './cases_global_components';

jest.mock('../../methods');
jest.mock('../../client/ui/get_create_case_flyout');
jest.mock('../../client/ui/get_all_cases_selector_modal');

const getCreateCaseFlyoutLazyNoProviderMock = getCreateCaseFlyoutLazyNoProvider as jest.Mock;
const getAllCasesSelectorModalNoProviderLazyMock =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
*/

import React from 'react';
import {
getAllCasesSelectorModalNoProviderLazy,
getCreateCaseFlyoutLazyNoProvider,
} from '../../methods';
import { getAllCasesSelectorModalNoProviderLazy } from '../../client/ui/get_all_cases_selector_modal';
import { getCreateCaseFlyoutLazyNoProvider } from '../../client/ui/get_create_case_flyout';
import { CasesContextState } from './cases_context_reducer';

export const CasesGlobalComponents = React.memo(({ state }: { state: CasesContextState }) => {
Expand Down
8 changes: 4 additions & 4 deletions x-pack/plugins/cases/public/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ export { DRAFT_COMMENT_STORAGE_ID } from './components/markdown_editor/plugins/l

export type { CasesUiPlugin };
export type { CasesUiStart } from './types';
export type { GetCasesProps } from './methods/get_cases';
export type { GetCreateCaseFlyoutProps } from './methods/get_create_case_flyout';
export type { GetAllCasesSelectorModalProps } from './methods/get_all_cases_selector_modal';
export type { GetRecentCasesProps } from './methods/get_recent_cases';
export type { GetCasesProps } from './client/ui/get_cases';
export type { GetCreateCaseFlyoutProps } from './client/ui/get_create_case_flyout';
export type { GetAllCasesSelectorModalProps } from './client/ui/get_all_cases_selector_modal';
export type { GetRecentCasesProps } from './client/ui/get_recent_cases';

export type { CaseAttachments, SupportedCaseAttachment } from './types';

Expand Down
12 changes: 0 additions & 12 deletions x-pack/plugins/cases/public/methods/index.ts

This file was deleted.

38 changes: 29 additions & 9 deletions x-pack/plugins/cases/public/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,40 @@
import { mockCasesContext } from './mocks/mock_cases_context';
import { CasesUiStart } from './types';

export const mockCasesContract = (): jest.Mocked<CasesUiStart> => ({
canUseCases: jest.fn(),
const apiMock: jest.Mocked<CasesUiStart['api']> = {
getRelatedCases: jest.fn(),
};

const uiMock: jest.Mocked<CasesUiStart['ui']> = {
getCases: jest.fn(),
getCasesContext: jest.fn().mockImplementation(() => mockCasesContext),
getAllCasesSelectorModal: jest.fn(),
getCreateCaseFlyout: jest.fn(),
getRecentCases: jest.fn(),
hooks: {
getUseCasesAddToNewCaseFlyout: jest.fn(),
getUseCasesAddToExistingCaseModal: jest.fn(),
},
helpers: {
getRuleIdFromEvent: jest.fn(),
},
};

const hooksMock: jest.Mocked<CasesUiStart['hooks']> = {
getUseCasesAddToNewCaseFlyout: jest.fn(),
getUseCasesAddToExistingCaseModal: jest.fn(),
};

const helpersMock: jest.Mocked<CasesUiStart['helpers']> = {
canUseCases: jest.fn(),
getRuleIdFromEvent: jest.fn(),
};

export interface CaseUiClientMock {
api: jest.Mocked<CasesUiStart['api']>;
ui: jest.Mocked<CasesUiStart['ui']>;
hooks: jest.Mocked<CasesUiStart['hooks']>;
helpers: jest.Mocked<CasesUiStart['helpers']>;
}

export const mockCasesContract = (): CaseUiClientMock => ({
api: apiMock,
ui: uiMock,
hooks: hooksMock,
helpers: helpersMock,
});

export const casesPluginMock = {
Expand Down
36 changes: 19 additions & 17 deletions x-pack/plugins/cases/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,22 @@
import { CoreSetup, CoreStart, Plugin, PluginInitializerContext } from 'src/core/public';
import { CasesUiStart, CasesPluginSetup, CasesPluginStart } from './types';
import { KibanaServices } from './common/lib/kibana';
import {
getCasesLazy,
getRecentCasesLazy,
getAllCasesSelectorModalLazy,
getCreateCaseFlyoutLazy,
canUseCases,
} from './methods';
import { CasesUiConfigType } from '../common/ui/types';
import { APP_ID, APP_PATH } from '../common/constants';
import { APP_TITLE, APP_DESC } from './common/translations';
import { FeatureCatalogueCategory } from '../../../../src/plugins/home/public';
import { ManagementAppMountParams } from '../../../../src/plugins/management/public';
import { Storage } from '../../../../src/plugins/kibana_utils/public';
import { getCasesContextLazy } from './methods/get_cases_context';
import { useCasesAddToExistingCaseModal } from './components/all_cases/selector_modal/use_cases_add_to_existing_case_modal';
import { useCasesAddToNewCaseFlyout } from './components/create/flyout/use_cases_add_to_new_case_flyout';
import { getRuleIdFromEvent } from './methods/get_rule_id_from_event';
import { createClientAPI } from './client/api';
import { canUseCases } from './client/helpers/can_use_cases';
import { getRuleIdFromEvent } from './client/helpers/get_rule_id_from_event';
import { getAllCasesSelectorModalLazy } from './client/ui/get_all_cases_selector_modal';
import { getCasesLazy } from './client/ui/get_cases';
import { getCasesContextLazy } from './client/ui/get_cases_context';
import { getCreateCaseFlyoutLazy } from './client/ui/get_create_case_flyout';
import { getRecentCasesLazy } from './client/ui/get_recent_cases';

/**
* @public
Expand Down Expand Up @@ -87,19 +86,22 @@ export class CasesUiPlugin
const config = this.initializerContext.config.get<CasesUiConfigType>();
KibanaServices.init({ ...core, ...plugins, kibanaVersion: this.kibanaVersion, config });
return {
canUseCases: canUseCases(core.application.capabilities),
getCases: getCasesLazy,
getCasesContext: getCasesContextLazy,
getRecentCases: getRecentCasesLazy,
// @deprecated Please use the hook getUseCasesAddToNewCaseFlyout
getCreateCaseFlyout: getCreateCaseFlyoutLazy,
// @deprecated Please use the hook getUseCasesAddToExistingCaseModal
getAllCasesSelectorModal: getAllCasesSelectorModalLazy,
api: createClientAPI({ http: core.http }),
ui: {
getCases: getCasesLazy,
getCasesContext: getCasesContextLazy,
getRecentCases: getRecentCasesLazy,
// @deprecated Please use the hook getUseCasesAddToNewCaseFlyout
getCreateCaseFlyout: getCreateCaseFlyoutLazy,
// @deprecated Please use the hook getUseCasesAddToExistingCaseModal
getAllCasesSelectorModal: getAllCasesSelectorModalLazy,
},
hooks: {
getUseCasesAddToNewCaseFlyout: useCasesAddToNewCaseFlyout,
getUseCasesAddToExistingCaseModal: useCasesAddToExistingCaseModal,
},
helpers: {
canUseCases: canUseCases(core.application.capabilities),
getRuleIdFromEvent,
},
};
Expand Down
Loading

0 comments on commit 60d5a99

Please sign in to comment.