Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SecuritySolution] Fix Pagination should reset when a new filter is applied to the entity table #198129

Merged
merged 4 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions x-pack/plugins/entity_manager/server/lib/entity_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,7 @@ export class EntityClient {
});

if (!definition) {
const message = `Unable to find entity definition [${id}]`;
this.options.logger.error(message);
throw new EntityDefinitionNotFound(message);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@elastic/obs-entities Whoever calls this client should determine if the error is expected or if it should be logged. Otherwise, we will duplicate errors or even display error messages when we shouldn't.

throw new EntityDefinitionNotFound(`Unable to find entity definition [${id}]`);
}

this.options.logger.info(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import React from 'react';
import { render, screen, fireEvent } from '@testing-library/react';
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import { EntitiesList } from './entities_list';
import { useGlobalTime } from '../../../common/containers/use_global_time';
import { useQueryToggle } from '../../../common/containers/query_toggle';
Expand All @@ -15,28 +15,31 @@ import { useErrorToast } from '../../../common/hooks/use_error_toast';
import type { ListEntitiesResponse } from '../../../../common/api/entity_analytics/entity_store/entities/list_entities.gen';
import { useGlobalFilterQuery } from '../../../common/hooks/use_global_filter_query';
import { TestProviders } from '../../../common/mock';
import { times } from 'lodash/fp';

jest.mock('../../../common/containers/use_global_time');
jest.mock('../../../common/containers/query_toggle');
jest.mock('./hooks/use_entities_list_query');
jest.mock('../../../common/hooks/use_error_toast');
jest.mock('../../../common/hooks/use_global_filter_query');

const entityName = 'Entity Name';
const secondPageTestId = 'pagination-button-1';
const entityName = 'Entity Name 1';
const responseData: ListEntitiesResponse = {
page: 1,
per_page: 10,
total: 1,
records: [
{
total: 20,
records: times(
(index) => ({
'@timestamp': '2021-08-02T14:00:00.000Z',
user: { name: entityName },
user: { name: `Entity Name ${index}` },
entity: {
name: entityName,
name: `Entity Name ${index}`,
source: 'test-index',
},
},
],
}),
10
),
inspect: undefined,
};

Expand Down Expand Up @@ -81,7 +84,7 @@ describe('EntitiesList', () => {

it('displays the correct number of rows', () => {
render(<EntitiesList />, { wrapper: TestProviders });
expect(screen.getAllByRole('row')).toHaveLength(2);
expect(screen.getAllByRole('row')).toHaveLength(10 + 1);
});

it('calls refetch on time range change', () => {
Expand Down Expand Up @@ -112,6 +115,21 @@ describe('EntitiesList', () => {
);
});

it('should reset the page when sort order changes ', async () => {
render(<EntitiesList />, { wrapper: TestProviders });

const secondPageButton = screen.getByTestId(secondPageTestId);
fireEvent.click(secondPageButton);

const columnHeader = screen.getByText('Name');
fireEvent.click(columnHeader);

await waitFor(() => {
const firstPageButton = screen.getByTestId('pagination-button-0');
expect(firstPageButton).toHaveAttribute('aria-current', 'true');
});
});

it('displays error toast when there is an error', () => {
const error = new Error('Test error');
mockUseEntitiesListQuery.mockReturnValueOnce({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ export const EntitiesList: React.FC = () => {
inspect: data?.inspect ?? null,
});

// Reset the active page when the search criteria changes
useEffect(() => {
setActivePage(0);
}, [sorting, limit, filter]);

const columns = useEntitiesListColumns();

// Force a refetch when "refresh" button is clicked.
Expand All @@ -112,7 +117,7 @@ export const EntitiesList: React.FC = () => {
return (
<PaginatedTable
id={ENTITIES_LIST_TABLE_ID}
activePage={(data?.page ?? 1) - 1}
activePage={activePage}
columns={columns}
headerCount={data?.total ?? 0}
titleSize="s"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ export class EntityStoreDataClient {
deleteData,
});
} catch (e) {
logger.error(`Error deleting entity definition for ${entityType}: ${e.message}`);
logger.warn(`Error deleting entity definition for ${entityType}: ${e.message}`);
}
await deleteEntityIndexComponentTemplate({
unitedDefinition,
Expand Down