|
1 | 1 | import React from 'react'
|
2 |
| -import { render, screen, fireEvent, waitFor } from '@testing-library/react' |
| 2 | +import { render, screen } from '@testing-library/react' |
3 | 3 | import { LogView } from './log-view'
|
4 |
| -import { getWorkflowLogs } from '@/lib/github' |
5 | 4 | import { vi, describe, it, expect, beforeEach } from 'vitest'
|
6 |
| -import { SWRConfig } from 'swr' |
7 | 5 |
|
8 |
| -vi.mock('@/lib/github', () => ({ |
9 |
| - getWorkflowLogs: vi.fn(), |
10 |
| -})) |
11 |
| - |
12 |
| -const mockLogs = ` |
13 |
| -2023-05-01T12:00:00.000Z File: test/file1.txt |
14 |
| -Line 1 of file 1 |
15 |
| -Line 2 of file 1 |
16 |
| -2023-05-01T12:01:00.000Z File: test/file2.txt |
17 |
| -Line 1 of file 2 |
18 |
| -Line 2 of file 2 |
19 |
| -2023-05-01T12:02:00.000Z Some other log |
20 |
| -` |
| 6 | +const mockParsedLogs = [ |
| 7 | + { |
| 8 | + id: 'group-0', |
| 9 | + name: 'test', |
| 10 | + logs: [ |
| 11 | + '1 | Line 1 of file 1', |
| 12 | + '2 | Line 2 of file 1' |
| 13 | + ] |
| 14 | + }, |
| 15 | + { |
| 16 | + id: 'group-1', |
| 17 | + name: 'test', |
| 18 | + logs: [ |
| 19 | + '1 | Line 1 of file 2', |
| 20 | + '2 | Line 2 of file 2' |
| 21 | + ] |
| 22 | + }, |
| 23 | + { |
| 24 | + id: 'group-2', |
| 25 | + name: 'Other', |
| 26 | + logs: [ |
| 27 | + '1 | Some other log' |
| 28 | + ] |
| 29 | + } |
| 30 | +] |
21 | 31 |
|
22 | 32 | describe('LogView', () => {
|
23 | 33 | const defaultProps = {
|
24 |
| - owner: 'testOwner', |
25 |
| - repo: 'testRepo', |
26 |
| - runId: '123', |
| 34 | + parsedLogs: mockParsedLogs, |
| 35 | + error: undefined, |
| 36 | + isLoading: false, |
27 | 37 | }
|
28 | 38 |
|
29 | 39 | beforeEach(() => {
|
30 | 40 | vi.clearAllMocks()
|
31 | 41 | })
|
32 | 42 |
|
33 | 43 | it('renders loading state', () => {
|
34 |
| - render(<LogView {...defaultProps} />) |
| 44 | + render(<LogView {...defaultProps} isLoading={true} />) |
35 | 45 | expect(screen.getByText('Loading logs...')).toBeInTheDocument()
|
36 | 46 | })
|
37 | 47 |
|
38 |
| - it('renders error state', async () => { |
39 |
| - vi.mocked(getWorkflowLogs).mockRejectedValue(new Error('Test error')) |
40 |
| - |
41 |
| - render( |
42 |
| - <SWRConfig value={{ provider: () => new Map() }}> |
43 |
| - <LogView {...defaultProps} /> |
44 |
| - </SWRConfig> |
45 |
| - ) |
46 |
| - |
47 |
| - await waitFor(() => { |
48 |
| - expect(screen.getByText('Error loading logs: Test error')).toBeInTheDocument() |
49 |
| - }) |
| 48 | + it('renders error state', () => { |
| 49 | + render(<LogView {...defaultProps} error={new Error('Test error')} />) |
| 50 | + expect(screen.getByText('Error loading logs: Test error')).toBeInTheDocument() |
50 | 51 | })
|
51 | 52 |
|
52 |
| - it('renders logs and groups correctly', async () => { |
53 |
| - vi.mocked(getWorkflowLogs).mockResolvedValue(mockLogs) |
54 |
| - |
55 |
| - render( |
56 |
| - <SWRConfig value={{ provider: () => new Map() }}> |
57 |
| - <LogView {...defaultProps} /> |
58 |
| - </SWRConfig> |
59 |
| - ) |
60 |
| - |
61 |
| - await waitFor(() => { |
62 |
| - const testElements = screen.getAllByText('test') |
63 |
| - expect(testElements.length).toBeGreaterThan(0) |
64 |
| - expect(screen.getByText('Other')).toBeInTheDocument() |
65 |
| - }) |
66 |
| - }) |
67 |
| - |
68 |
| - it('expands and collapses log groups', async () => { |
69 |
| - vi.mocked(getWorkflowLogs).mockResolvedValue(mockLogs) |
70 |
| - |
71 |
| - render( |
72 |
| - <SWRConfig value={{ provider: () => new Map() }}> |
73 |
| - <LogView {...defaultProps} /> |
74 |
| - </SWRConfig> |
75 |
| - ) |
76 |
| - |
77 |
| - await waitFor(() => { |
78 |
| - const testElements = screen.getAllByText('test') |
79 |
| - expect(testElements.length).toBeGreaterThan(0) |
80 |
| - }) |
81 |
| - |
82 |
| - // Expand the first group |
83 |
| - const testButtons = screen.getAllByText('test') |
84 |
| - fireEvent.click(testButtons[0]) |
85 |
| - |
86 |
| - expect(screen.getByText(/1 \| Line 1 of file 1/)).toBeInTheDocument() |
87 |
| - expect(screen.getByText(/2 \| Line 2 of file 1/)).toBeInTheDocument() |
88 |
| - |
89 |
| - // Collapse the first group |
90 |
| - fireEvent.click(testButtons[0]) |
| 53 | + it('renders logs and groups correctly', () => { |
| 54 | + render(<LogView {...defaultProps} />) |
91 | 55 |
|
92 |
| - expect(screen.queryByText(/1 \| Line 1 of file 1/)).not.toBeInTheDocument() |
93 |
| - expect(screen.queryByText(/2 \| Line 2 of file 1/)).not.toBeInTheDocument() |
| 56 | + expect(screen.getAllByText('test').length).toBe(2) |
| 57 | + expect(screen.getByText('Other')).toBeInTheDocument() |
94 | 58 | })
|
95 | 59 |
|
96 |
| - it('does not fetch logs when runId is null', () => { |
97 |
| - render(<LogView owner="testOwner" repo="testRepo" runId={null} />) |
98 |
| - expect(getWorkflowLogs).not.toHaveBeenCalled() |
| 60 | + it('renders empty state when no logs are provided', () => { |
| 61 | + render(<LogView parsedLogs={[]} error={undefined} isLoading={false} />) |
| 62 | + expect(screen.getByText(/No logs available/i)).toBeInTheDocument() |
99 | 63 | })
|
100 | 64 | })
|
0 commit comments