Skip to content

Commit

Permalink
Added suggested changes and updated tests.
Browse files Browse the repository at this point in the history
Signed-off-by: kishor82 <kishorrathva8298@gmail.com>
  • Loading branch information
kishor82 committed Dec 19, 2023
1 parent f170f1e commit d208248
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { SortOrder } from '../../../saved_searches/types';
import { buildColumns } from '../../utils/columns';
import { useOpenSearchDashboards } from '../../../../../opensearch_dashboards_react/public';
import { DiscoverServices } from '../../../build_services';
import { SAMPLE_SIZE_SETTING } from '../../../../common';

export interface DataGridTableProps {
columns: string[];
Expand Down Expand Up @@ -58,7 +59,8 @@ export const DataGridTable = ({

const [inspectedHit, setInspectedHit] = useState<OpenSearchSearchHit | undefined>();
const rowCount = useMemo(() => (rows ? rows.length : 0), [rows]);
const pagination = usePagination({ rowCount, services });
const pageSizeLimit = services.uiSettings.get(SAMPLE_SIZE_SETTING);
const pagination = usePagination({ rowCount, pageSizeLimit });

let adjustedColumns = buildColumns(columns);
// handle case where the user removes selected filed and leaves only time column
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,25 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { uniq } from 'lodash';

/**
* Generates an array of pagination options based on the provided `sampleSize`.
* The array includes default values (25, 50, 100) and additional options derived from the `sampleSize` setting.
* Generates an array of pagination options based on the provided `stepSize`.
* The array includes default values (25, 50, 100) and additional options derived from the `stepSize` setting.
* Ensures uniqueness and sorts the array in ascending order, representing available page size options for pagination.
* @param {number} sampleSize - The sample size used to determine additional pagination options.
* @param {number} stepSize - The sample size used to determine additional pagination options.
* @returns {number[]} - An array of available page size options.
*/

export const generatePageSizeOptions = (sampleSize: number): number[] => {
if (sampleSize && sampleSize > 0) {
export const generatePageSizeOptions = (stepSize: number): number[] => {
const isInDefaultRange = stepSize < defaultPageOptions[defaultPageOptions.length - 1];

if (stepSize && stepSize > 0 && !isInDefaultRange) {
const maxSize = 500;
const pageSizeFromSetting = [...Array(Math.floor(sampleSize / maxSize)).keys()].map(
const pageSizeFromSetting = [...Array(Math.ceil(stepSize / maxSize)).keys()].map(
(i) => (i + 1) * maxSize
);
return uniq([...defaultPageOptions, ...pageSizeFromSetting]).sort((a, b) => a - b);
return Array.from(new Set([...defaultPageOptions, ...pageSizeFromSetting])).sort(
(a, b) => a - b
);
}
return defaultPageOptions;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,24 @@
import { renderHook, act } from '@testing-library/react-hooks';
import { usePagination } from './use_pagination';

import { uiSettingsServiceMock } from '../../../../../../core/public/mocks';

describe('usePagination', () => {
// TODO: create mock for DiscoverServices
const services: any = {
uiSettings: uiSettingsServiceMock.createSetupContract(),
};

const pageSizeLimit = 500;
it('should initialize correctly with visParams and nRow', () => {
const nRow = 30;
const { result } = renderHook(() => usePagination({ rowCount: nRow, services }));
const { result } = renderHook(() => usePagination({ rowCount: nRow, pageSizeLimit }));

expect(result.current).toEqual({
pageIndex: 0,
pageSize: 100,
onChangeItemsPerPage: expect.any(Function),
onChangePage: expect.any(Function),
pageSizeOptions: [25, 50, 100],
pageSizeOptions: [25, 50, 100, 500],
});
});

it('should update pageSize correctly when calling onChangeItemsPerPage', () => {
const nRow = 30;
const { result } = renderHook(() => usePagination({ rowCount: nRow, services }));
const { result } = renderHook(() => usePagination({ rowCount: nRow, pageSizeLimit }));

act(() => {
result.current?.onChangeItemsPerPage(20);
Expand All @@ -40,13 +34,13 @@ describe('usePagination', () => {
pageSize: 20,
onChangeItemsPerPage: expect.any(Function),
onChangePage: expect.any(Function),
pageSizeOptions: [25, 50, 100],
pageSizeOptions: [25, 50, 100, 500],
});
});

it('should update pageIndex correctly when calling onChangePage', () => {
const nRow = 30;
const { result } = renderHook(() => usePagination({ rowCount: nRow, services }));
const { result } = renderHook(() => usePagination({ rowCount: nRow, pageSizeLimit }));

act(() => {
result.current?.onChangePage(1);
Expand All @@ -57,13 +51,13 @@ describe('usePagination', () => {
pageSize: 100,
onChangeItemsPerPage: expect.any(Function),
onChangePage: expect.any(Function),
pageSizeOptions: [25, 50, 100],
pageSizeOptions: [25, 50, 100, 500],
});
});

it('should correct pageIndex if it exceeds maximum page index after nRow or perPage change', () => {
const nRow = 300;
const { result } = renderHook(() => usePagination({ rowCount: nRow, services }));
const { result } = renderHook(() => usePagination({ rowCount: nRow, pageSizeLimit }));

act(() => {
result.current?.onChangePage(4);
Expand All @@ -74,7 +68,7 @@ describe('usePagination', () => {
pageSize: 100,
onChangeItemsPerPage: expect.any(Function),
onChangePage: expect.any(Function),
pageSizeOptions: [25, 50, 100],
pageSizeOptions: [25, 50, 100, 500],
});
});
});

0 comments on commit d208248

Please sign in to comment.