test: add vitest-environment-nuxt
(#1186)
This commit is contained in:
parent
3513f09148
commit
bf25339bc9
15 changed files with 82 additions and 64 deletions
|
@ -3,6 +3,8 @@ import type { Ref } from 'vue'
|
|||
import { del, get, set, update } from 'idb-keyval'
|
||||
import type { UseIDBOptions } from '@vueuse/integrations/useIDBKeyval'
|
||||
|
||||
const isIDBSupported = !process.test && typeof indexedDB !== 'undefined'
|
||||
|
||||
export async function useAsyncIDBKeyval<T>(
|
||||
key: IDBValidKey,
|
||||
initialValue: MaybeComputedRef<T>,
|
||||
|
@ -22,6 +24,8 @@ export async function useAsyncIDBKeyval<T>(
|
|||
const rawInit: T = resolveUnref(initialValue)
|
||||
|
||||
async function read() {
|
||||
if (!isIDBSupported)
|
||||
return
|
||||
try {
|
||||
const rawValue = await get<T>(key)
|
||||
if (rawValue === undefined) {
|
||||
|
@ -40,6 +44,8 @@ export async function useAsyncIDBKeyval<T>(
|
|||
await read()
|
||||
|
||||
async function write() {
|
||||
if (!isIDBSupported)
|
||||
return
|
||||
try {
|
||||
if (data.value == null) {
|
||||
await del(key)
|
||||
|
|
|
@ -4,7 +4,7 @@ import { STORAGE_KEY_DRAFTS } from '~/constants'
|
|||
import type { Draft, DraftMap } from '~/types'
|
||||
import type { Mutable } from '~/types/utils'
|
||||
|
||||
export const currentUserDrafts = process.server ? computed<DraftMap>(() => ({})) : useUserLocalStorage<DraftMap>(STORAGE_KEY_DRAFTS, () => ({}))
|
||||
export const currentUserDrafts = process.server || process.test ? computed<DraftMap>(() => ({})) : useUserLocalStorage<DraftMap>(STORAGE_KEY_DRAFTS, () => ({}))
|
||||
|
||||
export const builtinDraftKeys = [
|
||||
'dialog',
|
||||
|
|
|
@ -289,7 +289,7 @@ interface UseUserLocalStorageCache {
|
|||
* Create reactive storage for the current user
|
||||
*/
|
||||
export function useUserLocalStorage<T extends object>(key: string, initial: () => T): Ref<T> {
|
||||
if (process.server)
|
||||
if (process.server || process.test)
|
||||
return shallowRef(initial())
|
||||
|
||||
// @ts-expect-error bind value to the function
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue