diff --git a/src/lib/utils/__tests__/localStore.test.ts b/src/lib/utils/__tests__/localStore.test.ts index 960f409..58e5afe 100644 --- a/src/lib/utils/__tests__/localStore.test.ts +++ b/src/lib/utils/__tests__/localStore.test.ts @@ -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()) @@ -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' }) +}) diff --git a/src/lib/utils/localStore.ts b/src/lib/utils/localStore.ts index 358037c..5f35381 100644 --- a/src/lib/utils/localStore.ts +++ b/src/lib/utils/localStore.ts @@ -32,7 +32,7 @@ export function localStore( }) }, async getValue() { - const r = await adapter.read(key) + const r = (await adapter.read(key)) ?? initial set(r) return r },