Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions app/(root)/dashboard/error.empty-fallback.test.tsx
Original file line number Diff line number Diff line change
@@ -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<HTMLAnchorElement> & { children: React.ReactNode }) => (
<a {...props}>{children}</a>
),
}));

import DashboardError from './error';

describe('Dashboard Error Page - Empty & Missing Input Fallbacks', () => {
it('renders successfully when error is null', () => {
expect(() =>
render(<DashboardError error={null as unknown as Error} reset={vi.fn()} />)
).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(<DashboardError error={undefined as unknown as Error} reset={vi.fn()} />)
).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(<DashboardError error={{} as unknown as Error} reset={vi.fn()} />)
).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(<DashboardError error={{ name: 'CustomError' } as unknown as Error} reset={vi.fn()} />)
).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(<DashboardError error={new Error('')} reset={vi.fn()} />)).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(<DashboardError error={{} as unknown as Error} reset={vi.fn()} />);

expect(screen.getByRole('button', { name: /try again/i })).toBeInTheDocument();
expect(screen.getByRole('link', { name: /go back home/i })).toBeInTheDocument();
});
});
12 changes: 8 additions & 4 deletions app/(root)/dashboard/error.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<div className="min-h-[80vh] flex flex-col items-center justify-center p-6 text-center">
Expand All @@ -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.'}
</p>

<div className="flex flex-col gap-3">
Expand Down
Loading