diff --git a/components/ErrorState.test.tsx b/components/ErrorState.test.tsx new file mode 100644 index 0000000..dc8c503 --- /dev/null +++ b/components/ErrorState.test.tsx @@ -0,0 +1,44 @@ +import React from 'react' +import { render, fireEvent } from '@testing-library/react-native' +import ErrorState from './ErrorState' + +describe('ErrorState Component', () => { + describe('Basic Rendering', () => { + it('should render error message', () => { + const { getByText } = render() + expect(getByText('Something went wrong')).toBeTruthy() + }) + + it('should render with default message when not provided', () => { + const { getByText } = render() + expect(getByText('An error occurred')).toBeTruthy() + }) + }) + + describe('Retry Functionality', () => { + it('should render retry button when onRetry is provided', () => { + const onRetry = jest.fn() + const { getByText } = render() + expect(getByText('Retry')).toBeTruthy() + }) + + it('should not render retry button when onRetry is not provided', () => { + const { queryByText } = render() + expect(queryByText('Retry')).toBeNull() + }) + + it('should call onRetry when retry button is pressed', () => { + const onRetry = jest.fn() + const { getByText } = render() + fireEvent.press(getByText('Retry')) + expect(onRetry).toHaveBeenCalledTimes(1) + }) + }) + + describe('Props', () => { + it('should accept testID prop', () => { + const { getByTestId } = render() + expect(getByTestId('error-test')).toBeTruthy() + }) + }) +}) diff --git a/components/ErrorState.tsx b/components/ErrorState.tsx new file mode 100644 index 0000000..e032432 --- /dev/null +++ b/components/ErrorState.tsx @@ -0,0 +1,21 @@ +import React from 'react' +import { View, TouchableOpacity, ViewProps } from 'react-native' +import Text from './ui/Text' + +interface ErrorStateProps extends ViewProps { + message?: string + onRetry?: () => void +} + +export default function ErrorState({ message = 'An error occurred', onRetry, testID, ...props }: ErrorStateProps) { + return ( + + {message} + {onRetry && ( + + Retry + + )} + + ) +}