diff --git a/jest.config.ts b/jest.config.ts index de795c19a..a1d19af37 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -78,7 +78,7 @@ const config: Config.InitialOptions = { // ], // An array of file extensions your modules use - moduleFileExtensions: ['js', 'jsx', 'ts', 'tsx'], + moduleFileExtensions: ['js', 'jsx', 'ts', 'tsx', 'mjs'], // A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module moduleNameMapper: { @@ -173,7 +173,7 @@ const config: Config.InitialOptions = { // A map from regular expressions to paths to transformers transform: { - '^.+\\.(ts|tsx|js|jsx)?$': 'babel-jest', + '^.+\\.(ts|tsx|js|jsx|mjs)$': 'babel-jest', }, // An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation diff --git a/src/components/common/__tests__/DataViewFilterToolbar.spec.tsx b/src/components/common/__tests__/DataViewFilterToolbar.spec.tsx new file mode 100644 index 000000000..7a9ea2a94 --- /dev/null +++ b/src/components/common/__tests__/DataViewFilterToolbar.spec.tsx @@ -0,0 +1,42 @@ +import { render, screen } from '@testing-library/react'; +import { DataViewFilterToolbar } from '../DataViewFilterToolbar'; + +jest.mock('../MultiClusterFilterControl', () => ({ + MultiClusterFilterControl: ({ selected }: { selected: string }) => ( +
{selected || 'all'}
+ ), +})); + +const baseProps = { + filterValues: { name: '', labels: [] }, + onFilterChange: jest.fn(), + onClearAll: jest.fn(), + clusterFilter: { selected: 'spoke-1', onSelect: jest.fn() }, +}; + +describe('DataViewFilterToolbar cluster filter', () => { + it('should render the cluster filter for PipelineRuns', () => { + render(); + + expect(screen.getByTestId('cluster-filter').textContent).toBe('spoke-1'); + }); + + it('should hide the cluster filter for other resource types', () => { + render(); + + expect(screen.queryByTestId('cluster-filter')).toBeNull(); + }); + + it('should hide the cluster filter when clusterFilter is omitted', () => { + render( + , + ); + + expect(screen.queryByTestId('cluster-filter')).toBeNull(); + }); +}); diff --git a/src/components/common/__tests__/MultiClusterFilterControl.spec.tsx b/src/components/common/__tests__/MultiClusterFilterControl.spec.tsx new file mode 100644 index 000000000..8361c562d --- /dev/null +++ b/src/components/common/__tests__/MultiClusterFilterControl.spec.tsx @@ -0,0 +1,110 @@ +import { fireEvent, render, screen } from '@testing-library/react'; +import { MultiKueueClusterKind } from '../../../types'; +import { useMultiClusterProxyService } from '../../hooks/useMultiClusterProxyService'; +import { + isClusterConnected, + useMultiKueueClusters, +} from '../../hooks/useMultiKueueResources'; +import { MultiClusterFilterControl } from '../MultiClusterFilterControl'; + +jest.mock('../../hooks/useMultiClusterProxyService', () => ({ + useMultiClusterProxyService: jest.fn(), +})); + +jest.mock('../../hooks/useMultiKueueResources', () => ({ + isClusterConnected: jest.fn(), + useMultiKueueClusters: jest.fn(), +})); + +const useMultiClusterProxyServiceMock = + useMultiClusterProxyService as jest.Mock; +const useMultiKueueClustersMock = useMultiKueueClusters as jest.Mock; +const isClusterConnectedMock = isClusterConnected as jest.Mock; + +const spoke1: MultiKueueClusterKind = { metadata: { name: 'spoke-1' } }; +const spoke2: MultiKueueClusterKind = { metadata: { name: 'spoke-2' } }; + +describe('MultiClusterFilterControl', () => { + const onSelect = jest.fn(); + + beforeEach(() => { + useMultiClusterProxyServiceMock.mockReturnValue({ isHub: true }); + useMultiKueueClustersMock.mockReturnValue([[spoke1, spoke2], true, null]); + isClusterConnectedMock.mockImplementation( + (cluster: MultiKueueClusterKind) => cluster.metadata?.name === 'spoke-1', + ); + }); + + it('should render nothing on a non-hub cluster', () => { + useMultiClusterProxyServiceMock.mockReturnValue({ isHub: false }); + + const { container } = render( + , + ); + + expect(container.firstChild).toBeNull(); + }); + + it('should show an error alert when clusters fail to load', () => { + useMultiKueueClustersMock.mockReturnValue([undefined, false, new Error()]); + + render(); + + expect( + screen.getByText('Unable to load target cluster information'), + ).toBeTruthy(); + }); + + it('should show a loading spinner while clusters are loading', () => { + useMultiKueueClustersMock.mockReturnValue([undefined, false, null]); + + render(); + + expect(screen.getByLabelText('Loading clusters')).toBeTruthy(); + }); + + it('should disable the toggle when no clusters are available', () => { + useMultiKueueClustersMock.mockReturnValue([[], true, null]); + + render(); + + expect( + ( + screen.getByRole('button', { + name: 'All Clusters', + }) as HTMLButtonElement + ).disabled, + ).toBe(true); + }); + + it('should list cluster names and the status legend', () => { + render(); + + fireEvent.click(screen.getByRole('button', { name: 'All Clusters' })); + + expect(screen.getByText('spoke-1')).toBeTruthy(); + expect(screen.getByText('spoke-2')).toBeTruthy(); + expect(screen.getByText('Connected')).toBeTruthy(); + expect(screen.getByText('Disconnected')).toBeTruthy(); + }); + + it('should call onSelect with the cluster name', () => { + render(); + + fireEvent.click(screen.getByRole('button', { name: 'All Clusters' })); + fireEvent.click(screen.getByText('spoke-1')); + + expect(onSelect).toHaveBeenCalledWith('spoke-1'); + }); + + it('should show a clear button that resets the selection', () => { + render( + , + ); + + expect(screen.getByRole('button', { name: /spoke-1/ })).toBeTruthy(); + fireEvent.click(screen.getByLabelText('Clear cluster filter')); + + expect(onSelect).toHaveBeenCalledWith(''); + }); +}); diff --git a/src/components/hooks/__tests__/useDataViewFilter.spec.ts b/src/components/hooks/__tests__/useDataViewFilter.spec.ts new file mode 100644 index 000000000..6c958849a --- /dev/null +++ b/src/components/hooks/__tests__/useDataViewFilter.spec.ts @@ -0,0 +1,178 @@ +import { act } from '@testing-library/react'; +import { useFlag } from '@openshift-console/dynamic-plugin-sdk'; +import { WORKLOAD_KUEUE_UID_LABEL } from '../../../consts'; +import { testHook } from '../../../test-data/utils/hooks-utils'; +import { PipelineRunKind, WorkloadKind } from '../../../types'; +import { useDataViewFilter } from '../useDataViewFilter'; +import { useDateRangeFilter } from '../useDateRangeFilter'; +import { useDatasourcePreference } from '../useDatasourcePreference'; +import { useWorkloads } from '../useMultiKueueResources'; + +jest.mock('@openshift-console/dynamic-plugin-sdk', () => ({ + useFlag: jest.fn(), + getGroupVersionKindForModel: jest.fn((model) => ({ + group: model?.apiGroup, + version: model?.apiVersion, + kind: model?.kind, + })), + useK8sWatchResource: jest.fn(() => [[], true, undefined]), +})); + +jest.mock('react-router', () => ({ + useSearchParams: () => [new URLSearchParams(), jest.fn()], +})); + +jest.mock('../useDateRangeFilter', () => ({ + NO_DATE_RANGE_FILTER: 0, + parseDurationForDateRangeFiltering: jest.fn(), + useDateRangeFilter: jest.fn(), +})); + +jest.mock('../useDatasourcePreference', () => ({ + useDatasourcePreference: jest.fn(), +})); + +jest.mock('../useMultiKueueResources', () => ({ + useWorkloads: jest.fn(), +})); + +const useFlagMock = useFlag as jest.Mock; +const useDateRangeFilterMock = useDateRangeFilter as jest.Mock; +const useDatasourcePreferenceMock = useDatasourcePreference as jest.Mock; +const useWorkloadsMock = useWorkloads as jest.Mock; + +const makePipelineRun = (name: string, uid: string): PipelineRunKind => + ({ + apiVersion: 'tekton.dev/v1', + kind: 'PipelineRun', + metadata: { name, uid, namespace: 'test-ns' }, + spec: {}, + } as PipelineRunKind); + +const makeWorkload = (uid: string, clusterName: string): WorkloadKind => ({ + metadata: { labels: { [WORKLOAD_KUEUE_UID_LABEL]: uid } }, + status: { clusterName }, +}); + +const plrSpoke1 = makePipelineRun('plr-1', 'uid-1'); +const plrSpoke2 = makePipelineRun('plr-2', 'uid-2'); +const workloads = [ + makeWorkload('uid-1', 'spoke-1'), + makeWorkload('uid-2', 'spoke-2'), +]; + +describe('useDataViewFilter cluster filter', () => { + beforeEach(() => { + useFlagMock.mockReturnValue(false); + useDateRangeFilterMock.mockReturnValue({ + startDate: undefined, + timespan: 0, + setTimespanDateFilter: jest.fn(), + dateFilterCEL: '', + preferenceLoaded: true, + }); + useDatasourcePreferenceMock.mockReturnValue({ + preference: ['cluster-data'], + setPreference: jest.fn(), + clearPreference: jest.fn(), + loaded: true, + }); + useWorkloadsMock.mockReturnValue([workloads, true, null]); + }); + + it('should watch workloads for PipelineRuns, using "-" when namespace is missing', () => { + testHook(() => + useDataViewFilter({ + data: [plrSpoke1], + options: { resourceType: 'PipelineRun' }, + }), + ); + + expect(useWorkloadsMock).toHaveBeenCalledWith('-'); + }); + + it('should watch workloads in the given namespace for PipelineRuns', () => { + testHook(() => + useDataViewFilter({ + data: [plrSpoke1], + options: { resourceType: 'PipelineRun', namespace: 'test-ns' }, + }), + ); + + expect(useWorkloadsMock).toHaveBeenCalledWith('test-ns'); + }); + + it('should not watch workloads for other resource types', () => { + testHook(() => + useDataViewFilter({ + data: [], + options: { resourceType: 'Pipeline', namespace: 'test-ns' }, + }), + ); + + expect(useWorkloadsMock).toHaveBeenCalledWith(undefined); + }); + + it('should filter PipelineRuns by the selected spoke cluster', () => { + const { result } = testHook(() => + useDataViewFilter({ + data: [plrSpoke1, plrSpoke2], + options: { resourceType: 'PipelineRun', namespace: 'test-ns' }, + }), + ); + + act(() => { + result.current.onSelectCluster('spoke-1'); + }); + + expect(result.current.selectedCluster).toBe('spoke-1'); + expect(result.current.filteredData).toEqual([plrSpoke1]); + }); + + it('should not apply the cluster filter until workloads have loaded', () => { + useWorkloadsMock.mockReturnValue([[], false, null]); + + const { result } = testHook(() => + useDataViewFilter({ + data: [plrSpoke1, plrSpoke2], + options: { resourceType: 'PipelineRun', namespace: 'test-ns' }, + }), + ); + + act(() => { + result.current.onSelectCluster('spoke-1'); + }); + + expect(result.current.filteredData).toEqual([plrSpoke1, plrSpoke2]); + }); + + it('should reset the cluster selection on clear all', () => { + const { result } = testHook(() => + useDataViewFilter({ + data: [plrSpoke1, plrSpoke2], + options: { resourceType: 'PipelineRun', namespace: 'test-ns' }, + }), + ); + + act(() => { + result.current.onSelectCluster('spoke-1'); + }); + act(() => { + result.current.onClearAll(); + }); + + expect(result.current.selectedCluster).toBe(''); + expect(result.current.filteredData).toEqual([plrSpoke1, plrSpoke2]); + }); + + it('should return workloads from the watch', () => { + const { result } = testHook(() => + useDataViewFilter({ + data: [plrSpoke1], + options: { resourceType: 'PipelineRun', namespace: 'test-ns' }, + }), + ); + + expect(result.current.workloads).toEqual(workloads); + }); +}); diff --git a/src/components/hooks/__tests__/useMultiKueueResources.spec.ts b/src/components/hooks/__tests__/useMultiKueueResources.spec.ts index 3989d973f..1c476ef28 100644 --- a/src/components/hooks/__tests__/useMultiKueueResources.spec.ts +++ b/src/components/hooks/__tests__/useMultiKueueResources.spec.ts @@ -220,4 +220,25 @@ describe('useWorkloads', () => { expect(useK8sWatchResourceMock).toHaveBeenCalledWith(null); }); + + it('should skip the watch when namespace is missing', () => { + testHook(() => useWorkloads(undefined)); + + expect(useK8sWatchResourceMock).toHaveBeenCalledWith(null); + }); + + it('should watch all workloads in a namespace when no PipelineRun is given', () => { + testHook(() => useWorkloads('test-ns')); + + expect(useK8sWatchResourceMock).toHaveBeenCalledWith({ + isList: true, + groupVersionKind: { + group: WorkloadModel.apiGroup, + kind: WorkloadModel.kind, + version: WorkloadModel.apiVersion, + }, + namespace: 'test-ns', + namespaced: true, + }); + }); }); diff --git a/src/components/hooks/useDataViewFilter.ts b/src/components/hooks/useDataViewFilter.ts index bf35b4fff..5ddd741f4 100644 --- a/src/components/hooks/useDataViewFilter.ts +++ b/src/components/hooks/useDataViewFilter.ts @@ -201,7 +201,7 @@ export const useDataViewFilter = ({ const isPipelineRunType = resourceType === 'PipelineRun'; const [selectedCluster, setSelectedCluster] = useState(''); const [workloads, workloadsLoaded] = useWorkloads( - isPipelineRunType ? namespace : undefined, + isPipelineRunType ? namespace || '-' : undefined, ); const checkboxFilters = useMemo(() => { diff --git a/src/components/pipelineRuns-list/__tests__/PipelineRunsList.spec.tsx b/src/components/pipelineRuns-list/__tests__/PipelineRunsList.spec.tsx index bc2d4d67a..0a0b2ddff 100644 --- a/src/components/pipelineRuns-list/__tests__/PipelineRunsList.spec.tsx +++ b/src/components/pipelineRuns-list/__tests__/PipelineRunsList.spec.tsx @@ -47,17 +47,21 @@ jest.mock('../../hooks/useDataViewFilter', () => ({ ? [{ id: 'timeRange', singleSelect: true, options: [] }] : [], preferenceLoaded: preferenceLoaded ?? true, + selectedCluster: '', + onSelectCluster: jest.fn(), + workloads: [], }; }, })); jest.mock('../../common/DataViewFilterToolbar', () => ({ - DataViewFilterToolbar: ({ checkboxFilters }) => ( + DataViewFilterToolbar: ({ checkboxFilters, clusterFilter }) => (
{checkboxFilters?.map((f) => f.singleSelect ? ( ) : null, )} + {clusterFilter &&
}
), })); @@ -104,8 +108,7 @@ describe('PipelineRunsList', () => { }); it('should pass dateFilterCEL to useGetPipelineRuns', () => { - const cel = - 'data.status.startTime > timestamp("2026-06-14T00:00:00.000Z")'; + const cel = 'data.status.startTime > timestamp("2026-06-14T00:00:00.000Z")'; useDateRangeFilterMock.mockReturnValue( makeDateRange({ dateFilterCEL: cel }), ); @@ -169,4 +172,12 @@ describe('PipelineRunsList', () => { render(); expect(screen.queryByTestId('filter-toolbar')).toBeNull(); }); + + it('should pass cluster filter and workloads to the toolbar and table', () => { + render(); + expect(screen.getByTestId('cluster-filter')).toBeTruthy(); + expect( + consoleDataViewMock.mock.calls[0][0].customRowData.workloads, + ).toEqual([]); + }); }); diff --git a/src/components/pipelineRuns-list/__tests__/usePipelineRunsColumns.spec.ts b/src/components/pipelineRuns-list/__tests__/usePipelineRunsColumns.spec.ts new file mode 100644 index 000000000..268e7b09d --- /dev/null +++ b/src/components/pipelineRuns-list/__tests__/usePipelineRunsColumns.spec.ts @@ -0,0 +1,115 @@ +import { SortByDirection } from '@patternfly/react-table'; +import { WORKLOAD_KUEUE_UID_LABEL } from '../../../consts'; +import { testHook } from '../../../test-data/utils/hooks-utils'; +import { PipelineRunKind, WorkloadKind } from '../../../types'; +import { useMultiClusterProxyService } from '../../hooks/useMultiClusterProxyService'; +import { getClusterNameForPipelineRun } from '../PipelineRunsRow'; +import usePipelineRunsColumns from '../usePipelineRunsColumns'; + +jest.mock('@openshift-console/dynamic-plugin-sdk', () => ({ + ResourceLink: () => null, + Timestamp: () => null, + getGroupVersionKindForModel: jest.fn(), +})); + +jest.mock('@openshift-console/dynamic-plugin-sdk-internal', () => ({ + actionsCellProps: {}, + getNameCellProps: jest.fn(), + LazyActionMenu: () => null, +})); + +jest.mock('../../hooks/useMultiClusterProxyService', () => ({ + useMultiClusterProxyService: jest.fn(), +})); + +const useMultiClusterProxyServiceMock = + useMultiClusterProxyService as jest.Mock; + +const makePipelineRun = (name: string, uid: string): PipelineRunKind => + ({ + apiVersion: 'tekton.dev/v1', + kind: 'PipelineRun', + metadata: { name, uid, namespace: 'test-ns' }, + spec: {}, + } as PipelineRunKind); + +const makeWorkload = (uid: string, clusterName?: string): WorkloadKind => ({ + metadata: { labels: { [WORKLOAD_KUEUE_UID_LABEL]: uid } }, + status: clusterName ? { clusterName } : {}, +}); + +describe('getClusterNameForPipelineRun', () => { + it('should return the spoke cluster from the matching workload', () => { + const plr = makePipelineRun('plr', 'uid-1'); + const workloads = [makeWorkload('uid-1', 'spoke-1')]; + + expect(getClusterNameForPipelineRun(plr, workloads)).toBe('spoke-1'); + }); + + it('should return "-" when no workload matches', () => { + const plr = makePipelineRun('plr', 'uid-1'); + + expect(getClusterNameForPipelineRun(plr, [])).toBe('-'); + }); + + it('should return "-" when the workload has no cluster name', () => { + const plr = makePipelineRun('plr', 'uid-1'); + + expect(getClusterNameForPipelineRun(plr, [makeWorkload('uid-1')])).toBe( + '-', + ); + }); +}); + +describe('usePipelineRunsColumns', () => { + beforeEach(() => { + useMultiClusterProxyServiceMock.mockReturnValue({ isHub: false }); + }); + + it('should omit the Cluster column on a non-hub cluster', () => { + const { result } = testHook(() => + usePipelineRunsColumns('test-ns', false, []), + ); + + expect( + result.current.find((c) => c.id === 'spoke-cluster'), + ).toBeUndefined(); + }); + + it('should include a Cluster column on a hub cluster', () => { + useMultiClusterProxyServiceMock.mockReturnValue({ isHub: true }); + + const { result } = testHook(() => + usePipelineRunsColumns('test-ns', false, []), + ); + + const clusterColumn = result.current.find((c) => c.id === 'spoke-cluster'); + expect(clusterColumn?.title).toBe('Cluster'); + }); + + it('should sort PipelineRuns by spoke cluster name', () => { + useMultiClusterProxyServiceMock.mockReturnValue({ isHub: true }); + const plrA = makePipelineRun('plr-a', 'uid-a'); + const plrB = makePipelineRun('plr-b', 'uid-b'); + const workloads = [ + makeWorkload('uid-a', 'zeta'), + makeWorkload('uid-b', 'alpha'), + ]; + + const { result } = testHook(() => + usePipelineRunsColumns('test-ns', false, workloads), + ); + const clusterColumn = result.current.find((c) => c.id === 'spoke-cluster'); + const sort = clusterColumn?.sort as ( + data: PipelineRunKind[], + direction: SortByDirection, + ) => PipelineRunKind[]; + + expect( + sort([plrA, plrB], SortByDirection.asc).map((p) => p.metadata.name), + ).toEqual(['plr-b', 'plr-a']); + expect( + sort([plrA, plrB], SortByDirection.desc).map((p) => p.metadata.name), + ).toEqual(['plr-a', 'plr-b']); + }); +});