Skip to content

Commit

Permalink
fix: fix localStore getValue error ignoring initial value
Browse files Browse the repository at this point in the history
  • Loading branch information
rxliuli committed Oct 14, 2024
1 parent 4fc0c53 commit c1d2130
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
13 changes: 11 additions & 2 deletions src/lib/utils/__tests__/localStore.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
// @vitest-environment happy-dom
import 'fake-indexeddb/auto'
import { expect, it } from 'vitest'
import { beforeEach, expect, it } from 'vitest'
import {
indexedDBAdapter,
localStorageAdapter,
localStore,
} from '../localStore'
import { get } from 'svelte/store'
import { get as idbGet } from 'idb-keyval'
import { get as idbGet, clear } from 'idb-keyval'

beforeEach(async () => {
await clear()
})

it('indexedDBAdapter', async () => {
const store = localStore('settings', { theme: 'system' }, indexedDBAdapter())
Expand Down Expand Up @@ -47,3 +51,8 @@ it('update', async () => {
await new Promise((resolve) => setTimeout(resolve, 0))
expect(await idbGet('settings')).toEqual({ theme: 'dark' })
})

it('localStore for default value', async () => {
const store = localStore('settings', { theme: 'system' }, indexedDBAdapter())
expect(await store.getValue()).toEqual({ theme: 'system' })
})
2 changes: 1 addition & 1 deletion src/lib/utils/localStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function localStore<T>(
})
},
async getValue() {
const r = await adapter.read(key)
const r = (await adapter.read(key)) ?? initial
set(r)
return r
},
Expand Down

0 comments on commit c1d2130

Please sign in to comment.