|
| 1 | +import { describe, expect, it, vi } from 'vitest' |
| 2 | +import { h, createSSRApp } from 'vue' |
| 3 | +import { renderToString } from 'vue/server-renderer' |
| 4 | +import { mountSuspended } from '@nuxt/test-utils/runtime' |
| 5 | +import { addIcon, setCustomIconLoader } from '@iconify/vue' |
| 6 | +import Icon from '../../src/runtime/vue/components/Icon.vue' |
| 7 | + |
| 8 | +function renderIconToString(name: string) { |
| 9 | + return renderToString(createSSRApp({ render: () => h(Icon, { name, mode: 'svg' }) })) |
| 10 | +} |
| 11 | + |
| 12 | +describe('Icon', () => { |
| 13 | + it('outputs the real icon body when server-rendering a preloaded icon', async () => { |
| 14 | + addIcon('mock:server-icon', { |
| 15 | + body: '<path d="M2 2h20v20H2z" />', |
| 16 | + width: 24, |
| 17 | + height: 24 |
| 18 | + }) |
| 19 | + |
| 20 | + const html = await renderIconToString('i-mock-server-icon') |
| 21 | + |
| 22 | + expect(html).toContain('<path') |
| 23 | + }) |
| 24 | + |
| 25 | + it('keeps working on the client for a preloaded icon', async () => { |
| 26 | + addIcon('mock:client-icon', { |
| 27 | + body: '<path d="M2 2h20v20H2z" />', |
| 28 | + width: 24, |
| 29 | + height: 24 |
| 30 | + }) |
| 31 | + |
| 32 | + const wrapper = await mountSuspended(Icon, { |
| 33 | + props: { name: 'i-mock-client-icon', mode: 'svg' } |
| 34 | + }) |
| 35 | + |
| 36 | + expect(wrapper.html()).toContain('<path') |
| 37 | + }) |
| 38 | + |
| 39 | + it('falls back to an empty svg on the client when the icon is unknown', async () => { |
| 40 | + // null loader on a dedicated prefix so the lookup stays offline instead of hitting the Iconify API |
| 41 | + setCustomIconLoader(() => null, 'mock-missing') |
| 42 | + |
| 43 | + const wrapper = await mountSuspended(Icon, { |
| 44 | + props: { name: 'i-mock-missing:icon', mode: 'svg' } |
| 45 | + }) |
| 46 | + |
| 47 | + expect(wrapper.html()).toContain('<svg') |
| 48 | + expect(wrapper.html()).not.toContain('<path') |
| 49 | + }) |
| 50 | + |
| 51 | + it('never asks the loader for an icon that is not preloaded during SSR', async () => { |
| 52 | + // isolated prefix so a load queued by another test's `onMounted` can't bleed into this assertion |
| 53 | + const fetchIcon = vi.fn(() => new Promise<never>(() => {})) |
| 54 | + setCustomIconLoader(fetchIcon, 'mock-ssr-remote') |
| 55 | + |
| 56 | + await renderIconToString('i-mock-ssr-remote:icon') |
| 57 | + // the loader call is scheduled through `setTimeout`, give it a chance to run before checking |
| 58 | + await new Promise(resolve => setTimeout(resolve)) |
| 59 | + |
| 60 | + expect(fetchIcon).not.toHaveBeenCalled() |
| 61 | + }) |
| 62 | +}) |
0 commit comments