Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import {
useRef,
useState,
} from 'react';
import { INITIAL_PAGING_VALUE } from '../../../constants/constants';
import {
INITIAL_PAGING_VALUE,
PAGING_URL_PARAMS,
} from '../../../constants/constants';
import { DEFAULT_SORT_ORDER } from '../../../constants/profiler.constant';
import { OperationPermission } from '../../../context/PermissionProvider/PermissionProvider.interface';
import { DataQualityPageTabs } from '../../../enums/DataQuality.enum';
Expand Down Expand Up @@ -59,16 +62,6 @@ export interface UseTestCaseListProps {
showPagination: boolean;
}

// usePaging keeps these in the query string that `params` is parsed from. A paging action sets
// the page in state first and in the URL in a later (transition) render, so the fetch reads the
// page from `currentPage` and `pageSize` and must not refetch again when the URL catches up.
const PAGING_URL_PARAMS = [
'currentPage',
'pageSize',
'cursorType',
'cursorValue',
];

/**
* Owns the DATA concern: the test-case rows, their loading flag, the sort
* options and the fetch pipeline. The driving fetch effect stays co-located
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -945,27 +945,31 @@ describe('IncidentManagerPage', () => {
mockHandlePageSizeChange.mockClear();
});

it('should fetch next page when Next is clicked', async () => {
const { mockHandlePageChange } = require('../../hooks/paging/usePaging');
// usePaging is mocked, so a click cannot move `currentPage`; the click must
// only hand the page to usePaging, whose change drives the single fetch
// (covered with the real usePaging in useIncidentManagerListPage.integration.test).
const clickAndCountListRequests = async (testId: string) => {
const listMock = getListTestCaseIncidentStatusFromSearch as jest.Mock;
const button = await screen.findByTestId(testId);
const callsBefore = listMock.mock.calls.length;
await act(async () => {
render(<IncidentManager />);
fireEvent.click(button);
});

const nextButton = await screen.findByTestId('pagination-next');
return listMock.mock.calls.length - callsBefore;
};

it('should move to the next page when Next is clicked', async () => {
const { mockHandlePageChange } = require('../../hooks/paging/usePaging');
await act(async () => {
fireEvent.click(nextButton);
render(<IncidentManager />);
});

expect(getListTestCaseIncidentStatusFromSearch).toHaveBeenLastCalledWith(
expect.objectContaining({
limit: 10,
offset: 10,
})
);
expect(await clickAndCountListRequests('pagination-next')).toBe(0);
expect(mockHandlePageChange).toHaveBeenCalledWith(2);
});

it('should fetch previous page when Previous is clicked', async () => {
it('should move to the previous page when Previous is clicked', async () => {
const usePagingModule = require('../../hooks/paging/usePaging');
const { usePaging, mockHandlePageChange } = usePagingModule;
usePaging.mockReturnValue({
Expand All @@ -982,50 +986,23 @@ describe('IncidentManagerPage', () => {
render(<IncidentManager />);
});

const previousButton = await screen.findByTestId('pagination-previous');
await act(async () => {
fireEvent.click(previousButton);
});

expect(getListTestCaseIncidentStatusFromSearch).toHaveBeenLastCalledWith(
expect.objectContaining({
limit: 10,
offset: 0,
})
expect.objectContaining({ limit: 10, offset: 10 })
);
expect(await clickAndCountListRequests('pagination-previous')).toBe(0);
expect(mockHandlePageChange).toHaveBeenCalledWith(1);
});

it('should fetch correct page when page number is clicked', async () => {
it('should move to the clicked page number', async () => {
const { mockHandlePageChange } = require('../../hooks/paging/usePaging');
await act(async () => {
render(<IncidentManager />);
});

const page2Button = await screen.findByTestId('pagination-page-2');
await act(async () => {
fireEvent.click(page2Button);
});

expect(getListTestCaseIncidentStatusFromSearch).toHaveBeenLastCalledWith(
expect.objectContaining({
limit: 10,
offset: 10,
})
);
expect(await clickAndCountListRequests('pagination-page-2')).toBe(0);
expect(mockHandlePageChange).toHaveBeenCalledWith(2);

const page3Button = await screen.findByTestId('pagination-page-3');
await act(async () => {
fireEvent.click(page3Button);
});

expect(getListTestCaseIncidentStatusFromSearch).toHaveBeenLastCalledWith(
expect.objectContaining({
limit: 10,
offset: 20,
})
);
expect(await clickAndCountListRequests('pagination-page-3')).toBe(0);
expect(mockHandlePageChange).toHaveBeenCalledWith(3);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { renderHook } from '@testing-library/react';
import { isString, omit, parseInt } from 'lodash';
import QueryString, { ParsedQs } from 'qs';
import { act } from 'react';
import { PAGING_URL_PARAMS } from '../../constants/constants';
import { TestCaseIncidentStatusParams } from '../../rest/incidentManagerAPI';
import {
useIncidentFilters,
Expand All @@ -37,14 +38,18 @@ const renderFiltersHook = (overrides: Partial<UseIncidentFiltersProps> = {}) =>
);

// Models the production wiring in `useIncidentManagerListPage`: `filters` is
// derived from `omit(allParams, ['key', 'title'])`, so it carries `currentPage`
// /`pageSize`/cursor params verbatim (with `startTs`/`endTs` coerced to numbers).
// Tests that inject `filters` and `allParams` independently mask the stale-page
// bug; this helper keeps them in sync to exercise the real relationship.
// `allParams` without `key`/`title` and usePaging's params (with
// `startTs`/`endTs` coerced to numbers), while `allParams` keeps the paging
// params. Tests that inject the two independently mask the stale-page bug;
// this helper keeps them in sync to exercise the real relationship.
const deriveProductionFilters = (
allParams: ParsedQs
): TestCaseIncidentStatusParams => {
const base = omit(allParams, ['key', 'title']) as Record<string, unknown>;
const base = omit(allParams, [
'key',
'title',
...PAGING_URL_PARAMS,
]) as Record<string, unknown>;

if (isString(base.startTs)) {
base.startTs = parseInt(base.startTs, 10);
Expand Down Expand Up @@ -142,6 +147,25 @@ describe('useIncidentFilters', () => {
expect(firstCallArg.search).not.toContain('existing=y');
});

it('should keep the page size and reset to page 1 when the date range changes', () => {
const { result } = renderFiltersHook({
allParams: { currentPage: '3', pageSize: '25' },
});

act(() => {
result.current.updateFilters(
{ startTs: 1, endTs: 2 },
{ key: 'k', title: 't' }
);
});

const [firstCallArg] = mockNavigate.mock.calls[0];

expect(firstCallArg.search).toContain('pageSize=25');
expect(firstCallArg.search).toContain('currentPage=1');
expect(firstCallArg.search).not.toContain('currentPage=3');
});

it('should navigate on a changed date range via handleDateRangeChange', () => {
const { result } = renderFiltersHook();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
import { useCallback, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useNavigate } from 'react-router-dom';
import { INITIAL_PAGING_VALUE } from '../../constants/constants';
import {
INITIAL_PAGING_VALUE,
PAGING_URL_PARAMS,
} from '../../constants/constants';
import { Option } from '../../pages/TasksPage/TasksPage.interface';

Check warning on line 23 in openmetadata-ui/src/main/resources/ui/src/components/IncidentManager/useIncidentFilters.ts

View workflow job for this annotation

GitHub Actions / ui / lint

Pages are route-level composition modules. Move the shared implementation/type to a lower layer instead of importing a page from here
import { TestCaseIncidentStatusParams } from '../../rest/incidentManagerAPI';

// Static, single-select options for the incident resolution status filter.
Expand Down Expand Up @@ -74,10 +77,9 @@
dateRangeParams?: { key: string; title: string }
) => {
const updatedFilters = { ...filters, ...newFilters };
// `filters` mirrors the whole query string, so it also carries usePaging's
// `currentPage`/cursor params. A changed value yields a different result
// set, and keeping the old page would request an offset past its end. An
// unchanged value (e.g. re-selecting the same option) keeps the page.
// A changed value yields a different result set, and keeping the old page
// would request an offset past its end. An unchanged value (e.g.
// re-selecting the same option) keeps the page.
const filterChanged = Object.keys(newFilters).some(
(key) =>
!isEqual(
Expand All @@ -92,8 +94,15 @@
cursorValue: undefined,
}
: {};
// `filters` leaves out usePaging's params, so a date-range update, which
// rebuilds the query from `filters`, carries them over from the URL.
const allUpdatedParams = dateRangeParams
? { ...updatedFilters, ...dateRangeParams, ...pagingReset }
? {
...pick(allParams, PAGING_URL_PARAMS),
...updatedFilters,
...dateRangeParams,
...pagingReset,
}
: { ...allParams, ...updatedFilters, ...pagingReset };

navigate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { act, renderHook, waitFor } from '@testing-library/react';
import { OperationPermission } from '../../context/PermissionProvider/PermissionProvider.interface';
import { Table } from '../../generated/entity/data/table';
import { Include } from '../../generated/type/include';
import { usePaging } from '../../hooks/paging/usePaging';
import { useDomainStore } from '../../hooks/useDomainStore';
import { getListTestCaseIncidentStatusFromSearch } from '../../rest/incidentManagerAPI';
import { showErrorToast } from '../../utils/ToastUtils';
Expand Down Expand Up @@ -158,24 +159,42 @@ describe('useIncidentList', () => {
expect(mockGetList).not.toHaveBeenCalled();
});

it('should refetch with the paged offset and advance the page on handlePagingClick', async () => {
const props = makeProps({
commonTestCasePermission: undefined,
filters: { testCaseFQN: 'x' },
});
it('should move to another page through handlePageChange without fetching it directly', async () => {
const props = makeProps({ filters: { testCaseFQN: 'x' } });
const { result } = renderHook(() => useIncidentList(props));

await waitFor(() =>
expect(result.current.testCaseListData.isLoading).toBe(false)
);
mockGetList.mockClear();

act(() => {
result.current.handlePagingClick({ currentPage: 3 });
});

await waitFor(() => expect(mockGetList).toHaveBeenCalled());

const callArg = mockGetList.mock.calls[0][0];

expect(callArg.offset).toBe(20);
expect(callArg.testCaseFQN).toBe('x');
expect(mockHandlePageChange).toHaveBeenCalledWith(3);
expect(mockGetList).not.toHaveBeenCalled();
});

it('should fetch the offset of the current page', async () => {
const pagingMock = usePaging as jest.Mock;
const defaultPaging = pagingMock.getMockImplementation();
pagingMock.mockImplementation(() => ({
...defaultPaging?.(),
currentPage: 3,
}));
try {
const props = makeProps({ filters: { testCaseFQN: 'x' } });
renderHook(() => useIncidentList(props));

await waitFor(() => expect(mockGetList).toHaveBeenCalled());

expect(mockGetList.mock.calls[0][0]).toEqual(
expect.objectContaining({ offset: 20, testCaseFQN: 'x' })
);
} finally {
pagingMock.mockImplementation(defaultPaging);
}
});

it('should request deleted incidents scoped to the origin table when tableDetails is deleted', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from 'react';
import { DEFAULT_DOMAIN_VALUE } from '../../constants/constants';
Expand All @@ -25,8 +26,8 @@
import { Include } from '../../generated/type/include';
import { usePaging } from '../../hooks/paging/usePaging';
import { useDomainStore } from '../../hooks/useDomainStore';
import { TestCaseIncidentStatusData } from '../../pages/IncidentManager/IncidentManager.interface';

Check warning on line 29 in openmetadata-ui/src/main/resources/ui/src/components/IncidentManager/useIncidentList.ts

View workflow job for this annotation

GitHub Actions / ui / lint

Pages are route-level composition modules. Move the shared implementation/type to a lower layer instead of importing a page from here
import { Option } from '../../pages/TasksPage/TasksPage.interface';

Check warning on line 30 in openmetadata-ui/src/main/resources/ui/src/components/IncidentManager/useIncidentList.ts

View workflow job for this annotation

GitHub Actions / ui / lint

Pages are route-level composition modules. Move the shared implementation/type to a lower layer instead of importing a page from here
import {
getListTestCaseIncidentStatusFromSearch,
TestCaseIncidentStatusParams,
Expand Down Expand Up @@ -75,8 +76,13 @@
handlePageSizeChange,
} = usePaging();

// Only the latest request may write rows and paging: a slower response for a
// page the user already left must not overwrite the page they are on.
const latestRequestId = useRef(0);

const fetchTestCaseIncidents = useCallback(
async (params: TestCaseIncidentStatusParams) => {
const requestId = ++latestRequestId.current;
setTestCaseListData((prev) => ({ ...prev, isLoading: true }));
try {
const { data, paging } = await getListTestCaseIncidentStatusFromSearch({
Expand All @@ -89,6 +95,9 @@
activeDomain === DEFAULT_DOMAIN_VALUE ? undefined : activeDomain,
...params,
});
if (requestId !== latestRequestId.current) {
return;
}
const assigneeOptions = data.reduce((acc, curr) => {
const assignee = curr.testCaseResolutionStatusDetails?.assignee;
const isExist = acc.some((item) => item.value === assignee?.name);
Expand All @@ -112,12 +121,16 @@
setTestCaseListData((prev) => ({ ...prev, data: data }));
handlePagingChange(paging);
} catch (error) {
showErrorToast(error as AxiosError);
if (requestId === latestRequestId.current) {
showErrorToast(error as AxiosError);
}
} finally {
setTestCaseListData((prev) => ({ ...prev, isLoading: false }));
if (requestId === latestRequestId.current) {
setTestCaseListData((prev) => ({ ...prev, isLoading: false }));
}
}
},
[

Check warning on line 133 in openmetadata-ui/src/main/resources/ui/src/components/IncidentManager/useIncidentList.ts

View workflow job for this annotation

GitHub Actions / ui / lint

React Hook useCallback has missing dependencies: 'handlePagingChange' and 'setUsers'. Either include them or remove the dependency array. If 'setUsers' changes too often, find the parent component that defines it and wrap that definition in useCallback
pageSize,
setTestCaseListData,
activeDomain,
Expand All @@ -126,15 +139,10 @@
]
);

// A page change reaches the fetch effect through `currentPage`.
const handlePagingClick = useCallback(
({ currentPage }: PagingHandlerParams) => {
fetchTestCaseIncidents({
...filters,
offset: (currentPage - 1) * pageSize,
});
handlePageChange(currentPage);
},
[fetchTestCaseIncidents, filters, pageSize, handlePageChange]
({ currentPage: page }: PagingHandlerParams) => handlePageChange(page),
[handlePageChange]
);

const pagingData = useMemo(
Expand All @@ -159,12 +167,16 @@

useEffect(() => {
if (hasViewAccess) {
fetchTestCaseIncidents(filters);
fetchTestCaseIncidents({
...filters,
offset: (currentPage - 1) * pageSize,
});
} else {
setTestCaseListData((prev) => ({ ...prev, isLoading: false }));
}
}, [

Check warning on line 177 in openmetadata-ui/src/main/resources/ui/src/components/IncidentManager/useIncidentList.ts

View workflow job for this annotation

GitHub Actions / ui / lint

React Hook useEffect has a missing dependency: 'fetchTestCaseIncidents'. Either include it or remove the dependency array
hasViewAccess,
currentPage,
pageSize,
filters,
activeDomain,
Expand Down
Loading
Loading