Skip to content

Commit

Permalink
[Security Solution] [Cases] Move create page components and dependenc…
Browse files Browse the repository at this point in the history
…ies to Cases (#94444)
  • Loading branch information
stephmilovic authored Mar 16, 2021
1 parent 769243c commit c497239
Show file tree
Hide file tree
Showing 362 changed files with 17,515 additions and 324 deletions.
1 change: 1 addition & 0 deletions packages/kbn-optimizer/limits.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,4 @@ pageLoadAssetSize:
fileUpload: 25664
banners: 17946
mapsEms: 26072
cases: 102558
1 change: 1 addition & 0 deletions x-pack/plugins/cases/common/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

export * from './cases';
export * from './connectors';
export * from './helpers';
export * from './runtime_types';
export * from './saved_object';
export * from './user';
5 changes: 3 additions & 2 deletions x-pack/plugins/cases/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
* 2.0.
*/

import { DEFAULT_MAX_SIGNALS } from '../../security_solution/common/constants';

// The DEFAULT_MAX_SIGNALS value should match the one in `x-pack/plugins/security_solution/common/constants.ts`
// If either changes, engineer should ensure both values are updated
const DEFAULT_MAX_SIGNALS = 100;
export const APP_ID = 'cases';

/**
Expand Down
9 changes: 9 additions & 0 deletions x-pack/plugins/cases/common/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* 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.
*/

export * from './constants';
export * from './api';
5 changes: 3 additions & 2 deletions x-pack/plugins/cases/kibana.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
"configPath": ["xpack", "cases"],
"id": "cases",
"kibanaVersion": "kibana",
"requiredPlugins": ["actions", "securitySolution"],
"extraPublicDirs": ["common"],
"requiredPlugins": ["actions", "esUiShared", "kibanaReact", "triggersActionsUi"],
"optionalPlugins": [
"spaces",
"security"
],
"server": true,
"ui": false,
"ui": true,
"version": "8.0.0"
}
39 changes: 39 additions & 0 deletions x-pack/plugins/cases/public/common/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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 { has } from 'lodash/fp';

export interface AppError {
name: string;
message: string;
body: {
message: string;
};
}

export interface KibanaError extends AppError {
body: {
message: string;
statusCode: number;
};
}

export interface CasesAppError extends AppError {
body: {
message: string;
status_code: number;
};
}

export const isKibanaError = (error: unknown): error is KibanaError =>
has('message', error) && has('body.message', error) && has('body.statusCode', error);

export const isCasesAppError = (error: unknown): error is CasesAppError =>
has('message', error) && has('body.message', error) && has('body.status_code', error);

export const isAppError = (error: unknown): error is AppError =>
isKibanaError(error) || isCasesAppError(error);
30 changes: 30 additions & 0 deletions x-pack/plugins/cases/public/common/lib/kibana/__mocks__/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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 { notificationServiceMock } from '../../../../../../../../src/core/public/mocks';
import {
createKibanaContextProviderMock,
createStartServicesMock,
createWithKibanaMock,
} from '../kibana_react.mock';

export const KibanaServices = { get: jest.fn(), getKibanaVersion: jest.fn(() => '8.0.0') };
export const useKibana = jest.fn().mockReturnValue({
services: createStartServicesMock(),
});

export const useHttp = jest.fn().mockReturnValue(createStartServicesMock().http);
export const useTimeZone = jest.fn();
export const useDateFormat = jest.fn();
export const useBasePath = jest.fn(() => '/test/base/path');
export const useToasts = jest
.fn()
.mockReturnValue(notificationServiceMock.createStartContract().toasts);
export const useCurrentUser = jest.fn();
export const withKibana = jest.fn(createWithKibanaMock());
export const KibanaContextProvider = jest.fn(createKibanaContextProviderMock());
export const useGetUserSavedObjectPermissions = jest.fn();
9 changes: 9 additions & 0 deletions x-pack/plugins/cases/public/common/lib/kibana/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* 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.
*/

export * from './kibana_react';
export * from './services';
35 changes: 35 additions & 0 deletions x-pack/plugins/cases/public/common/lib/kibana/kibana_react.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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 React from 'react';

import { RecursivePartial } from '@elastic/eui/src/components/common';
import { coreMock } from '../../../../../../../src/core/public/mocks';
import { KibanaContextProvider } from '../../../../../../../src/plugins/kibana_react/public';
import { StartServices } from '../../../types';
import { EuiTheme } from '../../../../../../../src/plugins/kibana_react/common';

export const createStartServicesMock = (): StartServices =>
(coreMock.createStart() as unknown) as StartServices;

export const createWithKibanaMock = () => {
const services = createStartServicesMock();

return (Component: unknown) => (props: unknown) => {
return React.createElement(Component as string, { ...(props as object), kibana: { services } });
};
};

export const createKibanaContextProviderMock = () => {
const services = createStartServicesMock();

return ({ children }: { children: React.ReactNode }) =>
React.createElement(KibanaContextProvider, { services }, children);
};

