Skip to content

Commit

Permalink
updates tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dplumlee committed Jan 4, 2023
1 parent 35aa3da commit fd9656a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { mount, shallow } from 'enzyme';

import { ExceptionListTypeEnum } from '@kbn/securitysolution-io-ts-list-types';

import { fetchExceptionListsItemsByListIds } from '@kbn/securitysolution-list-api';

import { ExceptionsViewer } from '.';
import { useKibana } from '../../../../common/lib/kibana';
import { TestProviders } from '../../../../common/mock';
Expand All @@ -20,6 +22,7 @@ import * as i18n from './translations';

jest.mock('../../../../common/lib/kibana');
jest.mock('@kbn/securitysolution-list-hooks');
jest.mock('@kbn/securitysolution-list-api');
jest.mock('../../logic/use_find_references');
jest.mock('react', () => {
const r = jest.requireActual('react');
Expand Down Expand Up @@ -78,6 +81,8 @@ describe('ExceptionsViewer', () => {
},
});

(fetchExceptionListsItemsByListIds as jest.Mock).mockReturnValue({ total: 0 });

(useFindExceptionListReferences as jest.Mock).mockReturnValue([
false,
false,
Expand Down Expand Up @@ -130,6 +135,7 @@ describe('ExceptionsViewer', () => {
exceptionToEdit: null,
viewerState: 'loading',
exceptionLists: [],
exceptionsToShow: { active: true },
},
jest.fn(),
]);
Expand Down Expand Up @@ -168,6 +174,7 @@ describe('ExceptionsViewer', () => {
exceptionToEdit: null,
viewerState: 'empty_search',
exceptionLists: [],
exceptionsToShow: { active: true },
},
jest.fn(),
]);
Expand Down Expand Up @@ -206,6 +213,7 @@ describe('ExceptionsViewer', () => {
exceptionToEdit: null,
viewerState: 'empty',
exceptionLists: [],
exceptionsToShow: { active: true },
},
jest.fn(),
]);
Expand Down Expand Up @@ -250,6 +258,7 @@ describe('ExceptionsViewer', () => {
exceptionToEdit: null,
viewerState: 'empty',
exceptionLists: [],
exceptionsToShow: { active: true },
},
jest.fn(),
]);
Expand Down Expand Up @@ -294,6 +303,7 @@ describe('ExceptionsViewer', () => {
exceptionToEdit: null,
viewerState: null,
exceptionLists: [],
exceptionsToShow: { active: true },
},
jest.fn(),
]);
Expand Down Expand Up @@ -328,6 +338,7 @@ describe('ExceptionsViewer', () => {
exceptionToEdit: sampleExceptionItem,
viewerState: null,
exceptionLists: [],
exceptionsToShow: { active: true },
},
jest.fn(),
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const StyledText = styled(EuiText)`
font-style: italic;
`;

const STATES_SEARCH_HIDDEN: ViewerState[] = ['error', 'empty'];
const STATES_FILTERS_HIDDEN: ViewerState[] = ['error'];
const STATES_PAGINATION_UTILITY_HIDDEN: ViewerState[] = [
'loading',
'empty_search',
Expand Down Expand Up @@ -299,6 +299,24 @@ const ExceptionsViewerComponent = ({
]
);

const totalExceptionCount = useMemo(async () => {
const abortCtrl = new AbortController();

if (exceptionListsToQuery.length === 0) {
return 0;
}

const { total } = await fetchExceptionListsItemsByListIds({
filter: undefined,
http: services.http,
listIds: exceptionListsToQuery.map((list) => list.list_id),
namespaceTypes,
pagination: {},
signal: abortCtrl.signal,
});
return total;
}, [exceptionListsToQuery, namespaceTypes, services.http]);

const handleGetExceptionListItems = useCallback(
async (options?: GetExceptionItemProps) => {
try {
Expand All @@ -314,7 +332,9 @@ const ExceptionsViewerComponent = ({
},
});

setViewerState(total > 0 ? null : 'empty');
setViewerState(
total > 0 ? null : (await totalExceptionCount) > 0 ? 'empty_search' : 'empty'
);
} catch (e) {
setViewerState('error');

Expand All @@ -324,7 +344,7 @@ const ExceptionsViewerComponent = ({
});
}
},
[handleFetchItems, setExceptions, setViewerState, toasts]
[handleFetchItems, setExceptions, setViewerState, toasts, totalExceptionCount]
);

const handleSearch = useCallback(
Expand Down Expand Up @@ -485,27 +505,25 @@ const ExceptionsViewerComponent = ({
{isEndpointSpecified ? i18n.ENDPOINT_EXCEPTIONS_TAB_ABOUT : i18n.EXCEPTIONS_TAB_ABOUT}
</StyledText>
<EuiSpacer size="l" />
{!STATES_SEARCH_HIDDEN.includes(viewerState) && (
<ExceptionsViewerSearchBar
canAddException={isReadOnly}
isEndpoint={isEndpointSpecified}
isSearching={viewerState === 'searching'}
onSearch={handleSearch}
onAddExceptionClick={handleAddException}
/>
)}
{!STATES_PAGINATION_UTILITY_HIDDEN.includes(viewerState) && (
{!STATES_FILTERS_HIDDEN.includes(viewerState) && (
<>
<EuiSpacer size="l" />

<ExceptionsViewerUtility
pagination={pagination}
exceptionsToShow={exceptionsToShow}
onChangeExceptionsToShow={handleExceptionsToShow}
lastUpdated={lastUpdated}
/>
<EuiSpacer size="l" />
<ExceptionsViewerSearchBar
canAddException={isReadOnly}
isEndpoint={isEndpointSpecified}
isSearching={viewerState === 'searching'}
onSearch={handleSearch}
onAddExceptionClick={handleAddException}
/>
</>
)}
<EuiSpacer size="l" />

<ExceptionsViewerItems
isReadOnly={isReadOnly}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('ExceptionsViewerUtility', () => {
totalItemCount: 105,
pageSizeOptions: [5, 10, 20, 50, 100],
}}
exceptionsToShow={{}}
exceptionsToShow={{ active: true }}
onChangeExceptionsToShow={(optionId: string) => {}}
lastUpdated={1660534202}
/>
Expand All @@ -44,7 +44,7 @@ describe('ExceptionsViewerUtility', () => {
totalItemCount: 1,
pageSizeOptions: [5, 10, 20, 50, 100],
}}
exceptionsToShow={{}}
exceptionsToShow={{ active: true }}
onChangeExceptionsToShow={(optionId: string) => {}}
lastUpdated={Date.now()}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const ExceptionsViewerUtilityComponent: React.FC<ExceptionsViewerUtilityProps> =
defaultMessage="Showing {partOne} of {partTwo}"
values={{
partOne: (
<StyledText>{`1-${Math.min(
<StyledText>{`${pagination.totalItemCount === 0 ? '0' : '1'}-${Math.min(
pagination.pageSize,
pagination.totalItemCount
)}`}</StyledText>
Expand Down

0 comments on commit fd9656a

Please sign in to comment.