From e488fa32993c7ea8ad19366b8216e8df425a7dd8 Mon Sep 17 00:00:00 2001 From: soufiane Date: Thu, 27 Nov 2025 14:26:58 +0100 Subject: [PATCH] test: add error handling tests for storage helpers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- __tests__/utils/helpers.test.ts | 40 +++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/__tests__/utils/helpers.test.ts b/__tests__/utils/helpers.test.ts index 27e3698..a5a1032 100644 --- a/__tests__/utils/helpers.test.ts +++ b/__tests__/utils/helpers.test.ts @@ -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(); + }); }); });