Skip to content

Commit

Permalink
add unit tests for log-view component
Browse files Browse the repository at this point in the history
  • Loading branch information
m2rads committed Oct 14, 2024
1 parent 2a0efb2 commit 57191ad
Showing 1 changed file with 98 additions and 0 deletions.
98 changes: 98 additions & 0 deletions app/(dashboard)/dashboard/log-view.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import React from 'react'
import { render, screen, fireEvent, waitFor } from '@testing-library/react'
import { LogView } from './log-view'
import { getWorkflowLogs } from '@/lib/github'
import { vi, describe, it, expect, beforeEach } from 'vitest'
import { SWRConfig } from 'swr'

vi.mock('@/lib/github', () => ({
getWorkflowLogs: vi.fn(),
}))

const mockLogs = `
2023-05-01T12:00:00.000Z File: test/file1.txt
Line 1 of file 1
Line 2 of file 1
2023-05-01T12:01:00.000Z File: test/file2.txt
Line 1 of file 2
Line 2 of file 2
2023-05-01T12:02:00.000Z Some other log
`

describe('LogView', () => {
const defaultProps = {
owner: 'testOwner',
repo: 'testRepo',
runId: '123',
}

beforeEach(() => {
vi.clearAllMocks()
})

it('renders loading state', () => {
render(<LogView {...defaultProps} />)
expect(screen.getByText('Loading logs...')).toBeInTheDocument()
})

it('renders error state', async () => {
vi.mocked(getWorkflowLogs).mockRejectedValue(new Error('Test error'))

render(
<SWRConfig value={{ provider: () => new Map() }}>
<LogView {...defaultProps} />
</SWRConfig>
)

await waitFor(() => {
expect(screen.getByText('Error loading logs: Test error')).toBeInTheDocument()
})
})

it('renders logs and groups correctly', async () => {
vi.mocked(getWorkflowLogs).mockResolvedValue(mockLogs)

render(
<SWRConfig value={{ provider: () => new Map() }}>
<LogView {...defaultProps} />
</SWRConfig>
)

await waitFor(() => {

Check failure on line 61 in app/(dashboard)/dashboard/log-view.test.tsx

View workflow job for this annotation

GitHub Actions / test

app/(dashboard)/dashboard/log-view.test.tsx > LogView > renders logs and groups correctly

TestingLibraryElementError: Unable to find an element with the text: file1. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible. Ignored nodes: comments, script, style <body> <div> <div class="bg-gray-900 text-gray-100 rounded-lg overflow-hidden" > <div class="flex items-center justify-between p-2 bg-gray-800" > <h3 class="text-sm font-semibold" > Logs </h3> </div> <div class="h-96 overflow-y-auto p-4 font-mono text-sm" > <div class="mb-4" > <button class="flex items-center text-left w-full py-2 px-4 bg-gray-800 hover:bg-gray-700 rounded" > <svg class="lucide lucide-chevron-right w-4 h-4 mr-2" fill="none" height="24" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg" > <path d="m9 18 6-6-6-6" /> </svg> <span class="font-semibold" > Other </span> </button> </div> <div class="mb-4" > <button class="flex items-center text-left w-full py-2 px-4 bg-gray-800 hover:bg-gray-700 rounded" > <svg class="lucide lucide-chevron-right w-4 h-4 mr-2" fill="none" height="24" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg" > <path d="m9 18 6-6-6-6" /> </svg> <span class="font-semibold" > test </span> </button> </div> <div class="mb-4" > <button class="flex items-center text-left w-full py-2 px-4 bg-gray-800 hover:bg-gray-700 rounded" > <svg class="lucide lucide-chevron-right w-4 h-4 mr-2" fill="none" height="24" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg" > <path d="m9 18 6-6-6-6" /> </svg> <span class="font-semibold" > test </span> </button> </div> </div> </div> </div> </body> Ignored nodes: comments, script, style <html> <head /> <body> <div> <div class="bg-gray-900 text-gray-100 rounded-lg overflow-hidden" > <div class="flex items-center justify-between p-2 bg-gray-800" > <h3 class="text-sm font-semibold" > Logs </h3> </div> <div class="h-96 overflow-y-auto p-4 font-mono text-sm" > <div class="mb-4" > <button class="flex items-center text-left w-full py-2 px-4 bg-gray-800 hover:bg-gray-700 rounded" > <svg class="lucide lucide-chevron-right w-4 h-4 mr-2" fill="none" height="24" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" viewBox="0 0 24 24"
expect(screen.getByText('file1')).toBeInTheDocument()
expect(screen.getByText('file2')).toBeInTheDocument()
expect(screen.getByText('Other')).toBeInTheDocument()
})
})

it('expands and collapses log groups', async () => {
vi.mocked(getWorkflowLogs).mockResolvedValue(mockLogs)

render(
<SWRConfig value={{ provider: () => new Map() }}>
<LogView {...defaultProps} />
</SWRConfig>
)

await waitFor(() => {

Check failure on line 77 in app/(dashboard)/dashboard/log-view.test.tsx

View workflow job for this annotation

GitHub Actions / test

app/(dashboard)/dashboard/log-view.test.tsx > LogView > expands and collapses log groups

TestingLibraryElementError: Unable to find an element with the text: file1. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible. Ignored nodes: comments, script, style <body> <div> <div class="bg-gray-900 text-gray-100 rounded-lg overflow-hidden" > <div class="flex items-center justify-between p-2 bg-gray-800" > <h3 class="text-sm font-semibold" > Logs </h3> </div> <div class="h-96 overflow-y-auto p-4 font-mono text-sm" > <div class="mb-4" > <button class="flex items-center text-left w-full py-2 px-4 bg-gray-800 hover:bg-gray-700 rounded" > <svg class="lucide lucide-chevron-right w-4 h-4 mr-2" fill="none" height="24" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg" > <path d="m9 18 6-6-6-6" /> </svg> <span class="font-semibold" > Other </span> </button> </div> <div class="mb-4" > <button class="flex items-center text-left w-full py-2 px-4 bg-gray-800 hover:bg-gray-700 rounded" > <svg class="lucide lucide-chevron-right w-4 h-4 mr-2" fill="none" height="24" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg" > <path d="m9 18 6-6-6-6" /> </svg> <span class="font-semibold" > test </span> </button> </div> <div class="mb-4" > <button class="flex items-center text-left w-full py-2 px-4 bg-gray-800 hover:bg-gray-700 rounded" > <svg class="lucide lucide-chevron-right w-4 h-4 mr-2" fill="none" height="24" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg" > <path d="m9 18 6-6-6-6" /> </svg> <span class="font-semibold" > test </span> </button> </div> </div> </div> </div> </body> Ignored nodes: comments, script, style <html> <head /> <body> <div> <div class="bg-gray-900 text-gray-100 rounded-lg overflow-hidden" > <div class="flex items-center justify-between p-2 bg-gray-800" > <h3 class="text-sm font-semibold" > Logs </h3> </div> <div class="h-96 overflow-y-auto p-4 font-mono text-sm" > <div class="mb-4" > <button class="flex items-center text-left w-full py-2 px-4 bg-gray-800 hover:bg-gray-700 rounded" > <svg class="lucide lucide-chevron-right w-4 h-4 mr-2" fill="none" height="24" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" viewBox="0 0 24 24"
expect(screen.getByText('file1')).toBeInTheDocument()
})

// Expand the first group
fireEvent.click(screen.getByText('file1'))

expect(screen.getByText('1 | Line 1 of file 1')).toBeInTheDocument()
expect(screen.getByText('2 | Line 2 of file 1')).toBeInTheDocument()

// Collapse the first group
fireEvent.click(screen.getByText('file1'))

expect(screen.queryByText('1 | Line 1 of file 1')).not.toBeInTheDocument()
expect(screen.queryByText('2 | Line 2 of file 1')).not.toBeInTheDocument()
})

it('does not fetch logs when runId is null', () => {
render(<LogView owner="testOwner" repo="testRepo" runId={null} />)
expect(getWorkflowLogs).not.toHaveBeenCalled()
})
})

0 comments on commit 57191ad

Please sign in to comment.