boxons/components/shared/business/__tests__/AsyncStorage.test.ts

67 lines
2.0 KiB
TypeScript
Raw Permalink Normal View History

import AsyncStorage from '@react-native-async-storage/async-storage';
import { saveUserSettings, loadUserSettings } from '../AsyncStorage';
jest.mock('@react-native-async-storage/async-storage', () => ({
setItem: jest.fn(),
getItem: jest.fn(),
}));
2025-04-08 21:29:14 +00:00
const mockedSetItem = AsyncStorage.setItem as jest.MockedFunction<typeof AsyncStorage.setItem>;
const mockedGetItem = AsyncStorage.getItem as jest.MockedFunction<typeof AsyncStorage.getItem>;
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 () => {
2025-04-08 21:29:14 +00:00
mockedSetItem.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';
2025-04-08 21:29:14 +00:00
mockedGetItem.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 () => {
2025-04-08 21:29:14 +00:00
mockedGetItem.mockResolvedValueOnce(null);
const result = await loadUserSettings('nonexistent_key');
expect(result).toBeUndefined();
});
test('should throw an error if AsyncStorage.getItem fails', async () => {
2025-04-08 21:29:14 +00:00
mockedGetItem.mockRejectedValueOnce(new Error('AsyncStorage error'));
await expect(loadUserSettings('theme')).rejects.toThrow(
'Failed to load the data from AsyncStorage'
);
});
});
});