|
| 1 | +import { ref, defineComponent } from 'vue' |
1 | 2 | import { mount } from '@vue/test-utils' |
2 | | -import { ref } from 'vue' |
3 | 3 | import useFavicon from '..' |
4 | | -import Test from './Test.vue' |
5 | 4 |
|
6 | 5 | const DEFAULT_FAVICON_URL = |
7 | 6 | 'https://raw.githubusercontent.com/InhiblabCore/vue-hooks-plus/master/packages/hooks/docs/public/logo.svg' |
8 | 7 | const GOOGLE_FAVICON_URL = 'https://www.google.com/favicon.ico' |
9 | 8 |
|
10 | | -describe('useFavicon', () => { |
11 | | - const url = ref<string | undefined>(DEFAULT_FAVICON_URL) |
12 | | - useFavicon(url) |
13 | | - it('default be DEFAULT_FAVICON_URL', () => { |
14 | | - expect(url.value).toBe(DEFAULT_FAVICON_URL) |
15 | | - }) |
16 | | - |
17 | | - it('should work', async () => { |
18 | | - url.value = GOOGLE_FAVICON_URL |
19 | | - expect(url.value).toBe(GOOGLE_FAVICON_URL) |
20 | | - |
21 | | - const wrapper = mount(Test) |
| 9 | +function getFaviconHref() { |
| 10 | + const link = document.head.querySelector('link[rel*="icon"]') as HTMLLinkElement | null |
| 11 | + return link?.href || '' |
| 12 | +} |
22 | 13 |
|
23 | | - const currentFaviconURL = wrapper.find('span') |
| 14 | +describe('useFavicon', () => { |
| 15 | + let originalFavicon: string | null = null |
24 | 16 |
|
25 | | - const toggleToGoogleBtn = wrapper.find('.button1') |
26 | | - const toggleToAHooksBtn = wrapper.find('.button2') |
| 17 | + beforeEach(() => { |
| 18 | + const link = document.head.querySelector('link[rel="icon"]') as HTMLLinkElement | null |
| 19 | + originalFavicon = link?.href || null |
| 20 | + }) |
27 | 21 |
|
28 | | - expect(currentFaviconURL.text()).toBe(DEFAULT_FAVICON_URL) |
| 22 | + afterEach(() => { |
| 23 | + // 恢复原始 favicon |
| 24 | + let link = document.head.querySelector('link[rel="icon"]') as HTMLLinkElement | null |
| 25 | + if (!link && originalFavicon) { |
| 26 | + link = document.createElement('link') |
| 27 | + link.rel = 'icon' |
| 28 | + document.head.appendChild(link) |
| 29 | + } |
| 30 | + if (link && originalFavicon) { |
| 31 | + link.href = originalFavicon |
| 32 | + } else if (link) { |
| 33 | + link.parentNode?.removeChild(link) |
| 34 | + } |
| 35 | + }) |
29 | 36 |
|
30 | | - await toggleToGoogleBtn?.trigger('click') |
31 | | - expect(currentFaviconURL.text()).toBe(GOOGLE_FAVICON_URL) |
| 37 | + it('should set favicon to the provided URL', async () => { |
| 38 | + const url = ref(DEFAULT_FAVICON_URL) |
| 39 | + mount(defineComponent({ |
| 40 | + setup() { |
| 41 | + useFavicon(url) |
| 42 | + return () => null |
| 43 | + } |
| 44 | + })) |
| 45 | + await Promise.resolve().then() |
| 46 | + expect(getFaviconHref()).toBe(DEFAULT_FAVICON_URL) |
| 47 | + }) |
32 | 48 |
|
33 | | - await toggleToAHooksBtn?.trigger('click') |
34 | | - expect(currentFaviconURL.text()).toBe(DEFAULT_FAVICON_URL) |
| 49 | + it('should update favicon when URL changes', async () => { |
| 50 | + const url = ref(DEFAULT_FAVICON_URL) |
| 51 | + const wrapper = mount(defineComponent({ |
| 52 | + setup() { |
| 53 | + useFavicon(url) |
| 54 | + return () => null |
| 55 | + } |
| 56 | + })) |
| 57 | + await Promise.resolve().then() |
| 58 | + expect(getFaviconHref()).toBe(DEFAULT_FAVICON_URL) |
| 59 | + url.value = GOOGLE_FAVICON_URL |
| 60 | + await wrapper.vm.$nextTick() |
| 61 | + await Promise.resolve().then() |
| 62 | + expect(getFaviconHref()).toBe(GOOGLE_FAVICON_URL) |
35 | 63 | }) |
36 | 64 |
|
37 | | - it('support undefined', () => { |
| 65 | + it('should remove favicon when URL is undefined', async () => { |
| 66 | + const url = ref<string | undefined>(DEFAULT_FAVICON_URL) |
| 67 | + const wrapper = mount(defineComponent({ |
| 68 | + setup() { |
| 69 | + useFavicon(url) |
| 70 | + return () => null |
| 71 | + } |
| 72 | + })) |
| 73 | + await Promise.resolve().then() |
| 74 | + expect(getFaviconHref()).toBe(DEFAULT_FAVICON_URL) |
38 | 75 | url.value = undefined |
39 | | - expect(url.value).toBeUndefined |
| 76 | + await wrapper.vm.$nextTick() |
| 77 | + await Promise.resolve().then() |
| 78 | + const link = document.head.querySelector('link[rel="icon"]') |
| 79 | + expect(link).toBeNull() |
| 80 | + }) |
| 81 | + |
| 82 | + it('should restore previous favicon on unmount', async () => { |
| 83 | + const url = ref(GOOGLE_FAVICON_URL) |
| 84 | + const wrapper = mount(defineComponent({ |
| 85 | + setup() { |
| 86 | + useFavicon(url) |
| 87 | + return () => null |
| 88 | + } |
| 89 | + })) |
| 90 | + await Promise.resolve().then() |
| 91 | + expect(getFaviconHref()).toBe(GOOGLE_FAVICON_URL) |
| 92 | + wrapper.unmount() |
| 93 | + await Promise.resolve().then() |
| 94 | + expect(getFaviconHref()).toBe(GOOGLE_FAVICON_URL) |
40 | 95 | }) |
41 | 96 | }) |
0 commit comments