forked from CredenceOrg/Credence-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclearAppLocalStorage.test.ts
More file actions
36 lines (31 loc) · 1.2 KB
/
Copy pathclearAppLocalStorage.test.ts
File metadata and controls
36 lines (31 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { clearAppLocalStorage } from './clearAppLocalStorage'
describe('clearAppLocalStorage', () => {
beforeEach(() => {
localStorage.clear()
})
afterEach(() => {
vi.restoreAllMocks()
vi.unstubAllGlobals()
})
it('clears all items from localStorage', () => {
localStorage.setItem('credence:settings', JSON.stringify({ theme: 'dark' }))
localStorage.setItem('credence:onboarding:step', '3')
localStorage.setItem('other-key', 'keep-me')
clearAppLocalStorage()
expect(localStorage.getItem('credence:settings')).toBeNull()
expect(localStorage.getItem('credence:onboarding:step')).toBeNull()
expect(localStorage.getItem('other-key')).toBeNull()
expect(localStorage.length).toBe(0)
})
it('does not throw when localStorage.clear throws', () => {
vi.spyOn(Storage.prototype, 'clear').mockImplementation(() => {
throw new DOMException('Storage unavailable')
})
expect(() => clearAppLocalStorage()).not.toThrow()
})
it('does nothing when window is undefined (SSR)', () => {
vi.stubGlobal('window', undefined)
expect(() => clearAppLocalStorage()).not.toThrow()
})
})