Skip to content

Commit

Permalink
Removes tech debt from export all (#120170)
Browse files Browse the repository at this point in the history
## Summary

See: #110903

This removes the `export *` from:
* lists plugin

This also adds `import type` and `export type` in a few areas and fixes the `LicenseType` by changing it from `server` to using the version from `common` to remove the restricted paths. This extra addition prevents more memory leaks when we run jest.
  • Loading branch information
FrankHassanabad authored Dec 2, 2021
1 parent fcb1b9d commit d874c4c
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 113 deletions.
6 changes: 2 additions & 4 deletions x-pack/plugins/lists/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
* 2.0.
*/

// TODO: https://github.com/elastic/kibana/issues/110903
/* eslint-disable @kbn/eslint/no_export_all */

export * from './shared_exports';
export { getExceptionBuilderComponentLazy } from './exceptions/components/builder/index';
export type { OnChangeProps } from './exceptions/components/builder/index';

import type { PluginInitializerContext } from '../../../../src/core/public';

Expand Down
6 changes: 2 additions & 4 deletions x-pack/plugins/security/common/licensing/index.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@

import { of } from 'rxjs';

// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { LICENSE_TYPE } from '../../../licensing/server';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import type { LicenseType } from '../../../licensing/server';
import type { LicenseType } from '../../../licensing/common/types';
import { LICENSE_TYPE } from '../../../licensing/common/types';
import type { SecurityLicenseFeatures } from './license_features';
import type { SecurityLicense } from './license_service';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { waitFor } from '@testing-library/react';

import { AddExceptionModal } from './';
import { useCurrentUser } from '../../../../common/lib/kibana';
import { ExceptionBuilder } from '../../../../shared_imports';
import { getExceptionBuilderComponentLazy } from '../../../../../../lists/public';
import { useAsync } from '@kbn/securitysolution-hook-utils';
import { getExceptionListSchemaMock } from '../../../../../../lists/common/schemas/response/exception_list_schema.mock';
import { useFetchIndex } from '../../../containers/source';
Expand Down Expand Up @@ -54,47 +54,58 @@ jest.mock('@kbn/securitysolution-hook-utils', () => ({
useAsync: jest.fn(),
}));
jest.mock('../../../../detections/containers/detection_engine/rules/use_rule_async');
jest.mock('../../../../../../lists/public');

const mockGetExceptionBuilderComponentLazy = getExceptionBuilderComponentLazy as jest.Mock<
ReturnType<typeof getExceptionBuilderComponentLazy>
>;
const mockUseAsync = useAsync as jest.Mock<ReturnType<typeof useAsync>>;
const mockUseAddOrUpdateException = useAddOrUpdateException as jest.Mock<
ReturnType<typeof useAddOrUpdateException>
>;
const mockUseFetchOrCreateRuleExceptionList = useFetchOrCreateRuleExceptionList as jest.Mock<
ReturnType<typeof useFetchOrCreateRuleExceptionList>
>;
const mockUseSignalIndex = useSignalIndex as jest.Mock<Partial<ReturnType<typeof useSignalIndex>>>;
const mockUseFetchIndex = useFetchIndex as jest.Mock;
const mockUseCurrentUser = useCurrentUser as jest.Mock<Partial<ReturnType<typeof useCurrentUser>>>;
const mockUseRuleAsync = useRuleAsync as jest.Mock;

describe('When the add exception modal is opened', () => {
const ruleName = 'test rule';
let defaultEndpointItems: jest.SpyInstance<
ReturnType<typeof helpers.defaultEndpointExceptionItems>
>;
let ExceptionBuilderComponent: jest.SpyInstance<
ReturnType<typeof ExceptionBuilder.getExceptionBuilderComponentLazy>
>;
beforeEach(() => {
const emptyComp = <span data-test-subj="alert-exception-builder" />;
mockGetExceptionBuilderComponentLazy.mockReturnValue(
<span data-test-subj="alert-exception-builder" />
);
defaultEndpointItems = jest.spyOn(helpers, 'defaultEndpointExceptionItems');
ExceptionBuilderComponent = jest
.spyOn(ExceptionBuilder, 'getExceptionBuilderComponentLazy')
.mockReturnValue(emptyComp);

(useAsync as jest.Mock).mockImplementation(() => ({
mockUseAsync.mockImplementation(() => ({
start: jest.fn(),
loading: false,
error: {},
result: true,
}));

(useAddOrUpdateException as jest.Mock).mockImplementation(() => [
{ isLoading: false },
jest.fn(),
]);
(useFetchOrCreateRuleExceptionList as jest.Mock).mockImplementation(() => [
mockUseAddOrUpdateException.mockImplementation(() => [{ isLoading: false }, jest.fn()]);
mockUseFetchOrCreateRuleExceptionList.mockImplementation(() => [
false,
getExceptionListSchemaMock(),
]);
(useSignalIndex as jest.Mock).mockImplementation(() => ({
mockUseSignalIndex.mockImplementation(() => ({
loading: false,
signalIndexName: 'mock-siem-signals-index',
}));
(useFetchIndex as jest.Mock).mockImplementation(() => [
mockUseFetchIndex.mockImplementation(() => [
false,
{
indexPatterns: stubIndexPattern,
},
]);
(useCurrentUser as jest.Mock).mockReturnValue({ username: 'test-username' });
(useRuleAsync as jest.Mock).mockImplementation(() => ({
mockUseCurrentUser.mockReturnValue({ username: 'test-username' });
mockUseRuleAsync.mockImplementation(() => ({
rule: getRulesSchemaMock(),
}));
});
Expand All @@ -108,7 +119,7 @@ describe('When the add exception modal is opened', () => {
let wrapper: ReactWrapper;
beforeEach(() => {
// Mocks one of the hooks as loading
(useFetchIndex as jest.Mock).mockImplementation(() => [
mockUseFetchIndex.mockImplementation(() => [
true,
{
indexPatterns: stubIndexPattern,
Expand Down Expand Up @@ -147,7 +158,7 @@ describe('When the add exception modal is opened', () => {
/>
</ThemeProvider>
);
const callProps = ExceptionBuilderComponent.mock.calls[0][0];
const callProps = mockGetExceptionBuilderComponentLazy.mock.calls[0][0];
await waitFor(() => callProps.onChange({ exceptionItems: [] }));
});
it('has the add exception button disabled', () => {
Expand Down Expand Up @@ -192,7 +203,7 @@ describe('When the add exception modal is opened', () => {
/>
</ThemeProvider>
);
const callProps = ExceptionBuilderComponent.mock.calls[0][0];
const callProps = mockGetExceptionBuilderComponentLazy.mock.calls[0][0];
await waitFor(() =>
callProps.onChange({ exceptionItems: [...callProps.exceptionListItems] })
);
Expand Down Expand Up @@ -252,7 +263,7 @@ describe('When the add exception modal is opened', () => {
/>
</ThemeProvider>
);
const callProps = ExceptionBuilderComponent.mock.calls[0][0];
const callProps = mockGetExceptionBuilderComponentLazy.mock.calls[0][0];
await waitFor(() =>
callProps.onChange({ exceptionItems: [getExceptionListItemSchemaMock()] })
);
Expand Down Expand Up @@ -288,7 +299,7 @@ describe('When the add exception modal is opened', () => {
describe('when there is an exception being created on a sequence eql rule type', () => {
let wrapper: ReactWrapper;
beforeEach(async () => {
(useRuleAsync as jest.Mock).mockImplementation(() => ({
mockUseRuleAsync.mockImplementation(() => ({
rule: {
...getRulesEqlSchemaMock(),
query:
Expand All @@ -313,7 +324,7 @@ describe('When the add exception modal is opened', () => {
/>
</ThemeProvider>
);
const callProps = ExceptionBuilderComponent.mock.calls[0][0];
const callProps = mockGetExceptionBuilderComponentLazy.mock.calls[0][0];
await waitFor(() =>
callProps.onChange({ exceptionItems: [getExceptionListItemSchemaMock()] })
);
Expand Down Expand Up @@ -354,7 +365,7 @@ describe('When the add exception modal is opened', () => {
};
beforeEach(async () => {
// Mocks the index patterns to contain the pre-populated endpoint fields so that the exception qualifies as bulk closable
(useFetchIndex as jest.Mock).mockImplementation(() => [
mockUseFetchIndex.mockImplementation(() => [
false,
{
indexPatterns: {
Expand Down Expand Up @@ -387,7 +398,7 @@ describe('When the add exception modal is opened', () => {
/>
</ThemeProvider>
);
callProps = ExceptionBuilderComponent.mock.calls[0][0];
callProps = mockGetExceptionBuilderComponentLazy.mock.calls[0][0];
await waitFor(() =>
callProps.onChange({ exceptionItems: [...callProps.exceptionListItems] })
);
Expand Down Expand Up @@ -456,7 +467,7 @@ describe('When the add exception modal is opened', () => {
/>
</ThemeProvider>
);
const callProps = ExceptionBuilderComponent.mock.calls[0][0];
const callProps = mockGetExceptionBuilderComponentLazy.mock.calls[0][0];
await waitFor(() => callProps.onChange({ exceptionItems: [], errorExists: true }));
expect(
wrapper.find('button[data-test-subj="add-exception-confirm-button"]').getDOMNode()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ import {
isThresholdRule,
} from '../../../../../common/detection_engine/utils';
import { Status } from '../../../../../common/detection_engine/schemas/common/schemas';
import { ExceptionBuilder } from '../../../../../public/shared_imports';

import { getExceptionBuilderComponentLazy } from '../../../../../../lists/public';
import * as i18nCommon from '../../../translations';
import * as i18n from './translations';
import * as sharedI18n from '../translations';
Expand Down Expand Up @@ -475,7 +474,7 @@ export const AddExceptionModal = memo(function AddExceptionModal({
<EuiSpacer size="l" />
</>
)}
{ExceptionBuilder.getExceptionBuilderComponentLazy({
{getExceptionBuilderComponentLazy({
allowLargeValueLists:
!isEqlRule(maybeRule?.type) && !isThresholdRule(maybeRule?.type),
httpService: http,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
} from '../../../../../common/detection_engine/schemas/response/rules_schema.mocks';
import { useRuleAsync } from '../../../../detections/containers/detection_engine/rules/use_rule_async';
import { getMockTheme } from '../../../lib/kibana/kibana_react.mock';
import { ExceptionBuilder } from '../../../../shared_imports';
import { getExceptionBuilderComponentLazy } from '../../../../../../lists/public';

const mockTheme = getMockTheme({
eui: {
Expand All @@ -44,39 +44,31 @@ jest.mock('../../../containers/source');
jest.mock('../use_fetch_or_create_rule_exception_list');
jest.mock('../../../../detections/containers/detection_engine/alerts/use_signal_index');
jest.mock('../../../../detections/containers/detection_engine/rules/use_rule_async');
jest.mock('../../../../shared_imports', () => {
const originalModule = jest.requireActual('../../../../shared_imports');
const emptyComp = <span data-test-subj="edit-exception-modal-builder" />;
return {
...originalModule,
ExceptionBuilder: {
getExceptionBuilderComponentLazy: () => emptyComp,
},
};
});
jest.mock('../../../../../../lists/public');

const mockGetExceptionBuilderComponentLazy = getExceptionBuilderComponentLazy as jest.Mock<
ReturnType<typeof getExceptionBuilderComponentLazy>
>;
const mockUseSignalIndex = useSignalIndex as jest.Mock<Partial<ReturnType<typeof useSignalIndex>>>;
const mockUseAddOrUpdateException = useAddOrUpdateException as jest.Mock<
ReturnType<typeof useAddOrUpdateException>
>;
const mockUseFetchIndex = useFetchIndex as jest.Mock;
const mockUseCurrentUser = useCurrentUser as jest.Mock<Partial<ReturnType<typeof useCurrentUser>>>;
const mockUseRuleAsync = useRuleAsync as jest.Mock;

describe('When the edit exception modal is opened', () => {
const ruleName = 'test rule';

let ExceptionBuilderComponent: jest.SpyInstance<
ReturnType<typeof ExceptionBuilder.getExceptionBuilderComponentLazy>
>;

beforeEach(() => {
const emptyComp = <span data-test-subj="edit-exception-modal-builder" />;
ExceptionBuilderComponent = jest
.spyOn(ExceptionBuilder, 'getExceptionBuilderComponentLazy')
.mockReturnValue(emptyComp);

(useSignalIndex as jest.Mock).mockReturnValue({
mockGetExceptionBuilderComponentLazy.mockReturnValue(emptyComp);
mockUseSignalIndex.mockReturnValue({
loading: false,
signalIndexName: 'test-signal',
});
(useAddOrUpdateException as jest.Mock).mockImplementation(() => [
{ isLoading: false },
jest.fn(),
]);
(useFetchIndex as jest.Mock).mockImplementation(() => [
mockUseAddOrUpdateException.mockImplementation(() => [{ isLoading: false }, jest.fn()]);
mockUseFetchIndex.mockImplementation(() => [
false,
{
indexPatterns: createStubIndexPattern({
Expand All @@ -96,8 +88,8 @@ describe('When the edit exception modal is opened', () => {
}),
},
]);
(useCurrentUser as jest.Mock).mockReturnValue({ username: 'test-username' });
(useRuleAsync as jest.Mock).mockImplementation(() => ({
mockUseCurrentUser.mockReturnValue({ username: 'test-username' });
mockUseRuleAsync.mockImplementation(() => ({
rule: getRulesSchemaMock(),
}));
});
Expand All @@ -109,7 +101,7 @@ describe('When the edit exception modal is opened', () => {

describe('when the modal is loading', () => {
it('renders the loading spinner', async () => {
(useFetchIndex as jest.Mock).mockImplementation(() => [
mockUseFetchIndex.mockImplementation(() => [
true,
{
indexPatterns: stubIndexPattern,
Expand Down Expand Up @@ -157,7 +149,7 @@ describe('When the edit exception modal is opened', () => {
/>
</ThemeProvider>
);
const callProps = ExceptionBuilderComponent.mock.calls[0][0];
const callProps = mockGetExceptionBuilderComponentLazy.mock.calls[0][0];
await waitFor(() => {
callProps.onChange({ exceptionItems: [...callProps.exceptionListItems] });
});
Expand Down Expand Up @@ -202,7 +194,7 @@ describe('When the edit exception modal is opened', () => {
/>
</ThemeProvider>
);
const callProps = ExceptionBuilderComponent.mock.calls[0][0];
const callProps = mockGetExceptionBuilderComponentLazy.mock.calls[0][0];
await waitFor(() => {
callProps.onChange({ exceptionItems: [...callProps.exceptionListItems] });
});
Expand Down Expand Up @@ -255,7 +247,7 @@ describe('When the edit exception modal is opened', () => {
/>
</ThemeProvider>
);
const callProps = ExceptionBuilderComponent.mock.calls[0][0];
const callProps = (getExceptionBuilderComponentLazy as jest.Mock).mock.calls[0][0];
await waitFor(() => {
callProps.onChange({ exceptionItems: [...callProps.exceptionListItems] });
});
Expand Down Expand Up @@ -299,7 +291,7 @@ describe('When the edit exception modal is opened', () => {
/>
</ThemeProvider>
);
const callProps = ExceptionBuilderComponent.mock.calls[0][0];
const callProps = mockGetExceptionBuilderComponentLazy.mock.calls[0][0];
await waitFor(() => {
callProps.onChange({ exceptionItems: [...callProps.exceptionListItems] });
});
Expand Down Expand Up @@ -344,7 +336,7 @@ describe('When the edit exception modal is opened', () => {
/>
</ThemeProvider>
);
const callProps = ExceptionBuilderComponent.mock.calls[0][0];
const callProps = mockGetExceptionBuilderComponentLazy.mock.calls[0][0];
await waitFor(() => {
callProps.onChange({ exceptionItems: [...callProps.exceptionListItems] });
});
Expand Down Expand Up @@ -380,7 +372,7 @@ describe('When the edit exception modal is opened', () => {
/>
</ThemeProvider>
);
const callProps = ExceptionBuilderComponent.mock.calls[0][0];
const callProps = mockGetExceptionBuilderComponentLazy.mock.calls[0][0];
await waitFor(() => callProps.onChange({ exceptionItems: [], errorExists: true }));

expect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import {
import { useFetchIndex } from '../../../containers/source';
import { useSignalIndex } from '../../../../detections/containers/detection_engine/alerts/use_signal_index';
import { useRuleAsync } from '../../../../detections/containers/detection_engine/rules/use_rule_async';
import { ExceptionBuilder } from '../../../../../public/shared_imports';
import { getExceptionBuilderComponentLazy } from '../../../../../../lists/public';

import * as i18n from './translations';
import * as sharedI18n from '../translations';
Expand Down Expand Up @@ -344,7 +344,7 @@ export const EditExceptionModal = memo(function EditExceptionModal({
<EuiSpacer />
</>
)}
{ExceptionBuilder.getExceptionBuilderComponentLazy({
{getExceptionBuilderComponentLazy({
allowLargeValueLists:
!isEqlRule(maybeRule?.type) && !isThresholdRule(maybeRule?.type),
httpService: http,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import { Loader } from '../../../../../../common/components/loader';
import { useKibana } from '../../../../../../common/lib/kibana';
import { useFetchIndex } from '../../../../../../common/containers/source';
import { AppAction } from '../../../../../../common/store/actions';
import { ExceptionBuilder } from '../../../../../../shared_imports';

import { getExceptionBuilderComponentLazy } from '../../../../../../../../lists/public';
import type { OnChangeProps } from '../../../../../../../../lists/public';
import { useEventFiltersSelector } from '../../hooks';
import { getFormEntryStateMutable, getHasNameError, getNewComment } from '../../../store/selector';
import { NAME_LABEL, NAME_ERROR, NAME_PLACEHOLDER, OS_LABEL, RULE_NAME } from './translations';
Expand Down Expand Up @@ -67,7 +67,7 @@ export const EventFiltersForm: React.FC<EventFiltersFormProps> = memo(
);

const handleOnBuilderChange = useCallback(
(arg: ExceptionBuilder.OnChangeProps) => {
(arg: OnChangeProps) => {
dispatch({
type: 'eventFiltersChangeForm',
payload: {
Expand Down Expand Up @@ -121,7 +121,7 @@ export const EventFiltersForm: React.FC<EventFiltersFormProps> = memo(

const exceptionBuilderComponentMemo = useMemo(
() =>
ExceptionBuilder.getExceptionBuilderComponentLazy({
getExceptionBuilderComponentLazy({
allowLargeValueLists: false,
httpService: http,
autocompleteService: data.autocomplete,
Expand Down
Loading

0 comments on commit d874c4c

Please sign in to comment.