Skip to content

Commit

Permalink
[Security Solution] [Sourcerer] Jest beef up (#79907)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephmilovic authored Oct 12, 2020
1 parent 5e79523 commit 87715a2
Show file tree
Hide file tree
Showing 8 changed files with 303 additions and 224 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import React from 'react';
import { mount } from 'enzyme';
import { SourcererScopeName } from '../../store/sourcerer/model';
import { SourcererComponent } from './index';
import { Sourcerer } from './index';
import { DEFAULT_INDEX_PATTERN } from '../../../../common/constants';
import { sourcererActions, sourcererModel } from '../../store/sourcerer';
import {
Expand Down Expand Up @@ -75,7 +75,7 @@ describe('Sourcerer component', () => {
it('Mounts with all options selected', () => {
const wrapper = mount(
<TestProviders store={store}>
<SourcererComponent {...defaultProps} />
<Sourcerer {...defaultProps} />
</TestProviders>
);
wrapper.find(`[data-test-subj="sourcerer-trigger"]`).first().simulate('click');
Expand Down Expand Up @@ -108,7 +108,7 @@ describe('Sourcerer component', () => {
);
const wrapper = mount(
<TestProviders store={store}>
<SourcererComponent {...defaultProps} />
<Sourcerer {...defaultProps} />
</TestProviders>
);
wrapper.find(`[data-test-subj="sourcerer-trigger"]`).first().simulate('click');
Expand All @@ -119,19 +119,21 @@ describe('Sourcerer component', () => {
it('onChange calls updateSourcererScopeIndices', async () => {
const wrapper = mount(
<TestProviders store={store}>
<SourcererComponent {...defaultProps} />
<Sourcerer {...defaultProps} />
</TestProviders>
);
expect(true).toBeTruthy();
wrapper.find(`[data-test-subj="sourcerer-trigger"]`).first().simulate('click');

expect(
wrapper.find(`[data-test-subj="sourcerer-popover"]`).first().prop('isOpen')
).toBeTruthy();
await waitFor(() => {
((wrapper.find(EuiComboBox).props() as unknown) as {
onChange: (a: EuiComboBoxOptionOption[]) => void;
}).onChange([mockOptions[0], mockOptions[1]]);
wrapper.update();
});
wrapper.find(`[data-test-subj="add-index"]`).first().simulate('click');
expect(wrapper.find(`[data-test-subj="sourcerer-popover"]`).first().prop('isOpen')).toBeFalsy();

expect(mockDispatch).toHaveBeenCalledWith(
sourcererActions.setSelectedIndexPatterns({
Expand All @@ -140,4 +142,110 @@ describe('Sourcerer component', () => {
})
);
});
it('resets to config index patterns', async () => {
store = createStore(
{
...state,
sourcerer: {
...state.sourcerer,
kibanaIndexPatterns: [{ id: '1234', title: 'auditbeat-*' }],
configIndexPatterns: ['packetbeat-*'],
},
},
SUB_PLUGINS_REDUCER,
apolloClientObservable,
kibanaObservable,
storage
);
const wrapper = mount(
<TestProviders store={store}>
<Sourcerer {...defaultProps} />
</TestProviders>
);
wrapper.find(`[data-test-subj="sourcerer-trigger"]`).first().simulate('click');
expect(wrapper.find(`[data-test-subj="config-option"]`).first().exists()).toBeFalsy();
wrapper
.find(
`[data-test-subj="indexPattern-switcher"] [title="packetbeat-*"] button.euiBadge__iconButton`
)
.first()
.simulate('click');
expect(wrapper.find(`[data-test-subj="config-option"]`).first().exists()).toBeTruthy();
wrapper.find(`[data-test-subj="sourcerer-reset"]`).first().simulate('click');
expect(wrapper.find(`[data-test-subj="config-option"]`).first().exists()).toBeFalsy();
});
it('returns index pattern options for kibanaIndexPatterns and configIndexPatterns', () => {
store = createStore(
{
...state,
sourcerer: {
...state.sourcerer,
kibanaIndexPatterns: [{ id: '1234', title: 'auditbeat-*' }],
configIndexPatterns: ['packetbeat-*'],
},
},
SUB_PLUGINS_REDUCER,
apolloClientObservable,
kibanaObservable,
storage
);
const wrapper = mount(
<TestProviders store={store}>
<Sourcerer {...defaultProps} />
</TestProviders>
);
wrapper.find(`[data-test-subj="sourcerer-trigger"]`).first().simulate('click');
expect(wrapper.find(`[data-test-subj="config-option"]`).first().exists()).toBeFalsy();
wrapper
.find(
`[data-test-subj="indexPattern-switcher"] [title="auditbeat-*"] button.euiBadge__iconButton`
)
.first()
.simulate('click');
wrapper.update();
expect(wrapper.find(`[data-test-subj="kip-option"]`).first().text()).toEqual(' auditbeat-*');
wrapper
.find(
`[data-test-subj="indexPattern-switcher"] [title="packetbeat-*"] button.euiBadge__iconButton`
)
.first()
.simulate('click');
wrapper.update();
expect(wrapper.find(`[data-test-subj="config-option"]`).first().text()).toEqual('packetbeat-*');
});
it('combines index pattern options for kibanaIndexPatterns and configIndexPatterns', () => {
store = createStore(
{
...state,
sourcerer: {
...state.sourcerer,
kibanaIndexPatterns: [
{ id: '1234', title: 'auditbeat-*' },
{ id: '5678', title: 'packetbeat-*' },
],
configIndexPatterns: ['packetbeat-*'],
},
},
SUB_PLUGINS_REDUCER,
apolloClientObservable,
kibanaObservable,
storage
);
const wrapper = mount(
<TestProviders store={store}>
<Sourcerer {...defaultProps} />
</TestProviders>
);
wrapper.find(`[data-test-subj="sourcerer-trigger"]`).first().simulate('click');
wrapper
.find(
`[data-test-subj="indexPattern-switcher"] [title="packetbeat-*"] button.euiBadge__iconButton`
)
.first()
.simulate('click');
wrapper.update();
expect(
wrapper.find(`[title="packetbeat-*"] [data-test-subj="kip-option"]`).first().text()
).toEqual(' packetbeat-*');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import { useDispatch, useSelector } from 'react-redux';
import styled from 'styled-components';

import * as i18n from './translations';
import { SOURCERER_FEATURE_FLAG_ON } from '../../containers/sourcerer/constants';
import { sourcererActions, sourcererModel } from '../../store/sourcerer';
import { State } from '../../store';
import { getSourcererScopeSelector, SourcererScopeSelector } from './selectors';
Expand All @@ -40,7 +39,7 @@ interface SourcererComponentProps {
scope: sourcererModel.SourcererScopeName;
}

export const SourcererComponent = React.memo<SourcererComponentProps>(({ scope: scopeId }) => {
export const Sourcerer = React.memo<SourcererComponentProps>(({ scope: scopeId }) => {
const dispatch = useDispatch();
const sourcererScopeSelector = useMemo(getSourcererScopeSelector, []);
const { configIndexPatterns, kibanaIndexPatterns, sourcererScope } = useSelector<
Expand Down Expand Up @@ -71,17 +70,14 @@ export const SourcererComponent = React.memo<SourcererComponentProps>(({ scope:
);

const renderOption = useCallback(
(option) => {
const { value } = option;
if (kibanaIndexPatterns.some((kip) => kip.title === value)) {
return (
<>
<EuiIcon type="logoKibana" size="s" /> {value}
</>
);
}
return <>{value}</>;
},
({ value }) =>
kibanaIndexPatterns.some((kip) => kip.title === value) ? (
<span data-test-subj="kip-option">
<EuiIcon type="logoKibana" size="s" /> {value}
</span>
) : (
<span data-test-subj="config-option">{value}</span>
),
[kibanaIndexPatterns]
);

Expand Down Expand Up @@ -175,6 +171,7 @@ export const SourcererComponent = React.memo<SourcererComponentProps>(({ scope:
return (
<EuiToolTip position="top" content={tooltipContent}>
<EuiPopover
data-test-subj="sourcerer-popover"
button={trigger}
isOpen={isPopoverOpen}
closePopover={handleClosePopOver}
Expand Down Expand Up @@ -221,6 +218,4 @@ export const SourcererComponent = React.memo<SourcererComponentProps>(({ scope:
</EuiToolTip>
);
});
SourcererComponent.displayName = 'Sourcerer';

export const Sourcerer = SOURCERER_FEATURE_FLAG_ON ? SourcererComponent : () => null;
Sourcerer.displayName = 'Sourcerer';

This file was deleted.

Loading

0 comments on commit 87715a2

Please sign in to comment.