Skip to content

Commit

Permalink
Merge branch 'main' into 135886-field-stats-in-discover
Browse files Browse the repository at this point in the history
  • Loading branch information
kibanamachine authored Sep 14, 2022
2 parents 10234a9 + d02cd07 commit 5151fa8
Show file tree
Hide file tree
Showing 53 changed files with 993 additions and 112 deletions.
1 change: 1 addition & 0 deletions x-pack/plugins/cases/kibana.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"embeddable",
"esUiShared",
"lens",
"licensing",
"features",
"kibanaReact",
"kibanaUtils",
Expand Down
16 changes: 14 additions & 2 deletions x-pack/plugins/cases/public/common/lib/kibana/kibana_react.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,29 @@
/* eslint-disable react/display-name */

import React from 'react';
import { BehaviorSubject } from 'rxjs';

import { PublicAppInfo } from '@kbn/core/public';
import { RecursivePartial } from '@elastic/eui/src/components/common';
import { coreMock } from '@kbn/core/public/mocks';
import { KibanaContextProvider } from '@kbn/kibana-react-plugin/public';
import { ILicense } from '@kbn/licensing-plugin/public';
import { StartServices } from '../../../types';
import { EuiTheme } from '@kbn/kibana-react-plugin/common';
import { securityMock } from '@kbn/security-plugin/public/mocks';
import { spacesPluginMock } from '@kbn/spaces-plugin/public/mocks';
import { triggersActionsUiMock } from '@kbn/triggers-actions-ui-plugin/public/mocks';
import { BehaviorSubject } from 'rxjs';
import { licensingMock } from '@kbn/licensing-plugin/public/mocks';
import { registerConnectorsToMockActionRegistry } from '../../mock/register_connectors';
import { connectorsMock } from '../../mock/connectors';

