[Security Solution][Endpoint] Remove refresh button from policy trusted apps flyout (#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
This commit is contained in:
David Sánchez 2021-10-15 09:25:30 +02:00 committed by GitHub
parent 97848ca62a
commit 8aeaa5a2ff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 99 additions and 36 deletions

View file

@ -5,50 +5,77 @@
* 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', () => {
beforeEach(() => {
onSearchMock = jest.fn();
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,
});
const getElement = (defaultValue: string = '') => (
<SearchExceptions
defaultValue={defaultValue}
onSearch={onSearchMock}
placeholder={'placeholder'}
/>
);
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());
});
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, '', '');
@ -56,11 +83,42 @@ describe('Search exceptions', () => {
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();
});
});

View file

@ -19,6 +19,7 @@ export interface SearchExceptionsProps {
policyList?: ImmutableArray<PolicyData>;
defaultExcludedPolicies?: string;
defaultIncludedPolicies?: string;
hideRefreshButton?: boolean;
onSearch(query: string, includedPolicies?: string, excludedPolicies?: string): void;
}
@ -31,6 +32,7 @@ export const SearchExceptions = memo<SearchExceptionsProps>(
policyList,
defaultIncludedPolicies,
defaultExcludedPolicies,
hideRefreshButton = false,
}) => {
const { isPlatinumPlus } = useEndpointPrivileges();
const [query, setQuery] = useState<string>(defaultValue);
@ -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>
);
}

View file

@ -183,6 +183,7 @@ export const PolicyTrustedAppsFlyout = React.memo(() => {
defaultMessage: 'Search trusted applications',
}
)}
hideRefreshButton
/>
<EuiSpacer size="m" />