From daf83cbcccd791c8565d16a2cbad2c351f2f6652 Mon Sep 17 00:00:00 2001 From: Edward Runkel Date: Wed, 19 Jun 2024 08:05:06 -0400 Subject: [PATCH] Remove importing nuxt composable and mock it --- composables/useCurrentUser.ts | 1 - composables/useSessionToken.ts | 1 - vitest.setup.ts | 19 +++++++++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/composables/useCurrentUser.ts b/composables/useCurrentUser.ts index 837591ec..e901b39d 100644 --- a/composables/useCurrentUser.ts +++ b/composables/useCurrentUser.ts @@ -1,4 +1,3 @@ -import { useCookie } from 'nuxt/app' import type { UserViewModel } from 'lib/api/data-contracts' import type { CookieRef } from 'nuxt/app' diff --git a/composables/useSessionToken.ts b/composables/useSessionToken.ts index cafbfb11..34b8ac72 100644 --- a/composables/useSessionToken.ts +++ b/composables/useSessionToken.ts @@ -1,4 +1,3 @@ -import { useCookie } from 'nuxt/app' import type { CookieRef } from 'nuxt/app' export function useSessionToken(): CookieRef { diff --git a/vitest.setup.ts b/vitest.setup.ts index 26a822f8..8fd39016 100644 --- a/vitest.setup.ts +++ b/vitest.setup.ts @@ -11,6 +11,25 @@ const fetchMock = createFetchMock(vi) // sets globalThis.fetch and globalThis.fetchMock to our mocked version fetchMock.enableMocks() +const cookies = reactive<{ [key: string]: any }>({}) +interface CookieOptions { + default?: () => any +} + +export const useCookieMock = vi.fn((key: string, opts?: CookieOptions) => { + const cookie = toRef(cookies, key) + if (cookie.value === undefined && opts?.default) { + const initialValue = opts.default() + if (isRef(initialValue)) { + cookies[key] = initialValue + return initialValue as Ref + } + cookie.value = initialValue + } + return cookie +}) +vi.stubGlobal('useCookie', useCookieMock) + // Stolen from here: // https://zenn.dev/ninebolt6/articles/cadc924cb2416d const payload = reactive<{ state: Record }>({