|
| 1 | +import React from 'react'; |
| 2 | +import { render, fireEvent } from '@testing-library/react-native'; |
| 3 | + |
| 4 | +jest.mock('@react-native-async-storage/async-storage', () => ({ |
| 5 | + getItem: jest.fn(async () => null), |
| 6 | + setItem: jest.fn(async () => {}), |
| 7 | +})); |
| 8 | + |
| 9 | +jest.mock('lucide-react-native', () => ({ |
| 10 | + AlertTriangle: () => null, |
| 11 | +})); |
| 12 | + |
| 13 | +import { ErrorState } from '../src/components/ErrorState'; |
| 14 | + |
| 15 | +describe('ErrorState', () => { |
| 16 | + it('renders with the default title and alert role', () => { |
| 17 | + const { getByTestId } = render(<ErrorState />); |
| 18 | + |
| 19 | + const node = getByTestId('error-state'); |
| 20 | + expect(node.props.accessibilityRole).toBe('alert'); |
| 21 | + expect(node.props.accessibilityLabel).toBe('Something went wrong'); |
| 22 | + }); |
| 23 | + |
| 24 | + it('renders a custom title and message', () => { |
| 25 | + const { getByText } = render( |
| 26 | + <ErrorState title="Could not load data" message="Pull down to try again." /> |
| 27 | + ); |
| 28 | + |
| 29 | + expect(getByText('Could not load data')).toBeTruthy(); |
| 30 | + expect(getByText('Pull down to try again.')).toBeTruthy(); |
| 31 | + }); |
| 32 | + |
| 33 | + it('does not render a message when none is provided', () => { |
| 34 | + const { queryByText } = render(<ErrorState title="Failed" />); |
| 35 | + |
| 36 | + expect(queryByText('Pull down to try again.')).toBeNull(); |
| 37 | + }); |
| 38 | + |
| 39 | + it('renders the action button and fires onPress', () => { |
| 40 | + const onPress = jest.fn(); |
| 41 | + const { getByText } = render( |
| 42 | + <ErrorState |
| 43 | + title="Error" |
| 44 | + action={{ label: 'Retry', onPress }} |
| 45 | + /> |
| 46 | + ); |
| 47 | + |
| 48 | + fireEvent.press(getByText('Retry')); |
| 49 | + expect(onPress).toHaveBeenCalledTimes(1); |
| 50 | + }); |
| 51 | + |
| 52 | + it('accepts a caller-supplied testID', () => { |
| 53 | + const { getByTestId } = render( |
| 54 | + <ErrorState testID="recent-activity-error" /> |
| 55 | + ); |
| 56 | + |
| 57 | + expect(getByTestId('recent-activity-error')).toBeTruthy(); |
| 58 | + }); |
| 59 | +}); |
0 commit comments