From 12556de7fc77e2a33739d9db194c2c551073523f Mon Sep 17 00:00:00 2001 From: Diksha Dabhole Date: Fri, 12 Jun 2026 15:43:35 +0530 Subject: [PATCH] test(dashboard): add empty-fallback checks and tests for dashboard error boundary --- .../dashboard/error.empty-fallback.test.tsx | 79 +++++++++++++++++++ app/(root)/dashboard/error.tsx | 12 ++- 2 files changed, 87 insertions(+), 4 deletions(-) create mode 100644 app/(root)/dashboard/error.empty-fallback.test.tsx diff --git a/app/(root)/dashboard/error.empty-fallback.test.tsx b/app/(root)/dashboard/error.empty-fallback.test.tsx new file mode 100644 index 000000000..16ea3629c --- /dev/null +++ b/app/(root)/dashboard/error.empty-fallback.test.tsx @@ -0,0 +1,79 @@ +// app/(root)/dashboard/error.empty-fallback.test.tsx + +import { describe, expect, it, vi } from 'vitest'; +import { render, screen } from '@testing-library/react'; +import '@testing-library/jest-dom/vitest'; + +vi.mock('next/link', () => ({ + default: ({ + children, + ...props + }: React.AnchorHTMLAttributes & { children: React.ReactNode }) => ( + {children} + ), +})); + +import DashboardError from './error'; + +describe('Dashboard Error Page - Empty & Missing Input Fallbacks', () => { + it('renders successfully when error is null', () => { + expect(() => + render() + ).not.toThrow(); + + expect(screen.getByRole('heading', { name: 'Something went wrong' })).toBeInTheDocument(); + expect( + screen.getByText('An unexpected error occurred while fetching the dashboard data.') + ).toBeInTheDocument(); + expect(screen.getByText('⚠️')).toBeInTheDocument(); + }); + + it('renders successfully when error is undefined', () => { + expect(() => + render() + ).not.toThrow(); + + expect(screen.getByRole('heading', { name: 'Something went wrong' })).toBeInTheDocument(); + expect( + screen.getByText('An unexpected error occurred while fetching the dashboard data.') + ).toBeInTheDocument(); + }); + + it('renders successfully when error is an empty object', () => { + expect(() => + render() + ).not.toThrow(); + + expect(screen.getByRole('heading', { name: 'Something went wrong' })).toBeInTheDocument(); + expect( + screen.getByText('An unexpected error occurred while fetching the dashboard data.') + ).toBeInTheDocument(); + }); + + it('renders successfully when error has no message property', () => { + expect(() => + render() + ).not.toThrow(); + + expect(screen.getByRole('heading', { name: 'Something went wrong' })).toBeInTheDocument(); + expect( + screen.getByText('An unexpected error occurred while fetching the dashboard data.') + ).toBeInTheDocument(); + }); + + it('renders successfully when error message is an empty string', () => { + expect(() => render()).not.toThrow(); + + expect(screen.getByRole('heading', { name: 'Something went wrong' })).toBeInTheDocument(); + expect( + screen.getByText('An unexpected error occurred while fetching the dashboard data.') + ).toBeInTheDocument(); + }); + + it('renders interactive elements correctly in fallback state', () => { + render(); + + expect(screen.getByRole('button', { name: /try again/i })).toBeInTheDocument(); + expect(screen.getByRole('link', { name: /go back home/i })).toBeInTheDocument(); + }); +}); diff --git a/app/(root)/dashboard/error.tsx b/app/(root)/dashboard/error.tsx index 2da04fa9a..390cee464 100644 --- a/app/(root)/dashboard/error.tsx +++ b/app/(root)/dashboard/error.tsx @@ -11,12 +11,16 @@ export default function DashboardError({ reset: () => void; }) { useEffect(() => { - console.error(error); + if (error) { + console.error(error); + } }, [error]); - const isRateLimit = error.message.includes('API limit') || error.message.includes('rate limit'); + const errorMessage = error?.message || ''; - const isNotFound = error.message.includes('not found'); + const isRateLimit = errorMessage.includes('API limit') || errorMessage.includes('rate limit'); + + const isNotFound = errorMessage.includes('not found'); return (
@@ -38,7 +42,7 @@ export default function DashboardError({ ? "We couldn't find a GitHub user with that username. Please check the spelling and try again." : isRateLimit ? "GitHub's API rate limit has been reached. Please add a GITHUB_TOKEN to your environment variables to increase the limit, or try again later." - : error.message || 'An unexpected error occurred while fetching the dashboard data.'} + : errorMessage || 'An unexpected error occurred while fetching the dashboard data.'}