Skip to content

Commit

Permalink
Remove importing nuxt composable and mock it
Browse files Browse the repository at this point in the history
  • Loading branch information
erunks committed Jun 19, 2024
1 parent dff31c7 commit daf83cb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
1 change: 0 additions & 1 deletion composables/useCurrentUser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useCookie } from 'nuxt/app'
import type { UserViewModel } from 'lib/api/data-contracts'
import type { CookieRef } from 'nuxt/app'

Expand Down
1 change: 0 additions & 1 deletion composables/useSessionToken.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useCookie } from 'nuxt/app'
import type { CookieRef } from 'nuxt/app'

export function useSessionToken(): CookieRef<string> {
Expand Down
19 changes: 19 additions & 0 deletions vitest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>
}
cookie.value = initialValue
}
return cookie
})
vi.stubGlobal('useCookie', useCookieMock)

// Stolen from here:
// https://zenn.dev/ninebolt6/articles/cadc924cb2416d
const payload = reactive<{ state: Record<string, any> }>({
Expand Down

0 comments on commit daf83cb

Please sign in to comment.