Skip to content

Commit c49f6b5

Browse files
added unit test for localstorage-writable and fixed s small bug
Signed-off-by: Benjamin Strasser <bp.strasser@gmail.com>
1 parent eb13488 commit c49f6b5

File tree

2 files changed

+141
-1
lines changed

2 files changed

+141
-1
lines changed

src/lib/utils/localstorage-writable.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export const localStorageWritable = <T>(
2020

2121
const initialValue: T | null = localStorageValue ?? defaultValue ?? null
2222

23-
const { set, subscribe } = writable<T | null>(initialValue)
23+
const { set, subscribe } = writable<T | null>(null)
2424

2525
const setAndStore = (value: T | null) => {
2626
if (browser) {
@@ -38,6 +38,8 @@ export const localStorageWritable = <T>(
3838
set(null)
3939
}
4040

41+
setAndStore(initialValue)
42+
4143
return {
4244
subscribe,
4345
set: setAndStore,
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
import { localStorageWritable } from './localstorage-writable'
2+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
3+
import { get } from 'svelte/store'
4+
5+
const mockedBrowser = vi.hoisted(() => {
6+
return vi.fn()
7+
})
8+
9+
vi.mock('$app/environment', () => {
10+
return {
11+
get browser() {
12+
return mockedBrowser()
13+
}
14+
}
15+
})
16+
17+
const localStorageMockFactory = () => {
18+
const store: Record<string, string> = {}
19+
20+
return {
21+
getItem: (key: string) => store[key] || null,
22+
setItem: (key: string, value: string) => {
23+
store[key] = value
24+
},
25+
removeItem: (key: string) => {
26+
delete store[key]
27+
}
28+
}
29+
}
30+
31+
describe('localStorageWritable', () => {
32+
beforeEach(() => {
33+
mockedBrowser.mockReturnValue(true)
34+
vi.stubGlobal('localStorage', localStorageMockFactory())
35+
})
36+
37+
afterEach(() => {
38+
vi.resetAllMocks()
39+
})
40+
41+
it('should initialize with default value if no value in localStorage', () => {
42+
const defaultValue = 'default'
43+
44+
const store = localStorageWritable('testKey', defaultValue)
45+
46+
expect(get(store)).toBe(defaultValue)
47+
expect(localStorage.getItem('testKey')).toBe(JSON.stringify(defaultValue))
48+
})
49+
50+
it('should initialize with value from localStorage if present', () => {
51+
localStorage.setItem('testKey', JSON.stringify('storedValue'))
52+
53+
const store = localStorageWritable('testKey')
54+
55+
expect(get(store)).toBe('storedValue')
56+
})
57+
58+
it('should prioritize value from local storage over default', () => {
59+
localStorage.setItem('testKey', JSON.stringify('storedValue'))
60+
61+
const store = localStorageWritable('testKey', 'defaultValue')
62+
63+
expect(get(store)).toBe('storedValue')
64+
expect(localStorage.getItem('testKey')).toBe(JSON.stringify('storedValue'))
65+
})
66+
67+
it('should store value in localStorage when set', () => {
68+
const store = localStorageWritable('testKey')
69+
store.set('newValue')
70+
71+
expect(localStorage.getItem('testKey')).toBe(JSON.stringify('newValue'))
72+
expect(get(store)).toBe('newValue')
73+
})
74+
75+
it('should remove value from localStorage when wipe is called', () => {
76+
localStorage.setItem('testKey', JSON.stringify('storedValue'))
77+
78+
const store = localStorageWritable('testKey')
79+
store.wipe()
80+
81+
expect(localStorage.getItem('testKey')).toBe(null)
82+
expect(get(store)).toBe(null)
83+
})
84+
85+
it('should update the value correctly using update method', () => {
86+
const store = localStorageWritable<number>('testKey', 1)
87+
store.update((n) => n! + 1)
88+
89+
expect(localStorage.getItem('testKey')).toBe(JSON.stringify(2))
90+
expect(get(store)).toBe(2)
91+
})
92+
})
93+
94+
describe('localStorageWritable with no browser', () => {
95+
beforeEach(() => {
96+
mockedBrowser.mockReturnValue(false)
97+
vi.stubGlobal('localStorage', undefined)
98+
})
99+
100+
afterEach(() => {
101+
vi.resetAllMocks()
102+
})
103+
104+
it('should initialize with default value', () => {
105+
const defaultValue = 'default'
106+
const store = localStorageWritable('testKey', defaultValue)
107+
108+
expect(get(store)).toBe(defaultValue)
109+
})
110+
111+
it('should initialize with null', () => {
112+
const store = localStorageWritable('testKey')
113+
114+
expect(get(store)).toBe(null)
115+
})
116+
117+
it('should set value', () => {
118+
const store = localStorageWritable('testKey')
119+
store.set('newValue')
120+
121+
expect(get(store)).toBe('newValue')
122+
})
123+
124+
it('should remove value', () => {
125+
const store = localStorageWritable('testKey')
126+
store.set('newValue')
127+
store.wipe()
128+
129+
expect(get(store)).toBe(null)
130+
})
131+
132+
it('should update the value correctly using update method', () => {
133+
const store = localStorageWritable<number>('testKey', 1)
134+
store.update((n) => n! + 1)
135+
136+
expect(get(store)).toBe(2)
137+
})
138+
})

0 commit comments

Comments
 (0)