test: add error handling tests for storage helpers

Cover localStorage error catch blocks to improve coverage.
helpers.ts coverage: 93.93% (lines)

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
soufiane 2025-11-27 14:26:58 +01:00
parent e6769d507f
commit e488fa3299

View File

@ -74,6 +74,16 @@ describe('Storage Helpers', () => {
it('should return null for non-existent key', () => { it('should return null for non-existent key', () => {
expect(storage.get('non-existent')).toBeNull(); expect(storage.get('non-existent')).toBeNull();
}); });
it('should return null and log error when localStorage throws', () => {
const consoleSpy = jest.spyOn(console, 'error').mockImplementation();
localStorageMock.getItem.mockImplementationOnce(() => {
throw new Error('Storage error');
});
expect(storage.get('test-key')).toBeNull();
expect(consoleSpy).toHaveBeenCalled();
consoleSpy.mockRestore();
});
}); });
describe('storage.set', () => { describe('storage.set', () => {
@ -81,6 +91,16 @@ describe('Storage Helpers', () => {
storage.set('test-key', 'test-value'); storage.set('test-key', 'test-value');
expect(localStorageMock.setItem).toHaveBeenCalledWith('test-key', 'test-value'); expect(localStorageMock.setItem).toHaveBeenCalledWith('test-key', 'test-value');
}); });
it('should log error when localStorage throws', () => {
const consoleSpy = jest.spyOn(console, 'error').mockImplementation();
localStorageMock.setItem.mockImplementationOnce(() => {
throw new Error('Storage error');
});
storage.set('test-key', 'test-value');
expect(consoleSpy).toHaveBeenCalled();
consoleSpy.mockRestore();
});
}); });
describe('storage.remove', () => { describe('storage.remove', () => {
@ -88,6 +108,16 @@ describe('Storage Helpers', () => {
storage.remove('test-key'); storage.remove('test-key');
expect(localStorageMock.removeItem).toHaveBeenCalledWith('test-key'); expect(localStorageMock.removeItem).toHaveBeenCalledWith('test-key');
}); });
it('should log error when localStorage throws', () => {
const consoleSpy = jest.spyOn(console, 'error').mockImplementation();
localStorageMock.removeItem.mockImplementationOnce(() => {
throw new Error('Storage error');
});
storage.remove('test-key');
expect(consoleSpy).toHaveBeenCalled();
consoleSpy.mockRestore();
});
}); });
describe('storage.clear', () => { describe('storage.clear', () => {
@ -95,6 +125,16 @@ describe('Storage Helpers', () => {
storage.clear(); storage.clear();
expect(localStorageMock.clear).toHaveBeenCalled(); expect(localStorageMock.clear).toHaveBeenCalled();
}); });
it('should log error when localStorage throws', () => {
const consoleSpy = jest.spyOn(console, 'error').mockImplementation();
localStorageMock.clear.mockImplementationOnce(() => {
throw new Error('Storage error');
});
storage.clear();
expect(consoleSpy).toHaveBeenCalled();
consoleSpy.mockRestore();
});
}); });
}); });