Files
PriceUpdaterAppv2/tests/tui/components/common/ErrorBoundary.test.js

195 lines
5.2 KiB
JavaScript

const React = require("react");
const ErrorBoundary = require("../../../../src/tui/components/common/ErrorBoundary");
// Mock component that throws an error
const ThrowError = ({ shouldThrow = false, message = "Test error" }) => {
if (shouldThrow) {
throw new Error(message);
}
return React.createElement("div", {}, "No error");
};
describe("ErrorBoundary Component", () => {
// Suppress console.error for these tests
beforeEach(() => {
jest.spyOn(console, "error").mockImplementation(() => {});
});
afterEach(() => {
jest.restoreAllMocks();
});
test("component can be created with default props", () => {
const component = React.createElement(
ErrorBoundary,
{},
React.createElement("div", {}, "Child content")
);
expect(component).toBeDefined();
expect(component.type).toBe(ErrorBoundary);
});
test("component renders children when no error occurs", () => {
const childContent = React.createElement("div", {}, "Test content");
const component = React.createElement(ErrorBoundary, {}, childContent);
expect(component.props.children).toBe(childContent);
});
test("component accepts onError callback", () => {
const mockOnError = jest.fn();
const component = React.createElement(
ErrorBoundary,
{ onError: mockOnError },
React.createElement("div", {}, "Child")
);
expect(component.props.onError).toBe(mockOnError);
});
test("component accepts onRetry callback", () => {
const mockOnRetry = jest.fn();
const component = React.createElement(
ErrorBoundary,
{ onRetry: mockOnRetry },
React.createElement("div", {}, "Child")
);
expect(component.props.onRetry).toBe(mockOnRetry);
});
test("component accepts onReset callback", () => {
const mockOnReset = jest.fn();
const component = React.createElement(
ErrorBoundary,
{ onReset: mockOnReset },
React.createElement("div", {}, "Child")
);
expect(component.props.onReset).toBe(mockOnReset);
});
test("component accepts onExit callback", () => {
const mockOnExit = jest.fn();
const component = React.createElement(
ErrorBoundary,
{ onExit: mockOnExit },
React.createElement("div", {}, "Child")
);
expect(component.props.onExit).toBe(mockOnExit);
});
test("component accepts maxRetries prop", () => {
const component = React.createElement(
ErrorBoundary,
{ maxRetries: 5 },
React.createElement("div", {}, "Child")
);
expect(component.props.maxRetries).toBe(5);
});
test("component accepts showDetails prop", () => {
const component = React.createElement(
ErrorBoundary,
{ showDetails: false },
React.createElement("div", {}, "Child")
);
expect(component.props.showDetails).toBe(false);
});
test("component accepts title prop", () => {
const component = React.createElement(
ErrorBoundary,
{ title: "Custom Error Title" },
React.createElement("div", {}, "Child")
);
expect(component.props.title).toBe("Custom Error Title");
});
test("component accepts custom fallback function", () => {
const mockFallback = jest.fn(() =>
React.createElement("div", {}, "Custom error")
);
const component = React.createElement(
ErrorBoundary,
{ fallback: mockFallback },
React.createElement("div", {}, "Child")
);
expect(component.props.fallback).toBe(mockFallback);
});
test("component accepts all expected props", () => {
const fullProps = {
onError: jest.fn(),
onRetry: jest.fn(),
onReset: jest.fn(),
onExit: jest.fn(),
maxRetries: 3,
showDetails: true,
title: "Test Error",
fallback: jest.fn(),
};
const component = React.createElement(
ErrorBoundary,
fullProps,
React.createElement("div", {}, "Child")
);
expect(component).toBeDefined();
expect(component.props).toMatchObject(fullProps);
});
test("component is a class component", () => {
expect(typeof ErrorBoundary).toBe("function");
expect(ErrorBoundary.prototype.render).toBeDefined();
expect(ErrorBoundary.prototype.componentDidCatch).toBeDefined();
});
test("component has getDerivedStateFromError static method", () => {
expect(typeof ErrorBoundary.getDerivedStateFromError).toBe("function");
});
test("getDerivedStateFromError returns correct state", () => {
const error = new Error("Test error");
const newState = ErrorBoundary.getDerivedStateFromError(error);
expect(newState).toEqual({ hasError: true });
});
test("component handles multiple children", () => {
const child1 = React.createElement("div", {}, "Child 1");
const child2 = React.createElement("div", {}, "Child 2");
const component = React.createElement(ErrorBoundary, {}, child1, child2);
expect(component.props.children).toEqual([child1, child2]);
});
test("component has correct default behavior", () => {
const component = React.createElement(
ErrorBoundary,
{},
React.createElement("div", {}, "Test")
);
// Check that component can be created without required props
expect(component).toBeDefined();
expect(component.type).toBe(ErrorBoundary);
});
test("component type is correct", () => {
const component = React.createElement(
ErrorBoundary,
{},
React.createElement("div", {}, "Child")
);
expect(typeof ErrorBoundary).toBe("function");
expect(component.type).toBe(ErrorBoundary);
});
});