export const createStartServicesMock = (): StartServices => {
interface StartServiceArgs {
license?: ILicense | null;
}

export const createStartServicesMock = ({ license }: StartServiceArgs = {}): StartServices => {
const licensingPluginMock = licensingMock.createStart();

const services = {
...coreMock.createStart(),
storage: { ...coreMock.createStorage(), get: jest.fn(), set: jest.fn(), remove: jest.fn() },
Expand All @@ -33,6 +41,10 @@ export const createStartServicesMock = (): StartServices => {
security: securityMock.createStart(),
triggersActionsUi: triggersActionsUiMock.createStart(),
spaces: spacesPluginMock.createStartContract(),
licensing:
license != null
? { ...licensingPluginMock, license$: new BehaviorSubject(license) }
: licensingPluginMock,
} as unknown as StartServices;

services.application.currentAppId$ = new BehaviorSubject<string>('testAppId');
Expand Down
22 changes: 13 additions & 9 deletions x-pack/plugins/cases/public/common/mock/test_providers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,15 @@ import React from 'react';
import { euiDarkVars } from '@kbn/ui-theme';
import { I18nProvider } from '@kbn/i18n-react';
import { ThemeProvider } from 'styled-components';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';

import { render as reactRender, RenderOptions, RenderResult } from '@testing-library/react';
import { KibanaContextProvider } from '@kbn/kibana-react-plugin/public';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { ILicense } from '@kbn/licensing-plugin/public';
import { SECURITY_SOLUTION_OWNER } from '../../../common/constants';
import { CasesFeatures, CasesPermissions } from '../../../common/ui/types';
import { CasesProvider } from '../../components/cases_context';
import {
createKibanaContextProviderMock,
createStartServicesMock,
} from '../lib/kibana/kibana_react.mock';
import { createStartServicesMock } from '../lib/kibana/kibana_react.mock';
import { FieldHook } from '../shared_imports';
import { StartServices } from '../../types';
import { ReleasePhase } from '../../components/types';
Expand All @@ -37,11 +35,11 @@ interface TestProviderProps {
releasePhase?: ReleasePhase;
externalReferenceAttachmentTypeRegistry?: ExternalReferenceAttachmentTypeRegistry;
persistableStateAttachmentTypeRegistry?: PersistableStateAttachmentTypeRegistry;
license?: ILicense;
}
type UiRender = (ui: React.ReactElement, options?: RenderOptions) => RenderResult;

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

/** A utility for wrapping children in the providers required to run most tests */
const TestProvidersComponent: React.FC<TestProviderProps> = ({
Expand All @@ -52,6 +50,7 @@ const TestProvidersComponent: React.FC<TestProviderProps> = ({
releasePhase = 'ga',
externalReferenceAttachmentTypeRegistry = new ExternalReferenceAttachmentTypeRegistry(),
persistableStateAttachmentTypeRegistry = new PersistableStateAttachmentTypeRegistry(),
license,
}) => {
const queryClient = new QueryClient({
defaultOptions: {
Expand All @@ -66,9 +65,11 @@ const TestProvidersComponent: React.FC<TestProviderProps> = ({
},
});

const services = createStartServicesMock({ license });

return (
<I18nProvider>
<MockKibanaContextProvider>
<KibanaContextProvider services={services}>
<ThemeProvider theme={() => ({ eui: euiDarkVars, darkMode: true })}>
<QueryClientProvider client={queryClient}>
<CasesProvider
Expand All @@ -84,7 +85,7 @@ const TestProvidersComponent: React.FC<TestProviderProps> = ({
</CasesProvider>
</QueryClientProvider>
</ThemeProvider>
</MockKibanaContextProvider>
</KibanaContextProvider>
</I18nProvider>
);
};
Expand All @@ -100,6 +101,7 @@ export interface AppMockRenderer {
queryClient: QueryClient;
AppWrapper: React.FC<{ children: React.ReactElement }>;
}

export const testQueryClient = new QueryClient({
defaultOptions: {
queries: {
Expand All @@ -124,8 +126,10 @@ export const createAppMockRenderer = ({
releasePhase = 'ga',
externalReferenceAttachmentTypeRegistry = new ExternalReferenceAttachmentTypeRegistry(),
persistableStateAttachmentTypeRegistry = new PersistableStateAttachmentTypeRegistry(),
license,
}: Omit<TestProviderProps, 'children'> = {}): AppMockRenderer => {
const services = createStartServicesMock();
const services = createStartServicesMock({ license });

const queryClient = new QueryClient({
defaultOptions: {
queries: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@

import React from 'react';
import { renderHook } from '@testing-library/react-hooks';
import { licensingMock } from '@kbn/licensing-plugin/public/mocks';
import { CasesContextFeatures } from '../../common/ui';
import { useCasesFeatures, UseCasesFeatures } from './use_cases_features';
import { TestProviders } from '../../common/mock';
import { CasesContextFeatures } from '../../containers/types';
import { TestProviders } from './mock/test_providers';
import { LicenseType, LICENSE_TYPE } from '@kbn/licensing-plugin/common/types';

describe('useCasesFeatures', () => {
// isAlertsEnabled, isSyncAlertsEnabled, alerts
Expand Down Expand Up @@ -40,6 +42,8 @@ describe('useCasesFeatures', () => {
isAlertsEnabled,
isSyncAlertsEnabled,
metricsFeatures: [],
caseAssignmentAuthorized: false,
pushToServiceAuthorized: false,
});
}
);
Expand All @@ -55,6 +59,36 @@ describe('useCasesFeatures', () => {
isAlertsEnabled: true,
isSyncAlertsEnabled: true,
metricsFeatures: ['connectors'],
caseAssignmentAuthorized: false,
pushToServiceAuthorized: false,
});
});

const licenseTests: Array<[LicenseType, boolean]> = (Object.keys(LICENSE_TYPE) as LicenseType[])
.filter((type: LicenseType) => isNaN(Number(type)))
.map((type) => [
type,
type === 'platinum' || type === 'enterprise' || type === 'trial' ? true : false,
]);

it.each(licenseTests)(
'allows platinum features on a platinum license',
async (type, expectedResult) => {
const license = licensingMock.createLicense({
license: { type },
});

const { result } = renderHook<{}, UseCasesFeatures>(() => useCasesFeatures(), {
wrapper: ({ children }) => <TestProviders license={license}>{children}</TestProviders>,
});

expect(result.current).toEqual({
isAlertsEnabled: true,
isSyncAlertsEnabled: true,
metricsFeatures: [],
caseAssignmentAuthorized: expectedResult,
pushToServiceAuthorized: expectedResult,
});
}
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,23 @@
*/

import { useMemo } from 'react';
import { SingleCaseMetricsFeature } from '../../containers/types';
import { useCasesContext } from './use_cases_context';
import { SingleCaseMetricsFeature } from '../../common/ui';
import { useCasesContext } from '../components/cases_context/use_cases_context';
import { useLicense } from './use_license';

export interface UseCasesFeatures {
isAlertsEnabled: boolean;
isSyncAlertsEnabled: boolean;
caseAssignmentAuthorized: boolean;
pushToServiceAuthorized: boolean;
metricsFeatures: SingleCaseMetricsFeature[];
}

export const useCasesFeatures = (): UseCasesFeatures => {
const { features } = useCasesContext();
const { isAtLeastPlatinum } = useLicense();
const hasLicenseGreaterThanPlatinum = isAtLeastPlatinum();

const casesFeatures = useMemo(
() => ({
isAlertsEnabled: features.alerts.enabled,
Expand All @@ -30,8 +36,11 @@ export const useCasesFeatures = (): UseCasesFeatures => {
*/
isSyncAlertsEnabled: !features.alerts.enabled ? false : features.alerts.sync,
metricsFeatures: features.metrics,
caseAssignmentAuthorized: hasLicenseGreaterThanPlatinum,
pushToServiceAuthorized: hasLicenseGreaterThanPlatinum,
}),
[features.alerts.enabled, features.alerts.sync, features.metrics]
[features.alerts.enabled, features.alerts.sync, features.metrics, hasLicenseGreaterThanPlatinum]
);

return casesFeatures;
};
Loading

0 comments on commit 5151fa8

Please sign in to comment.