import { useParams } from 'react-router-dom';
import { screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { useFeatureFlags, useIsOnFeatureFlag } from '~/feature-flags/hooks';
import { useComponentV2 } from '~/hooks/useComponentsV2';
import { renderWithQueryClientAndRouter } from '~/unit-test-utils';
import { mockUseNamespaceHook } from '~/unit-test-utils/mock-namespace';
import { ComponentDependencyTab } from '../ComponentDependencyTab';
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useParams: jest.fn(),
}));
jest.mock('~/hooks/useComponentsV2', () => ({
useComponentV2: jest.fn(),
}));
jest.mock('~/feature-flags/hooks', () => ({
...jest.requireActual('~/feature-flags/hooks'),
useFeatureFlags: jest.fn(),
useIsOnFeatureFlag: jest.fn(),
}));
jest.mock('~/components/MintMaker/DependencyRuns/DependencyRunsListView', () => ({
DependencyRunsListView: (props: {
applicationName?: string;
componentNames: string[];
filterByCreationTimestampAfter?: string;
isSingleComponent: boolean;
}) =>
{JSON.stringify(props)}
,
}));
const useParamsMock = useParams as jest.Mock;
const useComponentMock = useComponentV2 as jest.Mock;
const useFeatureFlagsMock = useFeatureFlags as jest.Mock;
const useIsOnFeatureFlagMock = useIsOnFeatureFlag as jest.Mock;
const setFlagMock = jest.fn();
const component = {
metadata: {
name: 'standalone-component',
creationTimestamp: '2023-01-01T00:00:00Z',
},
spec: {
componentName: 'standalone-component',
},
};
describe('ComponentsPage ComponentDependencyTab', () => {
mockUseNamespaceHook('test-ns');
beforeEach(() => {
useParamsMock.mockReturnValue({ componentName: 'standalone-component' });
useComponentMock.mockReturnValue([component, true, undefined]);
useFeatureFlagsMock.mockReturnValue([{}, setFlagMock]);
useIsOnFeatureFlagMock.mockReturnValue(true);
});
afterEach(() => {
jest.clearAllMocks();
});
it('shows an alert when PipelineRuns are not fetched from KubeArchive', async () => {
const user = userEvent.setup();
useIsOnFeatureFlagMock.mockReturnValue(false);
renderWithQueryClientAndRouter();
expect(screen.getByTestId('mintmaker-plr-alert')).toBeInTheDocument();
await user.click(screen.getByRole('button', { name: 'Use KubeArchive for pipeline runs' }));
expect(setFlagMock).toHaveBeenCalledWith('pipelineruns-kubearchive', true);
});
it('shows a spinner while the component is loading', () => {
useComponentMock.mockReturnValue([undefined, false, undefined]);
renderWithQueryClientAndRouter();
expect(screen.getByTestId('dependency-runs-spinner')).toBeInTheDocument();
});
it('shows an error when the component cannot be loaded', () => {
useComponentMock.mockReturnValue([undefined, true, new Error('component unavailable')]);
renderWithQueryClientAndRouter();
expect(screen.getByText('Unable to load dependency runs')).toBeInTheDocument();
});
it('passes only the standalone component scope to the run list', () => {
renderWithQueryClientAndRouter();
expect(useComponentMock).toHaveBeenCalledWith('test-ns', 'standalone-component', true);
expect(screen.getByTestId('dependency-runs-props')).toHaveTextContent(
JSON.stringify({
componentNames: ['standalone-component'],
filterByCreationTimestampAfter: '2023-01-01T00:00:00Z',
isSingleComponent: true,
}),
);
});
});