Skip to content

Commit

Permalink
[Security Solution][Endpoint] Remove refresh button from policy trust…
Browse files Browse the repository at this point in the history
…ed apps flyout (elastic#114974)

* Hide refresh button by prop and refactor unit test

* Add test cases for policies selector when enable/disable license

* Remove unused code when adding mock
  • Loading branch information
dasansol92 authored Oct 15, 2021
1 parent 97848ca commit 8aeaa5a
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,62 +5,120 @@
* 2.0.
*/

import { mount } from 'enzyme';
import React from 'react';
import { act, fireEvent } from '@testing-library/react';
import { AppContextTestRender, createAppRootMockRenderer } from '../../../common/mock/endpoint';
import {
EndpointPrivileges,
useEndpointPrivileges,
} from '../../../common/components/user_privileges/use_endpoint_privileges';
import { EndpointDocGenerator } from '../../../../common/endpoint/generate_data';

import { SearchExceptions } from '.';
import { SearchExceptions, SearchExceptionsProps } from '.';
jest.mock('../../../common/components/user_privileges/use_endpoint_privileges');

let onSearchMock: jest.Mock;

interface EuiFieldSearchPropsFake {
onSearch(value: string): void;
}
const mockUseEndpointPrivileges = useEndpointPrivileges as jest.Mock;

describe('Search exceptions', () => {
let appTestContext: AppContextTestRender;
let renderResult: ReturnType<AppContextTestRender['render']>;
let render: (
props?: Partial<SearchExceptionsProps>
) => ReturnType<AppContextTestRender['render']>;

const loadedUserEndpointPrivilegesState = (
endpointOverrides: Partial<EndpointPrivileges> = {}
): EndpointPrivileges => ({
loading: false,
canAccessFleet: true,
canAccessEndpointManagement: true,
isPlatinumPlus: false,
...endpointOverrides,
});

beforeEach(() => {
onSearchMock = jest.fn();
appTestContext = createAppRootMockRenderer();

render = (overrideProps = {}) => {
const props: SearchExceptionsProps = {
placeholder: 'search test',
onSearch: onSearchMock,
...overrideProps,
};

renderResult = appTestContext.render(<SearchExceptions {...props} />);
return renderResult;
};

mockUseEndpointPrivileges.mockReturnValue(loadedUserEndpointPrivilegesState());
});

const getElement = (defaultValue: string = '') => (
<SearchExceptions
defaultValue={defaultValue}
onSearch={onSearchMock}
placeholder={'placeholder'}
/>
);
afterAll(() => {
mockUseEndpointPrivileges.mockReset();
});

it('should have a default value', () => {
const expectedDefaultValue = 'this is a default value';
const element = mount(getElement(expectedDefaultValue));
const defaultValue = element
.find('[data-test-subj="searchField"]')
.first()
.props().defaultValue;
expect(defaultValue).toBe(expectedDefaultValue);
const element = render({ defaultValue: expectedDefaultValue });

expect(element.getByDisplayValue(expectedDefaultValue)).not.toBeNull();
});

it('should dispatch search action when submit search field', () => {
const expectedDefaultValue = 'this is a default value';
const element = mount(getElement());
const element = render();
expect(onSearchMock).toHaveBeenCalledTimes(0);
const searchFieldProps = element
.find('[data-test-subj="searchField"]')
.first()
.props() as EuiFieldSearchPropsFake;

searchFieldProps.onSearch(expectedDefaultValue);
act(() => {
fireEvent.change(element.getByTestId('searchField'), {
target: { value: expectedDefaultValue },
});
});

expect(onSearchMock).toHaveBeenCalledTimes(1);
expect(onSearchMock).toHaveBeenCalledWith(expectedDefaultValue, '', '');
});

it('should dispatch search action when click on button', () => {
const expectedDefaultValue = 'this is a default value';
const element = mount(getElement(expectedDefaultValue));
const element = render({ defaultValue: expectedDefaultValue });
expect(onSearchMock).toHaveBeenCalledTimes(0);

element.find('[data-test-subj="searchButton"]').first().simulate('click');
act(() => {
fireEvent.click(element.getByTestId('searchButton'));
});

expect(onSearchMock).toHaveBeenCalledTimes(1);
expect(onSearchMock).toHaveBeenCalledWith(expectedDefaultValue, '', '');
});

it('should hide refresh button', () => {
const element = render({ hideRefreshButton: true });

expect(element.queryByTestId('searchButton')).toBeNull();
});

it('should hide policies selector when no license', () => {
const generator = new EndpointDocGenerator('policy-list');
const policy = generator.generatePolicyPackagePolicy();
mockUseEndpointPrivileges.mockReturnValue(
loadedUserEndpointPrivilegesState({ isPlatinumPlus: false })
);
const element = render({ policyList: [policy], hasPolicyFilter: true });

expect(element.queryByTestId('policiesSelectorButton')).toBeNull();
});

it('should display policies selector when right license', () => {
const generator = new EndpointDocGenerator('policy-list');
const policy = generator.generatePolicyPackagePolicy();
mockUseEndpointPrivileges.mockReturnValue(
loadedUserEndpointPrivilegesState({ isPlatinumPlus: true })
);
const element = render({ policyList: [policy], hasPolicyFilter: true });

expect(element.queryByTestId('policiesSelectorButton')).not.toBeNull();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface SearchExceptionsProps {
policyList?: ImmutableArray<PolicyData>;
defaultExcludedPolicies?: string;
defaultIncludedPolicies?: string;
hideRefreshButton?: boolean;
onSearch(query: string, includedPolicies?: string, excludedPolicies?: string): void;
}

Expand All @@ -31,6 +32,7 @@ export const SearchExceptions = memo<SearchExceptionsProps>(
policyList,
defaultIncludedPolicies,
defaultExcludedPolicies,
hideRefreshButton = false,
}) => {
const { isPlatinumPlus } = useEndpointPrivileges();
const [query, setQuery] = useState<string>(defaultValue);
Expand Down Expand Up @@ -101,13 +103,15 @@ export const SearchExceptions = memo<SearchExceptionsProps>(
</EuiFlexItem>
) : null}

<EuiFlexItem grow={false} onClick={handleOnSearch} data-test-subj="searchButton">
<EuiButton iconType="refresh">
{i18n.translate('xpack.securitySolution.management.search.button', {
defaultMessage: 'Refresh',
})}
</EuiButton>
</EuiFlexItem>
{!hideRefreshButton ? (
<EuiFlexItem grow={false} onClick={handleOnSearch} data-test-subj="searchButton">
<EuiButton iconType="refresh">
{i18n.translate('xpack.securitySolution.management.search.button', {
defaultMessage: 'Refresh',
})}
</EuiButton>
</EuiFlexItem>
) : null}
</EuiFlexGroup>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ export const PolicyTrustedAppsFlyout = React.memo(() => {
defaultMessage: 'Search trusted applications',
}
)}
hideRefreshButton
/>
<EuiSpacer size="m" />

Expand Down

0 comments on commit 8aeaa5a

Please sign in to comment.