Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(useFavicon): improve type overload #2123

Merged
merged 1 commit into from Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
69 changes: 69 additions & 0 deletions packages/core/useFavicon/index.test.ts
@@ -0,0 +1,69 @@
import { describe, expect } from 'vitest'
import { computed, ref } from 'vue'
import { useFavicon } from '.'

describe('useFavicon', () => {
it('no param', () => {
const favicon = useFavicon()
expect(favicon.value).toEqual(null)
favicon.value = 'https://www.google.at/favicon.ico'
expect(favicon.value).toEqual('https://www.google.at/favicon.ico')
})

it('const', () => {
const favicon = useFavicon('v1')
expect(favicon.value).toEqual('v1')
favicon.value = 'v2'
expect(favicon.value).toEqual('v2')
})

it('null', () => {
const favicon = useFavicon(null)
expect(favicon.value).toEqual(null)
favicon.value = 'v1'
expect(favicon.value).toEqual('v1')
})

it('undefined', () => {
const favicon = useFavicon(undefined)
expect(favicon.value).toEqual(null)
favicon.value = 'v1'
expect(favicon.value).toEqual('v1')
})

it('ref const', () => {
const tagetRef = ref('v1')
const favicon = useFavicon(tagetRef)
expect(favicon.value).toEqual('v1')
tagetRef.value = 'v2'
expect(favicon.value).toEqual('v2')
})

it('ref null', () => {
const tagetRef = ref(null)
const favicon = useFavicon(tagetRef)
expect(favicon.value).toEqual(null)
})

it('ref undefined', () => {
const favicon = useFavicon(ref(undefined))
expect(favicon.value).toEqual(undefined)
})

it('computed/readonly', () => {
const onoff = ref(1)
const target = computed(() => onoff.value === 1 ? 'a.jpg' : 'b.jpg')
const favicon = useFavicon(target)
expect(favicon.value).toEqual('a.jpg')
onoff.value = 2
expect(favicon.value).toEqual('b.jpg')
// favicon.value = '1'
})

it('function/readonly', () => {
const target = () => 'a.jpg'
const favicon = useFavicon(target)
expect(favicon.value).toEqual('a.jpg')
// favicon.value = '1'
})
})
8 changes: 4 additions & 4 deletions packages/core/useFavicon/index.ts
@@ -1,6 +1,6 @@
import type { MaybeComputedRef, MaybeReadonlyRef, MaybeRef } from '@vueuse/shared'
import { isString, resolveRef } from '@vueuse/shared'
import type { ComputedRef, Ref, WritableComputedRef } from 'vue-demi'
import type { ComputedRef, Ref } from 'vue-demi'
import { watch } from 'vue-demi'
import type { ConfigurableDocument } from '../_configurable'
import { defaultDocument } from '../_configurable'
Expand All @@ -18,11 +18,11 @@ export interface UseFaviconOptions extends ConfigurableDocument {
* @param options
*/
export function useFavicon(
newIcon?: MaybeReadonlyRef<string | null | undefined>,
newIcon: MaybeReadonlyRef<string | null | undefined>,
options?: UseFaviconOptions
): ComputedRef<string | null | undefined>
export function useFavicon(
newIcon: MaybeRef<string | null | undefined>,
newIcon?: MaybeRef<string | null | undefined>,
options?: UseFaviconOptions
): Ref<string | null | undefined>
export function useFavicon(
Expand Down Expand Up @@ -52,7 +52,7 @@ export function useFavicon(
{ immediate: true },
)

return favicon as WritableComputedRef<string | null | undefined>
return favicon
}

export type UseFaviconReturn = ReturnType<typeof useFavicon>