diff --git a/app/error.empty-fallback.test.tsx b/app/error.empty-fallback.test.tsx index 1bed148da..5bb431b1e 100644 --- a/app/error.empty-fallback.test.tsx +++ b/app/error.empty-fallback.test.tsx @@ -1,8 +1,10 @@ // app/error.empty-fallback.test.tsx import { describe, expect, it, vi, beforeEach } from 'vitest'; -import { render, screen, fireEvent } from '@testing-library/react'; +import { render, screen } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; import '@testing-library/jest-dom/vitest'; +import ErrorBoundary from './error'; vi.mock('next/link', () => ({ default: ({ @@ -20,92 +22,49 @@ vi.mock('sonner', () => ({ }, })); -import ErrorBoundary from './error'; - describe('Root Error Boundary - Empty & Missing Input Fallbacks', () => { + const mockWriteText = vi.fn().mockResolvedValue(undefined); + beforeEach(() => { vi.clearAllMocks(); - // Mock clipboard API - Object.defineProperty(navigator, 'clipboard', { - value: { - writeText: vi.fn().mockResolvedValue(undefined), + vi.stubGlobal('navigator', { + clipboard: { + writeText: mockWriteText, }, - configurable: true, - writable: true, }); }); - it('renders successfully when error is null', () => { - expect(() => - render() - ).not.toThrow(); - - expect( - screen.getByRole('heading', { - name: /Looks like an exception was thrown in the application/i, - }) - ).toBeInTheDocument(); - expect(screen.getByText('Unknown runtime error occurred.')).toBeInTheDocument(); - }); - - it('renders successfully when error is undefined', () => { - expect(() => - render() - ).not.toThrow(); - - expect( - screen.getByRole('heading', { - name: /Looks like an exception was thrown in the application/i, - }) - ).toBeInTheDocument(); - expect(screen.getByText('Unknown runtime error occurred.')).toBeInTheDocument(); - }); - - it('renders successfully when error is an empty object', () => { - expect(() => - render() - ).not.toThrow(); - - expect( - screen.getByRole('heading', { - name: /Looks like an exception was thrown in the application/i, - }) - ).toBeInTheDocument(); - expect(screen.getByText('Unknown runtime error occurred.')).toBeInTheDocument(); - }); - - it('renders successfully when error has no message property', () => { - expect(() => - render() - ).not.toThrow(); - - expect( - screen.getByRole('heading', { - name: /Looks like an exception was thrown in the application/i, - }) - ).toBeInTheDocument(); - expect(screen.getByText('Unknown runtime error occurred.')).toBeInTheDocument(); - }); + const emptyErrorCases = [ + { description: 'null', error: null as unknown as Error }, + { description: 'undefined', error: undefined as unknown as Error }, + { description: 'an empty object', error: {} as unknown as Error }, + { description: 'missing message property', error: { name: 'CustomError' } as unknown as Error }, + { description: 'an empty string message', error: new Error('') }, + ]; - it('renders successfully when error message is an empty string', () => { - expect(() => render()).not.toThrow(); + it.each(emptyErrorCases)('renders successfully when error is $description', ({ error }) => { + expect(() => render()).not.toThrow(); expect( screen.getByRole('heading', { name: /Looks like an exception was thrown in the application/i, }) ).toBeInTheDocument(); + expect(screen.getByText('Unknown runtime error occurred.')).toBeInTheDocument(); }); it('renders interactive elements and triggers actions correctly in fallback state', async () => { + const user = userEvent.setup(); // Initialize user-event const resetMock = vi.fn(); + render(); // Check actions const retryButton = screen.getByRole('button', { name: /git fetch/i }); expect(retryButton).toBeInTheDocument(); - fireEvent.click(retryButton); + + await user.click(retryButton); expect(resetMock).toHaveBeenCalledOnce(); const homeLink = screen.getByRole('link', { name: /Return to main/i }); @@ -113,12 +72,13 @@ describe('Root Error Boundary - Empty & Missing Input Fallbacks', () => { // Check clipboard copy fallback behavior const terminalContainer = screen.getByText('commitpulse — error').closest('div'); - expect(terminalContainer).toBeInTheDocument(); - if (terminalContainer) { - fireEvent.click(terminalContainer); - expect(navigator.clipboard.writeText).toHaveBeenCalledWith( - expect.stringContaining('Unknown exception in the render tree.') - ); - } + + expect(terminalContainer).not.toBeNull(); + + await user.click(terminalContainer!); + + expect(mockWriteText).toHaveBeenCalledWith( + expect.stringContaining('Unknown exception in the render tree.') + ); }); });