import '@testing-library/jest-dom';
import { screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { testPipelineRuns, DataState } from '~/__data__/pipelinerun-data';
import { PipelineRunLabel, runStatus } from '~/consts/pipelinerun';
import { CONFORMA_TASK, ENTERPRISE_CONTRACT_LABEL, ROXCTL_SCAN_TASK } from '~/consts/security';
import { useStatusOnFavicon } from '~/hooks/useStatusOnFavicon';
import { PipelineRunKind } from '~/types';
import { mockUseNamespaceHook } from '~/unit-test-utils/mock-namespace';
import { createPipelineRunMockStates } from '~/unit-test-utils/mock-pipelinerun-test-utils';
import { createUseParamsMock, routerRenderer } from '~/unit-test-utils/mock-react-router';
import { downloadYaml } from '~/utils/common-utils';
import { PipelineRunDetailsView } from '../PipelineRunDetailsView';
jest.mock('~/hooks/useStatusOnFavicon', () => ({
useStatusOnFavicon: jest.fn(),
}));
// Reuse existing mock data from src/__data__/pipelinerun-data.ts
const mockPipelineRun: PipelineRunKind = {
...testPipelineRuns[DataState.SUCCEEDED],
metadata: {
...testPipelineRuns[DataState.SUCCEEDED].metadata,
name: 'test-pipeline-run',
namespace: 'test-namespace',
labels: {
...testPipelineRuns[DataState.SUCCEEDED].metadata.labels,
[PipelineRunLabel.APPLICATION]: 'test-app',
[PipelineRunLabel.PIPELINE_NAME]: 'test-pipeline',
},
},
};
const mockEnterpriseContractPipelineRun: PipelineRunKind = {
...mockPipelineRun,
metadata: {
...mockPipelineRun.metadata,
labels: {
...mockPipelineRun.metadata.labels,
[PipelineRunLabel.PIPELINE_NAME]: 'enterprise-contract',
[ENTERPRISE_CONTRACT_LABEL]: 'enterprise-contract',
},
},
};
const mockConformaPipelineRun: PipelineRunKind = {
...mockPipelineRun,
status: {
...mockPipelineRun.status,
pipelineSpec: {
...mockPipelineRun.status.pipelineSpec,
tasks: [{ name: CONFORMA_TASK }],
},
},
};
const mockRoxctlPipelineRun: PipelineRunKind = {
...mockPipelineRun,
status: {
...mockPipelineRun.status,
pipelineSpec: {
...mockPipelineRun.status.pipelineSpec,
tasks: [{ name: ROXCTL_SCAN_TASK }],
},
},
};
const mockSecurityAndRoxctlPipelineRun: PipelineRunKind = {
...mockRoxctlPipelineRun,
status: {
...mockRoxctlPipelineRun.status,
pipelineSpec: {
...mockRoxctlPipelineRun.status.pipelineSpec,
tasks: [{ name: ROXCTL_SCAN_TASK }, { name: CONFORMA_TASK }],
},
},
};
const mockUsePipelineRunV2 = jest.fn();
const mockUsePipelinererunAction = jest.fn();
const mockUseAccessReviewForModel = jest.fn();
jest.mock('../../../../hooks/usePipelineRunsV2', () => ({
usePipelineRunV2: (...args: unknown[]) => mockUsePipelineRunV2(...args),
}));
jest.mock('../../PipelineRunListView/pipelinerun-actions', () => ({
usePipelinererunAction: (...args: unknown[]) => mockUsePipelinererunAction(...args),
}));
jest.mock('../../../../utils/rbac', () => ({
useAccessReviewForModel: (...args: unknown[]) => mockUseAccessReviewForModel(...args),
}));
jest.mock('~/utils/common-utils', () => {
const actual = jest.requireActual('~/utils/common-utils');
const mockDownloadYaml = jest.fn();
return {
...actual,
downloadYaml: mockDownloadYaml,
downloadYamlAction: (obj: { kind?: string }) => ({
cta: () => mockDownloadYaml(obj),
id: `download-${(obj.kind ?? 'resource').toLowerCase()}-yaml`,
label: 'Download YAML',
}),
};
});
// Shared mock state helpers
const mockPipelineRunStates = createPipelineRunMockStates();
const downloadYamlMock = downloadYaml as jest.Mock;
const useStatusOnFaviconMock = useStatusOnFavicon as jest.Mock;
// We are testing:
// component composition, loading/error states, tab rendering logic, action setup, query param handling
describe('PipelineRunDetailsView', () => {
const mockNamespace = 'test-namespace';
const mockPipelineRunName = 'test-pipeline-run';
const useNamespaceMock = mockUseNamespaceHook(mockNamespace);
const useParamsMock = createUseParamsMock();
beforeEach(() => {
jest.clearAllMocks();
useNamespaceMock.mockReturnValue(mockNamespace);
useParamsMock.mockReturnValue({ pipelineRunName: mockPipelineRunName });
mockUsePipelinererunAction.mockReturnValue({
cta: jest.fn(),
isDisabled: false,
disabledTooltip: '',
key: 'rerun',
label: 'Rerun',
});
mockUseAccessReviewForModel.mockReturnValue([true, true]);
});
it('should render spinner when loading pipeline run', () => {
mockUsePipelineRunV2.mockReturnValue(mockPipelineRunStates.loading());
routerRenderer();
expect(screen.getByRole('progressbar')).toBeInTheDocument();
});
it('should call usePipelineRunV2 with correct parameters', () => {
mockUsePipelineRunV2.mockReturnValue(mockPipelineRunStates.loaded(mockPipelineRun));
routerRenderer();
expect(mockUsePipelineRunV2).toHaveBeenCalledWith(mockNamespace, mockPipelineRunName);
});
it('should render error state when pipeline run fails to load', () => {
mockUsePipelineRunV2.mockReturnValue(
mockPipelineRunStates.error({ message: 'Pipeline run not found', code: 404 }),
);
routerRenderer();
expect(screen.getByText('404: Page not found')).toBeInTheDocument();
});
it('should render error state when pipeline run is not found', () => {
mockUsePipelineRunV2.mockReturnValue(mockPipelineRunStates.notFound());
routerRenderer();
expect(screen.getByText('404: Page not found')).toBeInTheDocument();
});
it('should render DetailsPage when pipeline run is loaded', () => {
mockUsePipelineRunV2.mockReturnValue(mockPipelineRunStates.loaded(mockPipelineRun));
routerRenderer();
// Find the breadcrumb navigation and verify it contains the pipeline run name
const breadcrumb = screen.getByRole('navigation', { name: 'Breadcrumb' });
expect(breadcrumb).toHaveTextContent(mockPipelineRunName);
expect(breadcrumb).toHaveTextContent('Pipeline runs');
// Find the status label and verify it shows success
const statusLabel = screen.getByText('Succeeded').closest('.pf-v6-c-label');
expect(statusLabel).toHaveClass('pf-m-green');
expect(statusLabel).toHaveTextContent('Succeeded');
// Find the Details tab and verify it's present
const detailsTab = screen.getByRole('tab', { name: /details/i });
expect(detailsTab).toBeInTheDocument();
});
it('passes pipeline run status to useStatusOnFavicon when loaded', () => {
mockUsePipelineRunV2.mockReturnValue(mockPipelineRunStates.loaded(mockPipelineRun));
routerRenderer();
expect(useStatusOnFaviconMock).toHaveBeenCalledWith(runStatus.Succeeded);
});
it('passes null to useStatusOnFavicon while pipeline run is loading', () => {
mockUsePipelineRunV2.mockReturnValue(mockPipelineRunStates.loading());
routerRenderer();
expect(useStatusOnFaviconMock).toHaveBeenCalledWith(null);
});
it('passes null to useStatusOnFavicon when pipeline run fails to load', () => {
mockUsePipelineRunV2.mockReturnValue(
mockPipelineRunStates.error({ message: 'Pipeline run not found', code: 404 }),
);
routerRenderer();
expect(useStatusOnFaviconMock).toHaveBeenCalledWith(null);
});
it('should render standard tabs for non-enterprise-contract pipeline', () => {
mockUsePipelineRunV2.mockReturnValue(mockPipelineRunStates.loaded(mockPipelineRun));
routerRenderer();
// Should have Details, Task runs, Logs tabs
expect(screen.getByRole('tab', { name: /details/i })).toBeInTheDocument();
expect(screen.getByRole('tab', { name: /task runs/i })).toBeInTheDocument();
expect(screen.getByRole('tab', { name: /logs/i })).toBeInTheDocument();
// Should not have Security tab
expect(screen.queryByRole('tab', { name: /security/i })).not.toBeInTheDocument();
});
it('should render Security tab for enterprise-contract pipeline', () => {
mockUsePipelineRunV2.mockReturnValue(
mockPipelineRunStates.loaded(mockEnterpriseContractPipelineRun),
);
routerRenderer();
// Should have Details, Task runs, Logs, and Security tabs
expect(screen.getByRole('tab', { name: /details/i })).toBeInTheDocument();
expect(screen.getByRole('tab', { name: /task runs/i })).toBeInTheDocument();
expect(screen.getByRole('tab', { name: /logs/i })).toBeInTheDocument();
expect(screen.getByRole('tab', { name: /security/i })).toBeInTheDocument();
});
it('should render Security tab when verify-conforma task run exists', () => {
mockUsePipelineRunV2.mockReturnValue(mockPipelineRunStates.loaded(mockConformaPipelineRun));
routerRenderer();
expect(screen.getByRole('tab', { name: /details/i })).toBeInTheDocument();
expect(screen.getByRole('tab', { name: /task runs/i })).toBeInTheDocument();
expect(screen.getByRole('tab', { name: /logs/i })).toBeInTheDocument();
expect(screen.getByRole('tab', { name: /security/i })).toBeInTheDocument();
});
it('should render Vulnerabilities tab when roxctl-scan is in the pipeline', () => {
mockUsePipelineRunV2.mockReturnValue(mockPipelineRunStates.loaded(mockRoxctlPipelineRun));
routerRenderer();
expect(screen.getByRole('tab', { name: /details/i })).toBeInTheDocument();
expect(screen.getByRole('tab', { name: /task runs/i })).toBeInTheDocument();
expect(screen.getByRole('tab', { name: /logs/i })).toBeInTheDocument();
expect(screen.getByRole('tab', { name: /vulnerabilities/i })).toBeInTheDocument();
expect(screen.queryByRole('tab', { name: /security/i })).not.toBeInTheDocument();
});
it('should not render Vulnerabilities tab when roxctl-scan is not in the pipeline', () => {
mockUsePipelineRunV2.mockReturnValue(mockPipelineRunStates.loaded(mockPipelineRun));
routerRenderer();
expect(screen.queryByRole('tab', { name: /vulnerabilities/i })).not.toBeInTheDocument();
});
it('should render Security before Vulnerabilities', () => {
mockUsePipelineRunV2.mockReturnValue(
mockPipelineRunStates.loaded(mockSecurityAndRoxctlPipelineRun),
);
routerRenderer();
const tabLabels = screen.getAllByRole('tab').map((tab) => tab.textContent?.trim() ?? '');
const securityTabIndex = tabLabels.findIndex((label) => /security/i.test(label));
const vulnerabilitiesTabIndex = tabLabels.findIndex((label) => /vulnerabilities/i.test(label));
expect(securityTabIndex).toBeGreaterThanOrEqual(0);
expect(vulnerabilitiesTabIndex).toBeGreaterThanOrEqual(0);
expect(securityTabIndex).toBeLessThan(vulnerabilitiesTabIndex);
});
it('should check access review for PipelineRunModel', () => {
mockUsePipelineRunV2.mockReturnValue(mockPipelineRunStates.loaded(mockPipelineRun));
routerRenderer();
expect(mockUseAccessReviewForModel).toHaveBeenCalled();
});
it('should render DetailsPage with pipeline run data', () => {
mockUsePipelineRunV2.mockReturnValue(mockPipelineRunStates.loaded(mockPipelineRun));
routerRenderer();
// Find the breadcrumb and verify it shows the pipeline run name
const breadcrumb = screen.getByRole('navigation', { name: 'Breadcrumb' });
expect(breadcrumb).toHaveTextContent('test-pipeline-run');
// Find the actions button and verify it's enabled
const actionsButton = screen.getByRole('button', { name: /actions/i });
expect(actionsButton).not.toBeDisabled();
// Find the status label and verify it shows the correct status
const statusLabel = screen.getByText('Succeeded').closest('.pf-v6-c-label');
expect(statusLabel).toHaveClass('pf-m-green');
});
it('should use the route pipeline run name for details breadcrumbs', () => {
const pipelineRunWithDifferentMetadataName = {
...mockPipelineRun,
metadata: {
...mockPipelineRun.metadata,
name: 'metadata-pipeline-run-name',
},
};
mockUsePipelineRunV2.mockReturnValue(
mockPipelineRunStates.loaded(pipelineRunWithDifferentMetadataName),
);
routerRenderer();
const breadcrumb = screen.getByRole('navigation', { name: 'Breadcrumb' });
expect(breadcrumb).toHaveTextContent(mockPipelineRunName);
expect(breadcrumb).not.toHaveTextContent('metadata-pipeline-run-name');
});
describe('Download YAML action', () => {
it('should render Download YAML action in actions dropdown', async () => {
const user = userEvent.setup();
mockUsePipelineRunV2.mockReturnValue(mockPipelineRunStates.loaded(mockPipelineRun));
routerRenderer();
const actionsButton = screen.getByRole('button', { name: /actions/i });
await user.click(actionsButton);
expect(screen.getByRole('menuitem', { name: /Download YAML/i })).toBeInTheDocument();
});
it('should call downloadYaml with pipelineRun when Download YAML is clicked', async () => {
const user = userEvent.setup();
mockUsePipelineRunV2.mockReturnValue(mockPipelineRunStates.loaded(mockPipelineRun));
routerRenderer();
const actionsButton = screen.getByRole('button', { name: /actions/i });
await user.click(actionsButton);
const downloadAction = screen.getByRole('menuitem', { name: /Download YAML/i });
await user.click(downloadAction);
expect(downloadYamlMock).toHaveBeenCalledWith(mockPipelineRun);
});
});
});