import { render, screen, waitFor } from '@testing-library/react'; import '@testing-library/jest-dom'; import { runStatus } from '~/consts/pipelinerun'; import { useTRTaskRunLog } from '~/hooks/useTektonResults'; import { TaskRunKind } from '~/types'; import { taskRunStatus } from '~/utils/pipeline-utils'; import { TektonTaskRunLog } from '../TektonTaskRunLog'; const mockLogViewer = jest.fn(); jest.mock('../LogViewer', () => { return function MockLogViewer(props: { normalizedSections: Array<{ containerName: string; lines: string[]; isCompleted?: boolean; hasTerminatedWithError?: boolean; }>; isLoading?: boolean; onScroll?: () => void; }) { mockLogViewer(props); return
; }; }); jest.mock('~/hooks/useTektonResults', () => ({ useTRTaskRunLog: jest.fn(), })); jest.mock('~/utils/pipeline-utils', () => ({ taskRunStatus: jest.fn(), })); jest.mock('react-i18next', () => ({ useTranslation: () => ({ t: (key: string) => key, }), })); const mockTaskRun: TaskRunKind = { apiVersion: 'tekton.dev/v1beta1', kind: 'TaskRun', metadata: { name: 'test-taskrun', namespace: 'test-namespace', uid: 'test-uid', }, spec: { taskRef: { name: 'test-task', }, }, status: { podName: 'test-pod', }, }; const getLastSections = (): Array<{ containerName: string; lines: string[]; isCompleted?: boolean; hasTerminatedWithError?: boolean; }> => { const lastCall = mockLogViewer.mock.calls[mockLogViewer.mock.calls.length - 1][0]; return lastCall.normalizedSections || []; }; describe('TektonTaskRunLog', () => { beforeEach(() => { jest.clearAllMocks(); (useTRTaskRunLog as jest.Mock).mockReturnValue(['pipeline log', true, undefined]); }); it('should render logs container', () => { (taskRunStatus as jest.Mock).mockReturnValue(runStatus.Running); render(); expect(screen.getByTestId('mock-log-viewer')).toBeInTheDocument(); }); it('should mark the section with hasTerminatedWithError when the task run failed', async () => { (taskRunStatus as jest.Mock).mockReturnValue(runStatus.Failed); render(); await waitFor(() => { expect(getLastSections()[0]).toEqual( expect.objectContaining({ containerName: 'test-task', lines: ['pipeline log'], isCompleted: true, hasTerminatedWithError: true, }), ); }); }); it('should not mark the section with hasTerminatedWithError when the task run succeeded', async () => { (taskRunStatus as jest.Mock).mockReturnValue(runStatus.Succeeded); render(); await waitFor(() => { expect(getLastSections()[0]).toEqual( expect.objectContaining({ containerName: 'test-task', lines: ['pipeline log'], isCompleted: true, hasTerminatedWithError: false, }), ); }); }); it('should keep the section incomplete while the task run is still running', async () => { (taskRunStatus as jest.Mock).mockReturnValue(runStatus.Running); render(); await waitFor(() => { expect(getLastSections()[0]).toEqual( expect.objectContaining({ containerName: 'test-task', lines: ['pipeline log'], isCompleted: false, hasTerminatedWithError: false, }), ); }); }); it('should pass no sections while the log is loading', () => { (useTRTaskRunLog as jest.Mock).mockReturnValue(['', false, undefined]); (taskRunStatus as jest.Mock).mockReturnValue(runStatus.Running); render(); expect(mockLogViewer).toHaveBeenCalledWith( expect.objectContaining({ normalizedSections: [], isLoading: true, }), ); }); });