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:
parent
e6769d507f
commit
e488fa3299
|
|
@ -74,6 +74,16 @@ describe('Storage Helpers', () => {
|
|||
it('should return null for non-existent key', () => {
|
||||
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', () => {
|
||||
|
|
@ -81,6 +91,16 @@ describe('Storage Helpers', () => {
|
|||
storage.set('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', () => {
|
||||
|
|
@ -88,6 +108,16 @@ describe('Storage Helpers', () => {
|
|||
storage.remove('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', () => {
|
||||
|
|
@ -95,6 +125,16 @@ describe('Storage Helpers', () => {
|
|||
storage.clear();
|
||||
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();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user