From b36d71abcd7e0d1529063f75d79351475648aedb Mon Sep 17 00:00:00 2001 From: soufiane Date: Thu, 27 Nov 2025 14:37:07 +0100 Subject: [PATCH] test: add generateId fallback tests for SSR environments MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cover all branches of generateId function: - window.crypto (browser) - globalThis.crypto (Node.js) - timestamp fallback (no crypto) helpers.ts coverage: 96.96% lines, 78.72% branches 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- __tests__/utils/helpers.test.ts | 37 +++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/__tests__/utils/helpers.test.ts b/__tests__/utils/helpers.test.ts index a5a1032..6cda80c 100644 --- a/__tests__/utils/helpers.test.ts +++ b/__tests__/utils/helpers.test.ts @@ -391,6 +391,43 @@ describe('ID Generation', () => { expect(typeof id).toBe('string'); expect(id.length).toBeGreaterThan(0); }); + + it('should use window.crypto when available', () => { + const id = generateId(); + expect(id).toBeDefined(); + expect(id.length).toBeGreaterThan(5); + }); + + it('should fallback to globalThis.crypto when window.crypto is unavailable', () => { + const originalWindow = global.window; + // @ts-ignore + delete global.window; + + const id = generateId(); + expect(id).toBeDefined(); + expect(typeof id).toBe('string'); + + global.window = originalWindow; + }); + + it('should fallback to timestamp when no crypto is available', () => { + const originalWindow = global.window; + const originalGlobalThis = global.globalThis; + const originalCrypto = globalThis.crypto; + + // @ts-ignore + delete global.window; + // @ts-ignore + delete globalThis.crypto; + + const id = generateId(); + expect(id).toBeDefined(); + expect(typeof id).toBe('string'); + expect(id.length).toBeGreaterThan(0); + + global.window = originalWindow; + globalThis.crypto = originalCrypto; + }); }); });