|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 2 | +import { loadFromStorage, saveToStorage, removeFromStorage } from '@todo-starter/utils'; |
| 3 | + |
| 4 | +const KEY = 'test/storage@v1'; |
| 5 | + |
| 6 | +// Save original env to restore between tests |
| 7 | +const ORIGINAL_ENV = process.env.NODE_ENV; |
| 8 | + |
| 9 | +describe('storage utils', () => { |
| 10 | + function ensureWindowWithLocalStorage() { |
| 11 | + // Ensure a Window-like global for Node environment |
| 12 | + if (typeof window === 'undefined') { |
| 13 | + Object.defineProperty(globalThis, 'window', { |
| 14 | + // unknown avoids explicit any; cast to Window shape for tests |
| 15 | + value: {} as unknown as Window & typeof globalThis, |
| 16 | + configurable: true |
| 17 | + }); |
| 18 | + } |
| 19 | + // Polyfill localStorage if missing |
| 20 | + if (!('localStorage' in window)) { |
| 21 | + const store = new Map<string, string>(); |
| 22 | + Object.defineProperty(window, 'localStorage', { |
| 23 | + value: { |
| 24 | + getItem: (k: string) => store.get(k) ?? null, |
| 25 | + setItem: (k: string, v: string) => { |
| 26 | + store.set(k, v); |
| 27 | + }, |
| 28 | + removeItem: (k: string) => { |
| 29 | + store.delete(k); |
| 30 | + } |
| 31 | + }, |
| 32 | + configurable: true |
| 33 | + }); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + beforeEach(() => { |
| 38 | + // Ensure clean slate |
| 39 | + try { |
| 40 | + window.localStorage.removeItem(KEY); |
| 41 | + } catch { |
| 42 | + // ignore |
| 43 | + } |
| 44 | + }); |
| 45 | + |
| 46 | + afterEach(() => { |
| 47 | + process.env.NODE_ENV = ORIGINAL_ENV; |
| 48 | + try { |
| 49 | + window.localStorage.removeItem(KEY); |
| 50 | + } catch { |
| 51 | + // ignore |
| 52 | + } |
| 53 | + }); |
| 54 | + |
| 55 | + it('SSR/test guard disables storage (returns fallback in test env)', () => { |
| 56 | + // In vitest, NODE_ENV is "test" by default. Verify guard path returns fallback. |
| 57 | + window.localStorage.setItem(KEY, JSON.stringify({ value: 123 })); |
| 58 | + const result = loadFromStorage(KEY, { value: 999 }); |
| 59 | + expect(result).toEqual({ value: 999 }); |
| 60 | + }); |
| 61 | + |
| 62 | + it('Malformed JSON returns fallback', () => { |
| 63 | + // Enable storage access by switching to a non-test env for this test |
| 64 | + process.env.NODE_ENV = 'development'; |
| 65 | + ensureWindowWithLocalStorage(); |
| 66 | + window.localStorage.setItem(KEY, '{not json'); |
| 67 | + const result = loadFromStorage(KEY, { good: true }); |
| 68 | + expect(result).toEqual({ good: true }); |
| 69 | + }); |
| 70 | + |
| 71 | + it('save/remove round-trip behavior works', () => { |
| 72 | + process.env.NODE_ENV = 'development'; |
| 73 | + ensureWindowWithLocalStorage(); |
| 74 | + |
| 75 | + const value = { a: 1, b: 'two' }; |
| 76 | + saveToStorage(KEY, value); |
| 77 | + |
| 78 | + const loaded = loadFromStorage<typeof value | null>(KEY, null); |
| 79 | + expect(loaded).toEqual(value); |
| 80 | + |
| 81 | + removeFromStorage(KEY); |
| 82 | + const afterRemove = loadFromStorage<typeof value | null>(KEY, null); |
| 83 | + expect(afterRemove).toBeNull(); |
| 84 | + }); |
| 85 | + |
| 86 | + it('validate guard: rejects invalid shape and returns fallback', () => { |
| 87 | + process.env.NODE_ENV = 'development'; |
| 88 | + ensureWindowWithLocalStorage(); |
| 89 | + |
| 90 | + window.localStorage.setItem(KEY, JSON.stringify({ nope: true })); |
| 91 | + |
| 92 | + const fallback = { ok: true }; |
| 93 | + const result = loadFromStorage( |
| 94 | + KEY, |
| 95 | + fallback, |
| 96 | + (v): v is typeof fallback => |
| 97 | + typeof v === 'object' && v !== null && 'ok' in v && typeof (v as { ok: unknown }).ok === 'boolean' |
| 98 | + ); |
| 99 | + expect(result).toEqual(fallback); |
| 100 | + }); |
| 101 | + |
| 102 | + it('validate guard: accepts valid shape', () => { |
| 103 | + process.env.NODE_ENV = 'development'; |
| 104 | + ensureWindowWithLocalStorage(); |
| 105 | + |
| 106 | + const value = { ok: true }; |
| 107 | + window.localStorage.setItem(KEY, JSON.stringify(value)); |
| 108 | + |
| 109 | + const result = loadFromStorage( |
| 110 | + KEY, |
| 111 | + { ok: false }, |
| 112 | + (v): v is typeof value => |
| 113 | + typeof v === 'object' && v !== null && 'ok' in v && typeof (v as { ok: unknown }).ok === 'boolean' |
| 114 | + ); |
| 115 | + expect(result).toEqual(value); |
| 116 | + }); |
| 117 | +}); |
0 commit comments