import AsyncStorage from '@react-native-async-storage/async-storage'; import { saveUserSettings, loadUserSettings } from '../AsyncStorage'; // Mock de AsyncStorage jest.mock('@react-native-async-storage/async-storage', () => ({ setItem: jest.fn(), getItem: jest.fn(), })); describe('UserSettings functions', () => { afterEach(() => { jest.clearAllMocks(); }); describe('saveUserSettings', () => { test('should save data to AsyncStorage', async () => { const key = 'theme'; const value = 'dark'; await saveUserSettings(key, value); expect(AsyncStorage.setItem).toHaveBeenCalledWith(key, value); }); test('should throw an error if AsyncStorage.setItem fails', async () => { AsyncStorage.setItem.mockRejectedValueOnce(new Error('AsyncStorage error')); await expect(saveUserSettings('theme', 'dark')).rejects.toThrow( 'Failed to load the data from AsyncStorage' ); }); }); describe('loadUserSettings', () => { test('should load data from AsyncStorage', async () => { const key = 'theme'; const value = 'dark'; AsyncStorage.getItem.mockResolvedValueOnce(value); const result = await loadUserSettings(key); expect(result).toBe(value); expect(AsyncStorage.getItem).toHaveBeenCalledWith(key); }); test('should return null if the value does not exist in AsyncStorage', async () => { AsyncStorage.getItem.mockResolvedValueOnce(null); const result = await loadUserSettings('nonexistent_key'); expect(result).toBeUndefined(); }); test('should throw an error if AsyncStorage.getItem fails', async () => { AsyncStorage.getItem.mockRejectedValueOnce(new Error('AsyncStorage error')); await expect(loadUserSettings('theme')).rejects.toThrow( 'Failed to load the data from AsyncStorage' ); }); }); });