|
| 1 | +import { beforeEach, describe, expect, test } from 'vite-plus/test'; |
| 2 | + |
| 3 | +import { |
| 4 | + getConsentGiven as getStorageConsentGiven, |
| 5 | + setConsentGiven as setStorageConsentGiven, |
| 6 | +} from '../helpers/localStorage'; |
| 7 | +import { db } from './client'; |
| 8 | +import { getConsentGiven } from './config'; |
| 9 | + |
| 10 | +describe('getConsentGiven', () => { |
| 11 | + beforeEach(() => { |
| 12 | + localStorage.clear(); |
| 13 | + }); |
| 14 | + |
| 15 | + test('defaults to false when nothing is stored', async () => { |
| 16 | + expect(await getConsentGiven()).toBe(false); |
| 17 | + }); |
| 18 | + |
| 19 | + test('reads the value persisted in localStorage', async () => { |
| 20 | + setStorageConsentGiven(true); |
| 21 | + expect(await getConsentGiven()).toBe(true); |
| 22 | + |
| 23 | + setStorageConsentGiven(false); |
| 24 | + expect(await getConsentGiven()).toBe(false); |
| 25 | + }); |
| 26 | + |
| 27 | + test('migrates a legacy IndexedDB value into localStorage', async () => { |
| 28 | + await db.put('Options', { key: 'userConsent', value: true }); |
| 29 | + expect(getStorageConsentGiven()).toBeNull(); |
| 30 | + |
| 31 | + expect(await getConsentGiven()).toBe(true); |
| 32 | + expect(getStorageConsentGiven()).toBe(true); |
| 33 | + |
| 34 | + // Once migrated, the value survives even if the IndexedDB row is gone -- |
| 35 | + // a wedged PWA can no longer drop it. |
| 36 | + await db.delete('Options', 'userConsent'); |
| 37 | + expect(await getConsentGiven()).toBe(true); |
| 38 | + }); |
| 39 | + |
| 40 | + test('prefers localStorage over a stale legacy IndexedDB value', async () => { |
| 41 | + await db.put('Options', { key: 'userConsent', value: true }); |
| 42 | + setStorageConsentGiven(false); |
| 43 | + expect(await getConsentGiven()).toBe(false); |
| 44 | + }); |
| 45 | +}); |
0 commit comments