export const getMockTheme = (partialTheme: RecursivePartial<EuiTheme>): EuiTheme =>
partialTheme as EuiTheme;
16 changes: 16 additions & 0 deletions x-pack/plugins/cases/public/common/lib/kibana/kibana_react.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 {
KibanaContextProvider,
useKibana,
} from '../../../../../../../src/plugins/kibana_react/public';
import { StartServices } from '../../../types';

const useTypedKibana = () => useKibana<StartServices>();

export { KibanaContextProvider, useTypedKibana as useKibana };
42 changes: 42 additions & 0 deletions x-pack/plugins/cases/public/common/lib/kibana/services.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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 { CoreStart } from 'kibana/public';

type GlobalServices = Pick<CoreStart, 'http'>;

export class KibanaServices {
private static kibanaVersion?: string;
private static services?: GlobalServices;

public static init({ http, kibanaVersion }: GlobalServices & { kibanaVersion: string }) {
this.services = { http };
this.kibanaVersion = kibanaVersion;
}

public static get(): GlobalServices {
if (!this.services) {
this.throwUninitializedError();
}

return this.services;
}

public static getKibanaVersion(): string {
if (!this.kibanaVersion) {
this.throwUninitializedError();
}

return this.kibanaVersion;
}

private static throwUninitializedError(): never {
throw new Error(
'Kibana services not initialized - are you trying to import this module from outside of the Cases app?'
);
}
}
8 changes: 8 additions & 0 deletions x-pack/plugins/cases/public/common/mock/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* 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.
*/

export * from './test_providers';
23 changes: 23 additions & 0 deletions x-pack/plugins/cases/public/common/mock/kibana_react.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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 { CoreStart } from 'kibana/public';
import { coreMock } from '../../../../../../src/core/public/mocks';
import React from 'react';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { KibanaContextProvider } from '../../../../../../src/plugins/kibana_react/public/context';

export const createStartServicesMock = (): CoreStart => {
const core = coreMock.createStart();
return (core as unknown) as CoreStart;
};
export const createKibanaContextProviderMock = () => {
const services = coreMock.createStart();

return ({ children }: { children: React.ReactNode }) =>
React.createElement(KibanaContextProvider, { services }, children);
};
58 changes: 58 additions & 0 deletions x-pack/plugins/cases/public/common/mock/test_providers.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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 euiDarkVars from '@elastic/eui/dist/eui_theme_dark.json';
import { I18nProvider } from '@kbn/i18n/react';
import React from 'react';
import { BehaviorSubject } from 'rxjs';
import { ThemeProvider } from 'styled-components';
import { createKibanaContextProviderMock, createStartServicesMock } from './kibana_react.mock';
import { FieldHook } from '../shared_imports';

interface Props {
children: React.ReactNode;
}

export const kibanaObservable = new BehaviorSubject(createStartServicesMock());

window.scrollTo = jest.fn();
const MockKibanaContextProvider = createKibanaContextProviderMock();

/** A utility for wrapping children in the providers required to run most tests */
const TestProvidersComponent: React.FC<Props> = ({ children }) => (
<I18nProvider>
<MockKibanaContextProvider>
<ThemeProvider theme={() => ({ eui: euiDarkVars, darkMode: true })}>{children}</ThemeProvider>
</MockKibanaContextProvider>
</I18nProvider>
);

export const TestProviders = React.memo(TestProvidersComponent);

export const useFormFieldMock = <T,>(options?: Partial<FieldHook<T>>): FieldHook<T> => {
return {
path: 'path',
type: 'type',
value: ('mockedValue' as unknown) as T,
isPristine: false,
isValidating: false,
isValidated: false,
isChangingValue: false,
errors: [],
isValid: true,
getErrorsMessages: jest.fn(),
onChange: jest.fn(),
setValue: jest.fn(),
setErrors: jest.fn(),
clearErrors: jest.fn(),
validate: jest.fn(),
reset: jest.fn(),
__isIncludedInOutput: true,
__serializeValue: jest.fn(),
...options,
};
};
33 changes: 33 additions & 0 deletions x-pack/plugins/cases/public/common/shared_imports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.
*/

export {
getUseField,
getFieldValidityAndErrorMessage,
FieldHook,
FieldValidateResponse,
FIELD_TYPES,
Form,
FormData,
FormDataProvider,
FormHook,
FormSchema,
UseField,
UseMultiFields,
useForm,
useFormContext,
useFormData,
ValidationError,
ValidationFunc,
VALIDATION_TYPES,
} from '../../../../../src/plugins/es_ui_shared/static/forms/hook_form_lib';
export {
Field,
SelectField,
} from '../../../../../src/plugins/es_ui_shared/static/forms/components';
export { fieldValidators } from '../../../../../src/plugins/es_ui_shared/static/forms/helpers';
export { ERROR_CODE } from '../../../../../src/plugins/es_ui_shared/static/forms/helpers/field_validators/types';
12 changes: 12 additions & 0 deletions x-pack/plugins/cases/public/common/test_utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* 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.
*/

/**
* Convenience utility to remove text appended to links by EUI
*/
export const removeExternalLinkText = (str: string) =>
str.replace(/\(opens in a new tab or window\)/g, '');
Loading

0 comments on commit c497239

Please sign in to comment.