Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SIEM] Fix eslint errors #49713

Merged
merged 8 commits into from
Nov 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,6 @@ module.exports = {
'jsx-a11y/click-events-have-key-events': 'off',
},
},
{
files: ['x-pack/legacy/plugins/siem/**/*.{js,ts,tsx}'],
rules: {
'react-hooks/exhaustive-deps': 'off',
'react-hooks/rules-of-hooks': 'off',
},
},
{
files: ['x-pack/legacy/plugins/snapshot_restore/**/*.{js,ts,tsx}'],
rules: {
Expand Down Expand Up @@ -839,6 +832,8 @@ module.exports = {
// might be introduced after the other warns are fixed
// 'react/jsx-sort-props': 'error',
'react/jsx-tag-spacing': 'error',
// might be introduced after the other warns are fixed
'react-hooks/exhaustive-deps': 'off',
'require-atomic-updates': 'error',
'rest-spread-spacing': ['error', 'never'],
'symbol-description': 'error',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ import * as React from 'react';
import { EmbeddedMap } from './embedded_map';
import { SetQuery } from './types';
import { useKibanaCore } from '../../lib/compose/kibana_core';
import { useIndexPatterns } from '../../hooks/use_index_patterns';

jest.mock('../search_bar', () => ({
siemFilterManager: {
addFilters: jest.fn(),
},
}));

const mockUseIndexPatterns = useIndexPatterns as jest.Mock;
jest.mock('../../hooks/use_index_patterns');
mockUseIndexPatterns.mockImplementation(() => [true, []]);

const mockUseKibanaCore = useKibanaCore as jest.Mock;
jest.mock('../../lib/compose/kibana_core');
mockUseKibanaCore.mockImplementation(() => ({
Expand All @@ -30,6 +35,10 @@ mockUseKibanaCore.mockImplementation(() => ({

jest.mock('../../lib/compose/kibana_plugins');

jest.mock('ui/vis/lib/timezone', () => ({
timezoneProvider: () => () => 'America/New_York',
}));

describe('EmbeddedMap', () => {
let setQuery: SetQuery;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ mockUseKibanaCore.mockImplementation(() => ({
uiSettings: mockUiSettings,
}));

jest.mock('ui/vis/lib/timezone', () => ({
timezoneProvider: () => () => 'America/New_York',
}));

describe('Pane', () => {
test('renders correctly against snapshot', () => {
const EmptyComponent = shallow(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

import React from 'react';
import { Redirect } from 'react-router-dom';
import { scrollToTop } from '../scroll_to_top';
import { useScrollToTop } from '../scroll_to_top';

export interface RedirectWrapperProps {
to: string;
}

export const RedirectWrapper = ({ to }: RedirectWrapperProps) => {
scrollToTop();
useScrollToTop();
return <Redirect to={to} />;
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { useState } from 'react';

export const isContainerResizing = () => {
export const useIsContainerResizing = () => {
const [isResizing, setIsResizing] = useState(false);

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { mount } from 'enzyme';
import * as React from 'react';

import { globalNode, HookWrapper } from '../../mock';
import { scrollToTop } from '.';
import { useScrollToTop } from '.';

const spyScroll = jest.fn();
const spyScrollTo = jest.fn();
Expand All @@ -21,7 +21,7 @@ describe('Scroll to top', () => {

test('scroll have been called', () => {
Object.defineProperty(globalNode.window, 'scroll', { value: spyScroll });
mount(<HookWrapper hook={() => scrollToTop()} />);
mount(<HookWrapper hook={() => useScrollToTop()} />);

expect(spyScroll).toHaveBeenCalledWith({
top: 0,
Expand All @@ -32,7 +32,7 @@ describe('Scroll to top', () => {
test('scrollTo have been called', () => {
Object.defineProperty(globalNode.window, 'scroll', { value: null });
Object.defineProperty(globalNode.window, 'scrollTo', { value: spyScrollTo });
mount(<HookWrapper hook={() => scrollToTop()} />);
mount(<HookWrapper hook={() => useScrollToTop()} />);
expect(spyScrollTo).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { useEffect } from 'react';

export const scrollToTop = () => {
export const useScrollToTop = () => {
useEffect(() => {
// trying to use new API - https://developer.mozilla.org/en-US/docs/Web/API/Window/scrollTo
if (window.scroll) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { ColumnHeaders } from '.';

jest.mock('../../../resize_handle/is_resizing', () => ({
...jest.requireActual('../../../resize_handle/is_resizing'),
isContainerResizing: () => ({
useIsContainerResizing: () => ({
isResizing: true,
setIsResizing: jest.fn(),
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
import { DraggableFieldBadge } from '../../../draggables/field_badge';
import { StatefulFieldsBrowser } from '../../../fields_browser';
import { FIELD_BROWSER_HEIGHT, FIELD_BROWSER_WIDTH } from '../../../fields_browser/helpers';
import { isContainerResizing } from '../../../resize_handle/is_resizing';
import { useIsContainerResizing } from '../../../resize_handle/is_resizing';
import {
OnColumnRemoved,
OnColumnResized,
Expand Down Expand Up @@ -72,7 +72,7 @@ export const ColumnHeaders = React.memo<Props>(
timelineId,
toggleColumn,
}) => {
const { isResizing, setIsResizing } = isContainerResizing();
const { isResizing, setIsResizing } = useIsContainerResizing();

return (
<EventsThead data-test-subj="column-headers">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
getHumanReadableLogonType,
getUserDomainField,
getUserNameField,
useTargetUserAndTargetDomain,
getTargetUserAndTargetDomain,
} from './helpers';

import * as i18n from './translations';
Expand Down Expand Up @@ -65,10 +65,10 @@ export const EndgameSecurityEventDetailsLine = React.memo<Props>(
userName,
winlogEventId,
}) => {
const domain = useTargetUserAndTargetDomain(eventAction) ? endgameTargetDomainName : userDomain;
const domain = getTargetUserAndTargetDomain(eventAction) ? endgameTargetDomainName : userDomain;
const eventDetails = getEventDetails(eventAction);
const hostNameSeparator = getHostNameSeparator(eventAction);
const user = useTargetUserAndTargetDomain(eventAction) ? endgameTargetUserName : userName;
const user = getTargetUserAndTargetDomain(eventAction) ? endgameTargetUserName : userName;
const userDomainField = getUserDomainField(eventAction);
const userNameField = getUserNameField(eventAction);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import {
getHostNameSeparator,
getHumanReadableLogonType,
useTargetUserAndTargetDomain,
getTargetUserAndTargetDomain,
getUserDomainField,
getUserNameField,
getEventDetails,
Expand Down Expand Up @@ -102,29 +102,29 @@ describe('helpers', () => {
});
});

describe('#useTargetUserAndTargetDomain', () => {
describe('#getTargetUserAndTargetDomain', () => {
test('it returns false when eventAction is undefined', () => {
expect(useTargetUserAndTargetDomain(undefined)).toEqual(false);
expect(getTargetUserAndTargetDomain(undefined)).toEqual(false);
});

test('it returns false when eventAction is null', () => {
expect(useTargetUserAndTargetDomain(null)).toEqual(false);
expect(getTargetUserAndTargetDomain(null)).toEqual(false);
});

test('it returns false when eventAction is an empty string', () => {
expect(useTargetUserAndTargetDomain('')).toEqual(false);
expect(getTargetUserAndTargetDomain('')).toEqual(false);
});

test('it returns false when eventAction is a random value', () => {
expect(useTargetUserAndTargetDomain('a random value')).toEqual(false);
expect(getTargetUserAndTargetDomain('a random value')).toEqual(false);
});

test('it returns true when eventAction is "explicit_user_logon"', () => {
expect(useTargetUserAndTargetDomain('explicit_user_logon')).toEqual(true);
expect(getTargetUserAndTargetDomain('explicit_user_logon')).toEqual(true);
});

test('it returns true when eventAction is "user_logoff"', () => {
expect(useTargetUserAndTargetDomain('user_logoff')).toEqual(true);
expect(getTargetUserAndTargetDomain('user_logoff')).toEqual(true);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ export const getHumanReadableLogonType = (endgameLogonType: number | null | unde
export const getHostNameSeparator = (eventAction: string | null | undefined): string =>
eventAction === 'explicit_user_logon' ? i18n.TO : '@';

export const useTargetUserAndTargetDomain = (eventAction: string | null | undefined): boolean =>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andrew-goldstein seems only hooks can start with use, are you ok to change the name to get or maybe do you have some other suggestion for the name?

export const getTargetUserAndTargetDomain = (eventAction: string | null | undefined): boolean =>
eventAction === 'explicit_user_logon' || eventAction === 'user_logoff';

export const getUserDomainField = (eventAction: string | null | undefined): string =>
useTargetUserAndTargetDomain(eventAction) ? 'endgame.target_domain_name' : 'user.domain';
getTargetUserAndTargetDomain(eventAction) ? 'endgame.target_domain_name' : 'user.domain';

export const getUserNameField = (eventAction: string | null | undefined): string =>
useTargetUserAndTargetDomain(eventAction) ? 'endgame.target_user_name' : 'user.name';
getTargetUserAndTargetDomain(eventAction) ? 'endgame.target_user_name' : 'user.name';

export const getEventDetails = (eventAction: string | null | undefined): string => {
switch (eventAction) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ mockUseKibanaCore.mockImplementation(() => ({
uiSettings: mockUiSettings,
}));

jest.mock('ui/vis/lib/timezone', () => ({
timezoneProvider: () => () => 'America/New_York',
}));

describe('Properties', () => {
const usersViewing = ['elastic'];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ describe('Toaster', () => {
</ManageGlobalToaster>
);
wrapper.find('[data-test-subj="add-toast"]').simulate('click');
wrapper.update();
expect(wrapper.find('[data-test-subj="add-toaster-id-super-id"]').exists()).toBe(true);
});
test('we can delete a toast in the reducer', () => {
Expand All @@ -61,7 +60,7 @@ describe('Toaster', () => {
return (
<>
<button
data-test-subj="add-toast"
data-test-subj="delete-toast"
type="button"
onClick={() => dispatch({ type: 'deleteToaster', id: mockToast.id })}
/>
Expand All @@ -79,10 +78,9 @@ describe('Toaster', () => {
<DeleteToaster />
</ManageGlobalToaster>
);
wrapper.update();

expect(wrapper.find('[data-test-subj="delete-toaster-id-super-id"]').exists()).toBe(true);
wrapper.find('[data-test-subj="add-toast"]').simulate('click');
wrapper.update();
wrapper.find('[data-test-subj="delete-toast"]').simulate('click');
expect(wrapper.find('[data-test-subj="delete-toaster-id-super-id"]').exists()).toBe(false);
});
});
Expand Down Expand Up @@ -111,7 +109,6 @@ describe('Toaster', () => {
</ManageGlobalToaster>
);
wrapper.find('[data-test-subj="add-toast"]').simulate('click');
wrapper.update();

expect(wrapper.find('.euiGlobalToastList').exists()).toBe(true);
expect(wrapper.find('.euiToastHeader__title').text()).toBe('Test & Test');
Expand Down Expand Up @@ -144,7 +141,6 @@ describe('Toaster', () => {
</ManageGlobalToaster>
);
wrapper.find('[data-test-subj="add-toast"]').simulate('click');
wrapper.update();

expect(wrapper.find('.euiGlobalToastList').exists()).toBe(true);
expect(wrapper.find('.euiToastHeader__title').text()).toBe('Test & Test ERROR');
Expand Down Expand Up @@ -190,12 +186,10 @@ describe('Toaster', () => {
</ManageGlobalToaster>
);
wrapper.find('[data-test-subj="add-toast"]').simulate('click');
wrapper.update();

expect(wrapper.find('button[data-test-subj="toastCloseButton"]').length).toBe(1);
expect(wrapper.find('.euiToastHeader__title').text()).toBe('Test & Test');
wrapper.find('button[data-test-subj="delete-toast"]').simulate('click');
wrapper.update();
expect(wrapper.find('.euiToast').length).toBe(1);
expect(wrapper.find('.euiToastHeader__title').text()).toBe('Test & Test II');
});
Expand Down Expand Up @@ -230,12 +224,8 @@ describe('Toaster', () => {
</ManageGlobalToaster>
);
wrapper.find('[data-test-subj="add-toast"]').simulate('click');
wrapper.update();

wrapper.find('button[data-test-subj="toaster-show-all-error-modal"]').simulate('click');

wrapper.update();

expect(wrapper.find('.euiToast').length).toBe(0);
});

Expand Down Expand Up @@ -270,18 +260,12 @@ describe('Toaster', () => {
</ManageGlobalToaster>
);
wrapper.find('[data-test-subj="add-toast"]').simulate('click');
wrapper.update();

expect(wrapper.find('.euiToastHeader__title').text()).toBe('Test & Test II');
wrapper.find('button[data-test-subj="toaster-show-all-error-modal"]').simulate('click');

wrapper.update();

wrapper.find('button[data-test-subj="toaster-show-all-error-modal"]').simulate('click');
expect(wrapper.find('.euiToast').length).toBe(0);
wrapper.find('button[data-test-subj="modal-all-errors-close"]').simulate('click');

wrapper.update();

wrapper.find('button[data-test-subj="modal-all-errors-close"]').simulate('click');
expect(wrapper.find('.euiToast').length).toBe(1);
expect(wrapper.find('.euiToastHeader__title').text()).toBe('Test & Test');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,22 @@ export const useKibanaUiSetting = (key: string, defaultValue?: GenericValue) =>
const core = useKibanaCore();
const uiSettingsClient = core.uiSettings;
const uiInjectedMetadata = core.injectedMetadata;
const uiSetting$ = useMemo(() => uiSettingsClient.get$(key, defaultValue), [uiSettingsClient]);
const uiSetting = useObservable(uiSetting$);
const setUiSetting = useCallback((value: GenericValue) => uiSettingsClient.set(key, value), [
uiSettingsClient,
]);
const defaultTimezoneProvider = useMemo(() => timezoneProvider(uiSettingsClient)(), [
uiSettingsClient,
]);

if (key === DEFAULT_KBN_VERSION) {
return [uiInjectedMetadata.getKibanaVersion()];
}

if (key === DEFAULT_TIMEZONE_BROWSER) {
return [useMemo(() => timezoneProvider(uiSettingsClient)(), [uiSettingsClient])];
return [defaultTimezoneProvider];
}

const uiSetting$ = useMemo(() => uiSettingsClient.get$(key, defaultValue), [uiSettingsClient]);
const uiSetting = useObservable(uiSetting$);
const setUiSetting = useCallback((value: GenericValue) => uiSettingsClient.set(key, value), [
uiSettingsClient,
]);
return [uiSetting, setUiSetting];
};
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ jest.mock('./use_observable', () => ({

describe('useKibanaUiSetting', () => {
test('getKibanaVersion', () => {
const [kbnVersion] = useKibanaUiSetting(DEFAULT_KBN_VERSION);
expect(kbnVersion).toEqual('8.0.0');
const wrapper = mount(<HookWrapper hook={() => useKibanaUiSetting(DEFAULT_KBN_VERSION)} />);
expect(wrapper.text()).toEqual('["8.0.0"]');
});

test('getTimezone', () => {
Expand Down