import type { Meta, StoryObj } from '@storybook/react';
import React from 'react';
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
Content rendered successfully!
;
};
const meta: Meta = {
title: 'Docs/Failures/ErrorBoundary',
component: ErrorBoundary,
parameters: {
layout: 'fullscreen',
chromatic: { disableSnapshot: true },
},
tags: ['autodocs'],
};
export default meta;
type Story = StoryObj;
export const NoError: Story = {
render: () => (
),
};
// 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}>;
};
export const WithError: Story = {
parameters: { storybookAudit: { expectConsoleErrors: true } },
render: () => (
),
};
export const WithCustomFallback: Story = {
parameters: { storybookAudit: { expectConsoleErrors: true } },
render: () => (
Custom Fallback
Something went wrong, but we have a custom fallback UI.
}
>
),
};