Skip to content

Commit

Permalink
updated hook tests and clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
yctercero committed Jun 8, 2020
1 parent 854892a commit 5303cc9
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import * as api from '../api';
import { createKibanaCoreStartMock } from '../../common/mocks/kibana_core';
import { getExceptionListSchemaMock } from '../../../common/schemas/response/exception_list_schema.mock';
import { getExceptionListItemSchemaMock } from '../../../common/schemas/response/exception_list_item_schema.mock';
import { ExceptionListSchema } from '../../../common/schemas';
import { ExceptionItemsAndPagination, UseExceptionListProps } from '../types';
import { ExceptionListItemSchema } from '../../../common/schemas';
import { ExceptionList, UseExceptionListProps } from '../types';

import { ReturnExceptionListAndItems, useExceptionList } from './use_exception_list';

Expand All @@ -35,14 +35,23 @@ describe('useExceptionList', () => {
>(() =>
useExceptionList({
http: mockKibanaHttpService,
id: 'myListId',
namespaceType: 'single',
lists: [{ id: 'myListId', namespaceType: 'single' }],
onError: onErrorMock,
})
);
await waitForNextUpdate();

expect(result.current).toEqual([true, null, null, null]);
expect(result.current).toEqual([
true,
[],
[],
{
page: 1,
perPage: 20,
total: 0,
},
null,
]);
});
});

Expand All @@ -54,30 +63,31 @@ describe('useExceptionList', () => {
>(() =>
useExceptionList({
http: mockKibanaHttpService,
id: 'myListId',
namespaceType: 'single',
lists: [{ id: 'myListId', namespaceType: 'single' }],
onError: onErrorMock,
})
);
await waitForNextUpdate();
await waitForNextUpdate();

const expectedListResult: ExceptionListSchema = getExceptionListSchemaMock();
const expectedListResult: ExceptionList[] = [
{ ...getExceptionListSchemaMock(), totalItems: 1 },
];

const expectedListItemsResult: ExceptionItemsAndPagination = {
items: [getExceptionListItemSchemaMock()],
pagination: {
page: 1,
perPage: 20,
total: 1,
},
};
const expectedListItemsResult: ExceptionListItemSchema[] = [
{ ...getExceptionListItemSchemaMock() },
];

expect(result.current).toEqual([
false,
expectedListResult,
expectedListItemsResult,
result.current[3],
{
page: 1,
perPage: 20,
total: 1,
},
result.current[4],
]);
});
});
Expand All @@ -90,22 +100,20 @@ describe('useExceptionList', () => {
UseExceptionListProps,
ReturnExceptionListAndItems
>(
({ filterOptions, http, id, namespaceType, pagination, onError }) =>
useExceptionList({ filterOptions, http, id, namespaceType, onError, pagination }),
({ filterOptions, http, lists, pagination, onError }) =>
useExceptionList({ filterOptions, http, lists, onError, pagination }),
{
initialProps: {
http: mockKibanaHttpService,
id: 'myListId',
namespaceType: 'single',
lists: [{ id: 'myListId', namespaceType: 'single' }],
onError: onErrorMock,
},
}
);
await waitForNextUpdate();
rerender({
http: mockKibanaHttpService,
id: 'newListId',
namespaceType: 'single',
lists: [{ id: 'newListId', namespaceType: 'single' }],
onError: onErrorMock,
});
await waitForNextUpdate();
Expand All @@ -125,18 +133,17 @@ describe('useExceptionList', () => {
>(() =>
useExceptionList({
http: mockKibanaHttpService,
id: 'myListId',
namespaceType: 'single',
lists: [{ id: 'myListId', namespaceType: 'single' }],
onError: onErrorMock,
})
);
await waitForNextUpdate();
await waitForNextUpdate();

expect(typeof result.current[3]).toEqual('function');
expect(typeof result.current[4]).toEqual('function');

if (result.current[3] != null) {
result.current[3]({ listId: 'myListId', listNamespaceType: 'single' });
if (result.current[4] != null) {
result.current[4]();
}

await waitForNextUpdate();
Expand All @@ -157,8 +164,7 @@ describe('useExceptionList', () => {
() =>
useExceptionList({
http: mockKibanaHttpService,
id: 'myListId',
namespaceType: 'single',
lists: [{ id: 'myListId', namespaceType: 'single' }],
onError: onErrorMock,
})
);
Expand All @@ -180,8 +186,7 @@ describe('useExceptionList', () => {
() =>
useExceptionList({
http: mockKibanaHttpService,
id: 'myListId',
namespaceType: 'single',
lists: [{ id: 'myListId', namespaceType: 'single' }],
onError: onErrorMock,
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useEffect, useMemo, useRef, useState } from 'react';

import { fetchExceptionListById, fetchExceptionListItemsByListId } from '../api';
import { ExceptionIdentifiers, ExceptionList, Pagination, UseExceptionListProps } from '../types';
import { ExceptionListItemSchema, ExceptionListSchema } from '../../../common/schemas';
import { ExceptionListItemSchema } from '../../../common/schemas';

type Func = () => void;
export type ReturnExceptionListAndItems = [
Expand Down Expand Up @@ -45,9 +45,7 @@ export const useExceptionList = ({
onError,
dispatchListsInReducer,
}: UseExceptionListProps): ReturnExceptionListAndItems => {
const [exceptionLists, setExceptionLists] = useState<
Array<ExceptionListSchema & { totalItems: number }>
>([]);
const [exceptionLists, setExceptionLists] = useState<ExceptionList[]>([]);
const [exceptionItems, setExceptionListItems] = useState<ExceptionListItemSchema[]>([]);
const [paginationInfo, setPagination] = useState<Pagination>(pagination);
const fetchExceptionList = useRef<Func | null>(null);
Expand Down Expand Up @@ -142,10 +140,6 @@ export const useExceptionList = ({
onError(error);
}
}

if (isSubscribed) {
setLoading(false);
}
};

// TODO: Workaround for now. Once api updated, we can pass in array of lists to fetch
Expand All @@ -155,6 +149,10 @@ export const useExceptionList = ({
fetchData({ id, namespaceType })
)
);

if (isSubscribed) {
setLoading(false);
}
};

fetchLists();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ const ExceptionsViewerPaginationComponent = ({
}, [pagination, onPaginationChange]);

const totalPages = useMemo((): number => {
return Math.ceil(pagination.totalItemCount / pagination.pageSize);
if (pagination.totalItemCount > 0) {
return Math.ceil(pagination.totalItemCount / pagination.pageSize);
} else {
return 1;
}
}, [pagination]);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,8 @@ const ExceptionsViewerComponent = ({
}, [filterOptions.showEndpointList, filterOptions.showDetectionsList, ruleSettingsUrl]);

const showEmpty = useMemo((): boolean => {
return !initLoading && exceptions.length === 0;
}, [initLoading, exceptions.length]);
return !initLoading && !loadingList && exceptions.length === 0;
}, [initLoading, exceptions.length, loadingList]);

return (
<>
Expand Down

0 comments on commit 5303cc9

Please sign in to comment.