import { useParams } from 'react-router-dom'; import { act, screen } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import { downloadYaml } from '~/utils/common-utils'; import { useAccessReviewForModel } from '~/utils/rbac'; import { useK8sAndKarchResource } from '../../../hooks/useK8sAndKarchResources'; import { usePipelineRunV2 } from '../../../hooks/usePipelineRunsV2'; import { PipelineRunGroupVersionKind, SnapshotGroupVersionKind } from '../../../models'; import { IntegrationTestScenarioKind } from '../../../types/coreBuildService'; import { ResourceSource, WatchK8sResource } from '../../../types/k8s'; import { renderWithQueryClientAndRouter } from '../../../utils/test-utils'; import { pipelineWithCommits } from '../../Commits/__data__/pipeline-with-commits'; import { useCommitStatus } from '../../Commits/commit-status'; import { MockSnapshots } from '../../Commits/CommitDetails/visualization/__data__/MockCommitWorkflowData'; import SnapshotDetails from '../SnapshotDetailsView'; jest.mock('../../Commits/commit-status', () => ({ useCommitStatus: jest.fn(), })); jest.mock('react-router-dom', () => { const actual = jest.requireActual('react-router-dom'); return { ...actual, Link: (props) => {props.children}, useNavigate: () => {}, useParams: jest.fn(), }; }); jest.mock('../../../hooks/useTektonResults'); jest.mock('../../../utils/rbac', () => ({ useAccessReviewForModel: jest.fn(() => [true, true]), })); const useParamsMock = useParams as jest.Mock; jest.mock('../../../hooks/useK8sAndKarchResources', () => ({ useK8sAndKarchResource: jest.fn(), })); jest.mock('../../../hooks/usePipelineRunsV2', () => ({ usePipelineRunV2: jest.fn(), })); 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', }), }; }); const useSnapshotMock = useK8sAndKarchResource as jest.Mock; const usePipelineRunV2Mock = usePipelineRunV2 as jest.Mock; const downloadYamlMock = downloadYaml as jest.Mock; const mockSnapshots: IntegrationTestScenarioKind[] = [...MockSnapshots]; const errorSnapshotResources = (params: WatchK8sResource) => { if (params?.groupVersionKind === SnapshotGroupVersionKind) { return { data: mockSnapshots[0], isLoading: false }; } if (params?.groupVersionKind === PipelineRunGroupVersionKind) { return { data: pipelineWithCommits[0], isLoading: false }; } return { data: undefined, isLoading: false }; }; describe('SnapshotDetailsView', () => { beforeEach(() => { useParamsMock.mockReturnValue({ snapshotName: 'my-test-output-1', applicationName: 'my-test-output', }); (useCommitStatus as jest.Mock).mockReturnValueOnce(['-', true]); usePipelineRunV2Mock.mockReturnValue([pipelineWithCommits[0], true, false]); }); it('should render loading indicator', () => { useSnapshotMock.mockReturnValue({ data: undefined, isLoading: true }); renderWithQueryClientAndRouter(); screen.getByRole('progressbar'); }); it('should show error state if test cannot be loaded', () => { useSnapshotMock.mockReturnValue({ data: undefined, isLoading: false, fetchError: { message: 'Application does not exist', code: 404 }, }); renderWithQueryClientAndRouter(); screen.getByText('404: Page not found'); }); it('should display snapshot data when loaded', () => { useSnapshotMock.mockReturnValue({ data: mockSnapshots[0], isLoading: false, }); renderWithQueryClientAndRouter(); expect(screen.getByTestId('snapshot-header-details')).toBeInTheDocument(); expect(screen.getByTestId('snapshot-name').innerHTML).toBe('my-test-output-1'); }); it('should display correct breadcrumbs', () => { useSnapshotMock.mockReturnValue({ data: mockSnapshots[0], isLoading: false, }); renderWithQueryClientAndRouter(); expect(screen.getByRole('link', { name: 'Applications' })).toHaveAttribute( 'href', '/ns//applications', ); expect(screen.getByRole('link', { name: 'Snapshots' })).toHaveAttribute( 'href', '/ns//applications/my-test-output/snapshots', ); expect(screen.getAllByRole('tab').map((tab) => tab.textContent.trim())).toEqual([ 'Overview', 'Pipeline runs', ]); }); it('should show EnvProvisionError', () => { mockSnapshots[0].metadata.deletionTimestamp = '1'; useSnapshotMock.mockImplementation(errorSnapshotResources); useParamsMock.mockReturnValue({ snapshotName: 'my-test-output-2', applicationName: 'my-test-output', }); renderWithQueryClientAndRouter(); screen.queryByTestId('env-provision-err-alert'); screen.queryByText('Failed for app-sample-go-basic-enterprise-contract'); }); it('should show failed scenario on EnvProvisionError', () => { mockSnapshots[0].metadata.deletionTimestamp = '1'; useSnapshotMock.mockImplementation(errorSnapshotResources); useParamsMock.mockReturnValue({ snapshotName: 'my-test-output-2', applicationName: 'my-test-output', }); renderWithQueryClientAndRouter(); screen.queryByText('scn 2'); }); it('should render without crashing when snapshot has no labels', () => { useSnapshotMock.mockReturnValue({ data: mockSnapshots[0], isLoading: false, }); useParamsMock.mockReturnValue({ snapshotName: 'my-test-output-no-labels', applicationName: 'my-test-output', }); renderWithQueryClientAndRouter(); expect(screen.getByTestId('snapshot-header-details')).toBeInTheDocument(); expect(screen.getByTestId('snapshot-name').innerHTML).toBe('my-test-output-no-labels'); }); it('should show enabled trigger release action for cluster snapshot', async () => { useSnapshotMock.mockReturnValue({ data: mockSnapshots[0], isLoading: false, source: ResourceSource.Cluster, }); renderWithQueryClientAndRouter(); const actionsButton = screen.getByRole('button', { name: /Actions/i }); await act(async () => { await userEvent.click(actionsButton); }); const triggerAction = screen.getByText('Trigger release'); expect(triggerAction).toBeInTheDocument(); expect(triggerAction.closest('button, a')).not.toHaveAttribute('aria-disabled', 'true'); }); it('should show disabled trigger release action for archived snapshot', async () => { useSnapshotMock.mockReturnValue({ data: mockSnapshots[0], isLoading: false, source: ResourceSource.Archive, }); renderWithQueryClientAndRouter(); const actionsButton = screen.getByRole('button', { name: /Actions/i }); await act(async () => { await userEvent.click(actionsButton); }); const triggerAction = screen.getByText('Trigger release'); expect(triggerAction).toBeInTheDocument(); expect(triggerAction.closest('button, a')).toHaveAttribute('aria-disabled', 'true'); }); it('disables release for a cluster snapshot when permission is denied', async () => { const user = userEvent.setup(); jest.mocked(useAccessReviewForModel).mockReturnValueOnce([false, true]); useSnapshotMock.mockReturnValue({ data: mockSnapshots[0], isLoading: false, source: ResourceSource.Cluster, }); renderWithQueryClientAndRouter(); await user.click(screen.getByRole('button', { name: 'Actions' })); expect(screen.getByText('Trigger release').closest('button, a')).toHaveAttribute( 'aria-disabled', 'true', ); }); describe('Download YAML action', () => { it('should render Download YAML action in actions dropdown', async () => { const user = userEvent.setup(); useSnapshotMock.mockReturnValue({ data: mockSnapshots[0], isLoading: false, source: ResourceSource.Cluster, }); renderWithQueryClientAndRouter(); 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 snapshot when Download YAML is clicked', async () => { const user = userEvent.setup(); useSnapshotMock.mockReturnValue({ data: mockSnapshots[0], isLoading: false, source: ResourceSource.Cluster, }); renderWithQueryClientAndRouter(); 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(mockSnapshots[0]); }); }); });