2026-02-03 08:56:11 +00:00
|
|
|
import type { Meta, StoryObj } from '@storybook/react';
|
2026-02-05 11:17:50 +00:00
|
|
|
import React from 'react';
|
2026-02-03 08:56:11 +00:00
|
|
|
import { ErrorBoundary } from './ErrorBoundary';
|
|
|
|
|
|
|
|
|
|
// Component that throws an error for testing
|
|
|
|
|
const ErrorThrower = ({ shouldThrow }: { shouldThrow?: boolean }) => {
|
|
|
|
|
if (shouldThrow) {
|
|
|
|
|
throw new Error('This is a test error for demonstrating ErrorBoundary');
|
|
|
|
|
}
|
|
|
|
|
return <div className="p-4 bg-green-100 text-green-800 rounded">Content rendered successfully!</div>;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const meta: Meta<typeof ErrorBoundary> = {
|
2026-02-05 12:21:54 +00:00
|
|
|
title: 'Docs/Failures/ErrorBoundary',
|
2026-02-03 08:56:11 +00:00
|
|
|
component: ErrorBoundary,
|
|
|
|
|
parameters: {
|
|
|
|
|
layout: 'fullscreen',
|
|
|
|
|
chromatic: { disableSnapshot: true },
|
|
|
|
|
},
|
|
|
|
|
tags: ['autodocs'],
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default meta;
|
|
|
|
|
type Story = StoryObj<typeof ErrorBoundary>;
|
|
|
|
|
|
|
|
|
|
export const NoError: Story = {
|
|
|
|
|
render: () => (
|
|
|
|
|
<ErrorBoundary>
|
|
|
|
|
<ErrorThrower shouldThrow={false} />
|
|
|
|
|
</ErrorBoundary>
|
|
|
|
|
),
|
|
|
|
|
};
|
|
|
|
|
|
2026-02-05 11:17:50 +00:00
|
|
|
// Helper to silence console.error during expected error tests
|
|
|
|
|
const ConsoleSilencer = ({ children }: { children: React.ReactNode }) => {
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
|
const originalError = console.error;
|
|
|
|
|
console.error = (...args) => {
|
|
|
|
|
const msg = args[0]?.toString() || '';
|
|
|
|
|
// Suppress the specific test error and React's error boundary noise
|
|
|
|
|
if (msg.includes('This is a test error') || msg.includes('The above error occurred')) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
originalError(...args);
|
|
|
|
|
};
|
|
|
|
|
return () => {
|
|
|
|
|
console.error = originalError;
|
|
|
|
|
};
|
|
|
|
|
}, []);
|
|
|
|
|
return <>{children}</>;
|
|
|
|
|
};
|
|
|
|
|
|
2026-02-03 08:56:11 +00:00
|
|
|
export const WithError: Story = {
|
2026-02-05 11:17:50 +00:00
|
|
|
parameters: { storybookAudit: { expectConsoleErrors: true } },
|
2026-02-03 08:56:11 +00:00
|
|
|
render: () => (
|
2026-02-05 11:17:50 +00:00
|
|
|
<ConsoleSilencer>
|
|
|
|
|
<ErrorBoundary>
|
|
|
|
|
<ErrorThrower shouldThrow={true} />
|
|
|
|
|
</ErrorBoundary>
|
|
|
|
|
</ConsoleSilencer>
|
2026-02-03 08:56:11 +00:00
|
|
|
),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const WithCustomFallback: Story = {
|
2026-02-05 11:17:50 +00:00
|
|
|
parameters: { storybookAudit: { expectConsoleErrors: true } },
|
2026-02-03 08:56:11 +00:00
|
|
|
render: () => (
|
2026-02-05 11:17:50 +00:00
|
|
|
<ConsoleSilencer>
|
|
|
|
|
<ErrorBoundary
|
|
|
|
|
fallback={
|
|
|
|
|
<div className="p-8 text-center bg-amber-100 text-amber-800 rounded-lg">
|
|
|
|
|
<h2 className="text-xl font-bold mb-2">Custom Fallback</h2>
|
|
|
|
|
<p>Something went wrong, but we have a custom fallback UI.</p>
|
|
|
|
|
</div>
|
|
|
|
|
}
|
|
|
|
|
>
|
|
|
|
|
<ErrorThrower shouldThrow={true} />
|
|
|
|
|
</ErrorBoundary>
|
|
|
|
|
</ConsoleSilencer>
|
2026-02-03 08:56:11 +00:00
|
|
|
),
|
|
|
|
|
};
|