|
| 1 | +import { ZodError } from 'zod'; |
| 2 | + |
| 3 | +import { render, screen, userEvent } from '@/test-utils/rtl'; |
| 4 | + |
| 5 | +import { mockWorkflowDiagnosticsResult } from '@/route-handlers/diagnose-workflow/__fixtures__/mock-workflow-diagnostics-result'; |
| 6 | +import { type DiagnoseWorkflowResponse } from '@/route-handlers/diagnose-workflow/diagnose-workflow.types'; |
| 7 | + |
| 8 | +import WorkflowDiagnosticsJson from '../workflow-diagnostics-json'; |
| 9 | + |
| 10 | +const mockDownloadJson = jest.fn(); |
| 11 | +jest.mock('@/utils/download-json', () => |
| 12 | + jest.fn((json, filename) => mockDownloadJson(json, filename)) |
| 13 | +); |
| 14 | + |
| 15 | +jest.mock('@/components/copy-text-button/copy-text-button', () => |
| 16 | + jest.fn(({ textToCopy }) => ( |
| 17 | + <div data-testid="copy-text-button">{textToCopy}</div> |
| 18 | + )) |
| 19 | +); |
| 20 | + |
| 21 | +jest.mock('@/components/pretty-json/pretty-json', () => |
| 22 | + jest.fn(({ json }) => ( |
| 23 | + <div data-testid="pretty-json">{JSON.stringify(json)}</div> |
| 24 | + )) |
| 25 | +); |
| 26 | + |
| 27 | +const mockDiagnosticsResponse = { |
| 28 | + result: mockWorkflowDiagnosticsResult, |
| 29 | + parsingError: null, |
| 30 | +}; |
| 31 | + |
| 32 | +describe(WorkflowDiagnosticsJson.name, () => { |
| 33 | + beforeEach(() => { |
| 34 | + jest.clearAllMocks(); |
| 35 | + }); |
| 36 | + |
| 37 | + it('renders the PrettyJson component with diagnostics result', () => { |
| 38 | + setup({}); |
| 39 | + expect(screen.getByTestId('pretty-json')).toBeInTheDocument(); |
| 40 | + expect(screen.getByTestId('pretty-json')).toHaveTextContent( |
| 41 | + '"DiagnosticsResult"' |
| 42 | + ); |
| 43 | + }); |
| 44 | + |
| 45 | + it('renders copy text button with text to copy', () => { |
| 46 | + setup({}); |
| 47 | + const copyButton = screen.getByTestId('copy-text-button'); |
| 48 | + expect(copyButton).toBeInTheDocument(); |
| 49 | + expect(copyButton).toHaveTextContent('"DiagnosticsResult"'); |
| 50 | + }); |
| 51 | + |
| 52 | + it('downloads JSON when download button is clicked', async () => { |
| 53 | + const { user } = setup({}); |
| 54 | + const downloadButton = screen.getByTestId('download-json-button'); |
| 55 | + await user.click(downloadButton); |
| 56 | + |
| 57 | + expect(mockDownloadJson).toHaveBeenCalledWith( |
| 58 | + mockDiagnosticsResponse.result, |
| 59 | + 'diagnostics_test-workflow-id_test-run-id' |
| 60 | + ); |
| 61 | + }); |
| 62 | + |
| 63 | + it('renders with empty diagnostics result', () => { |
| 64 | + setup({ |
| 65 | + diagnosticsResponse: { |
| 66 | + result: { |
| 67 | + DiagnosticsResult: {}, |
| 68 | + DiagnosticsCompleted: true, |
| 69 | + }, |
| 70 | + parsingError: null, |
| 71 | + }, |
| 72 | + }); |
| 73 | + |
| 74 | + expect(screen.getByTestId('pretty-json')).toBeInTheDocument(); |
| 75 | + expect(screen.getByTestId('pretty-json')).toHaveTextContent( |
| 76 | + '"DiagnosticsResult"' |
| 77 | + ); |
| 78 | + }); |
| 79 | + |
| 80 | + it('renders JSON even if there is a parsing error', () => { |
| 81 | + setup({ |
| 82 | + diagnosticsResponse: { |
| 83 | + result: { |
| 84 | + DiagnosticsResult: { |
| 85 | + invalidParsedField: 'invalid-value', |
| 86 | + }, |
| 87 | + DiagnosticsCompleted: true, |
| 88 | + }, |
| 89 | + parsingError: new ZodError([]), |
| 90 | + }, |
| 91 | + }); |
| 92 | + |
| 93 | + expect(screen.getByTestId('pretty-json')).toBeInTheDocument(); |
| 94 | + expect(screen.getByTestId('pretty-json')).toHaveTextContent( |
| 95 | + '"invalid-value"' |
| 96 | + ); |
| 97 | + }); |
| 98 | +}); |
| 99 | + |
| 100 | +function setup({ |
| 101 | + diagnosticsResponse = mockDiagnosticsResponse, |
| 102 | +}: { |
| 103 | + diagnosticsResponse?: DiagnoseWorkflowResponse; |
| 104 | +}) { |
| 105 | + const user = userEvent.setup(); |
| 106 | + const defaultProps = { |
| 107 | + domain: 'test-domain', |
| 108 | + cluster: 'test-cluster', |
| 109 | + workflowId: 'test-workflow-id', |
| 110 | + runId: 'test-run-id', |
| 111 | + diagnosticsResponse, |
| 112 | + }; |
| 113 | + |
| 114 | + const view = render(<WorkflowDiagnosticsJson {...defaultProps} />); |
| 115 | + return { user, ...view }; |
| 116 | +} |
0 commit comments