WellNuo/__tests__/screens/equipment.test.tsx
Sergei 88fc9042a7 Add retry button to error states in equipment and subscription screens
- Added error state with retry functionality to equipment.tsx
  - Display error message when sensor loading fails
  - Provide "Try Again" button to retry loading
  - Clear error on successful retry

- Added error state with retry functionality to subscription.tsx
  - Display error message when beneficiary loading fails
  - Provide "Try Again" button with icon to retry loading
  - Show offline icon and proper error layout

- Added comprehensive tests for error handling
  - ErrorMessage component tests for inline errors
  - FullScreenError component tests
  - Equipment screen error state tests
  - Subscription screen error state tests

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2026-01-29 11:27:19 -08:00

92 lines
2.8 KiB
TypeScript

import React from 'react';
import { render, fireEvent, waitFor } from '@testing-library/react-native';
import { api } from '@/services/api';
// Mock dependencies
jest.mock('@/services/api');
jest.mock('expo-router', () => ({
useLocalSearchParams: () => ({ id: '1' }),
router: { back: jest.fn(), push: jest.fn() },
}));
jest.mock('@/contexts/BeneficiaryContext', () => ({
useBeneficiary: () => ({
currentBeneficiary: { id: 1, name: 'Test User' },
}),
}));
jest.mock('@/contexts/BLEContext', () => ({
useBLE: () => ({
isBLEAvailable: true,
}),
}));
// Import the screen after mocks are set up
import EquipmentScreen from '@/app/(tabs)/beneficiaries/[id]/equipment';
describe('EquipmentScreen - Error Handling', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('displays error message when API call fails', async () => {
const mockError = { ok: false, error: { message: 'Network error' } };
(api.getDevicesForBeneficiary as jest.Mock).mockResolvedValue(mockError);
const { getByText, findByText } = render(<EquipmentScreen />);
await waitFor(() => {
expect(findByText('Network error')).toBeTruthy();
});
});
it('displays retry button when error occurs', async () => {
const mockError = { ok: false, error: { message: 'Failed to load sensors' } };
(api.getDevicesForBeneficiary as jest.Mock).mockResolvedValue(mockError);
const { findByText } = render(<EquipmentScreen />);
await waitFor(() => {
expect(findByText('Try Again')).toBeTruthy();
});
});
it('retries loading sensors when retry button is pressed', async () => {
const mockError = { ok: false, error: { message: 'Failed to load sensors' } };
const mockSuccess = { ok: true, data: [] };
(api.getDevicesForBeneficiary as jest.Mock)
.mockResolvedValueOnce(mockError)
.mockResolvedValueOnce(mockSuccess);
const { findByText, queryByText } = render(<EquipmentScreen />);
// Wait for error to appear
await waitFor(() => {
expect(findByText('Try Again')).toBeTruthy();
});
// Press retry button
const retryButton = await findByText('Try Again');
fireEvent.press(retryButton);
// Wait for error to disappear after successful retry
await waitFor(() => {
expect(queryByText('Failed to load sensors')).toBeNull();
});
// Verify API was called twice
expect(api.getDevicesForBeneficiary).toHaveBeenCalledTimes(2);
});
it('displays generic error message for unknown errors', async () => {
(api.getDevicesForBeneficiary as jest.Mock).mockRejectedValue(
new Error('Unknown error')
);
const { findByText } = render(<EquipmentScreen />);
await waitFor(() => {
expect(findByText(/Unknown error|Failed to load sensors/)).toBeTruthy();
});
});
});