Skip to content

Commit

Permalink
[SecuritySolution] Fix Pagination should reset when a new filter is a…
Browse files Browse the repository at this point in the history
…pplied to the entity table (#198129)

## Summary

Fix "Pagination should reset when a new filter is applied to the entity
table"
* Extra changes: I demoted some error logs to warning as part of
elastic/security-team#10951


https://github.com/user-attachments/assets/16357db1-5e77-4c7f-ae94-9371d71c4970


### How to test it?
* Start kibana with data
* Navigate to the security solution / entity analytics dashboard
* Start the entity store
* Navigate between pages and change the filter, sorting, and query.
  • Loading branch information
machadoum authored Oct 30, 2024
1 parent e3c0807 commit 40edfe1
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
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);
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 @@ -377,7 +377,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

0 comments on commit 40edfe1

Please sign in to comment.