Files
rxminder/hooks/__tests__/useSettings.test.ts
William Valentin 2556250f2c feat: add comprehensive test coverage and fix lint issues
- Add comprehensive tests for MailgunService (439 lines)
  * Email sending functionality with template generation
  * Configuration status validation
  * Error handling and edge cases
  * Mock setup for fetch API and FormData

- Add DatabaseService tests (451 lines)
  * Strategy pattern testing (Mock vs Production)
  * All CRUD operations for users, medications, settings
  * Legacy compatibility method testing
  * Proper TypeScript typing

- Add MockDatabaseStrategy tests (434 lines)
  * Complete coverage of mock database implementation
  * User operations, medication management
  * Settings and custom reminders functionality
  * Data persistence and error handling

- Add React hooks tests
  * useLocalStorage hook with comprehensive edge cases (340 lines)
  * useSettings hook with fetch operations and error handling (78 lines)

- Fix auth integration tests
  * Update mocking to use new database service instead of legacy couchdb.factory
  * Fix service variable references and expectations

- Simplify mailgun config tests
  * Remove redundant edge case testing
  * Focus on core functionality validation

- Fix all TypeScript and ESLint issues
  * Proper FormData mock typing
  * Correct database entity type usage
  * Remove non-existent property references

Test Results:
- 184 total tests passing
- Comprehensive coverage of core services
- Zero TypeScript compilation errors
- Full ESLint compliance
2025-09-08 10:13:50 -07:00

79 lines
2.3 KiB
TypeScript

import { renderHook, waitFor } from '@testing-library/react';
import useSettings from '../useSettings';
// Mock fetch
global.fetch = jest.fn();
describe('useSettings', () => {
beforeEach(() => {
jest.clearAllMocks();
});
test('should fetch settings on mount', async () => {
const mockSettings = { theme: 'dark', notifications: true };
(fetch as jest.MockedFunction<typeof fetch>).mockResolvedValue({
ok: true,
json: jest.fn().mockResolvedValue(mockSettings),
} as any);
const { result } = renderHook(() => useSettings());
expect(result.current.loading).toBe(true);
expect(result.current.settings).toBe(null);
await waitFor(() => {
expect(result.current.loading).toBe(false);
});
expect(result.current.settings).toEqual(mockSettings);
expect(result.current.error).toBe(null);
expect(fetch).toHaveBeenCalledWith('/api/settings');
});
test('should handle fetch errors', async () => {
const mockError = new Error('Network error');
(fetch as jest.MockedFunction<typeof fetch>).mockRejectedValue(mockError);
const { result } = renderHook(() => useSettings());
await waitFor(() => {
expect(result.current.loading).toBe(false);
});
expect(result.current.settings).toBe(null);
expect(result.current.error).toBe(mockError);
});
test('should update settings', async () => {
const initialSettings = { theme: 'light', notifications: false };
const updatedSettings = { theme: 'dark', notifications: true };
(fetch as jest.MockedFunction<typeof fetch>)
.mockResolvedValueOnce({
ok: true,
json: jest.fn().mockResolvedValue(initialSettings),
} as any)
.mockResolvedValueOnce({
ok: true,
json: jest.fn().mockResolvedValue(updatedSettings),
} as any);
const { result } = renderHook(() => useSettings());
await waitFor(() => {
expect(result.current.loading).toBe(false);
});
await result.current.updateSettings(updatedSettings);
expect(result.current.settings).toEqual(updatedSettings);
expect(fetch).toHaveBeenCalledWith('/api/settings', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(updatedSettings),
});
});
});