|
| 1 | +// @vitest-environment jsdom |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 3 | + |
| 4 | +const svg = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path fill="currentColor" d="M0 0h24v24H0z"/></svg>' |
| 5 | + |
| 6 | +beforeEach(() => { |
| 7 | + vi.resetModules() |
| 8 | + vi.useFakeTimers() |
| 9 | +}) |
| 10 | + |
| 11 | +afterEach(() => { |
| 12 | + vi.unstubAllGlobals() |
| 13 | + vi.restoreAllMocks() |
| 14 | + vi.useRealTimers() |
| 15 | +}) |
| 16 | + |
| 17 | +describe('remote icons', () => { |
| 18 | + it('recovers the actual mounted component after a transient failure', async () => { |
| 19 | + expect.assertions(3) |
| 20 | + const fetchMock = vi.fn().mockRejectedValueOnce(new TypeError('offline')).mockResolvedValue(new Response(svg)) |
| 21 | + vi.stubGlobal('fetch', fetchMock) |
| 22 | + const { createApp, nextTick } = await import('vue') |
| 23 | + const { default: IconifyIcon } = await import('../components/icons/IconifyIcon.vue') |
| 24 | + const container = document.createElement('div') |
| 25 | + const application = createApp(IconifyIcon, { icon: 'ph:robot-duotone' }) |
| 26 | + application.mount(container) |
| 27 | + try { |
| 28 | + expect(container.querySelector('svg')).toBeNull() |
| 29 | + await vi.advanceTimersByTimeAsync(500) |
| 30 | + await nextTick() |
| 31 | + expect(container.querySelector('svg')).not.toBeNull() |
| 32 | + expect(fetchMock).toHaveBeenCalledTimes(2) |
| 33 | + } |
| 34 | + finally { |
| 35 | + application.unmount() |
| 36 | + } |
| 37 | + }) |
| 38 | + |
| 39 | + it('lets a shared request finish after one consumer unmounts', async () => { |
| 40 | + expect.assertions(3) |
| 41 | + const fetchMock = vi.fn().mockRejectedValueOnce(new TypeError('offline')).mockResolvedValue(new Response(svg)) |
| 42 | + vi.stubGlobal('fetch', fetchMock) |
| 43 | + const { createApp, nextTick } = await import('vue') |
| 44 | + const { default: IconifyIcon } = await import('../components/icons/IconifyIcon.vue') |
| 45 | + const firstContainer = document.createElement('div') |
| 46 | + const secondContainer = document.createElement('div') |
| 47 | + const first = createApp(IconifyIcon, { icon: 'ph:robot-duotone' }) |
| 48 | + const second = createApp(IconifyIcon, { icon: 'ph:robot-duotone' }) |
| 49 | + first.mount(firstContainer) |
| 50 | + second.mount(secondContainer) |
| 51 | + first.unmount() |
| 52 | + try { |
| 53 | + await vi.advanceTimersByTimeAsync(500) |
| 54 | + await nextTick() |
| 55 | + expect(firstContainer.innerHTML).toBe('') |
| 56 | + expect(secondContainer.querySelector('svg')).not.toBeNull() |
| 57 | + expect(fetchMock).toHaveBeenCalledTimes(2) |
| 58 | + } |
| 59 | + finally { |
| 60 | + second.unmount() |
| 61 | + } |
| 62 | + }) |
| 63 | + |
| 64 | + it('shares retries and caches the successful SVG', async () => { |
| 65 | + expect.assertions(6) |
| 66 | + const fetchMock = vi.fn().mockRejectedValueOnce(new TypeError('offline')).mockResolvedValue(new Response(svg)) |
| 67 | + vi.stubGlobal('fetch', fetchMock) |
| 68 | + const { getIconifySvg } = await import('./iconify') |
| 69 | + const first = getIconifySvg('ph', 'robot-duotone') |
| 70 | + const second = getIconifySvg('ph', 'robot-duotone') |
| 71 | + await vi.advanceTimersByTimeAsync(499) |
| 72 | + expect(fetchMock).toHaveBeenCalledTimes(1) |
| 73 | + await vi.advanceTimersByTimeAsync(1) |
| 74 | + expect(await first).toContain('<svg') |
| 75 | + expect(await second).toBe(await first) |
| 76 | + expect(fetchMock).toHaveBeenCalledTimes(2) |
| 77 | + expect(await getIconifySvg('ph', 'robot-duotone')).toBe(await first) |
| 78 | + expect(fetchMock).toHaveBeenCalledTimes(2) |
| 79 | + }) |
| 80 | + |
| 81 | + it.each([408, 429, 500, 503])('retries HTTP %i', async (status) => { |
| 82 | + expect.assertions(2) |
| 83 | + const fetchMock = vi.fn().mockResolvedValueOnce(new Response('', { status })).mockResolvedValue(new Response(svg)) |
| 84 | + vi.stubGlobal('fetch', fetchMock) |
| 85 | + const { getIconifySvg } = await import('./iconify') |
| 86 | + const result = getIconifySvg('ph', 'robot-duotone') |
| 87 | + await vi.advanceTimersByTimeAsync(500) |
| 88 | + expect(await result).toContain('<svg') |
| 89 | + expect(fetchMock).toHaveBeenCalledTimes(2) |
| 90 | + }) |
| 91 | + |
| 92 | + it('exhausts three attempts and allows a later request', async () => { |
| 93 | + expect.assertions(5) |
| 94 | + const fetchMock = vi.fn().mockRejectedValue(new TypeError('offline')) |
| 95 | + vi.stubGlobal('fetch', fetchMock) |
| 96 | + const { getIconifySvg } = await import('./iconify') |
| 97 | + const result = expect(getIconifySvg('ph', 'robot-duotone')).rejects.toThrow('offline') |
| 98 | + await vi.advanceTimersByTimeAsync(500) |
| 99 | + expect(fetchMock).toHaveBeenCalledTimes(2) |
| 100 | + await vi.advanceTimersByTimeAsync(1499) |
| 101 | + expect(fetchMock).toHaveBeenCalledTimes(2) |
| 102 | + await vi.advanceTimersByTimeAsync(1) |
| 103 | + await result |
| 104 | + expect(fetchMock).toHaveBeenCalledTimes(3) |
| 105 | + fetchMock.mockResolvedValue(new Response(svg)) |
| 106 | + expect(await getIconifySvg('ph', 'robot-duotone')).toContain('<svg') |
| 107 | + }) |
| 108 | + |
| 109 | + it.each([400, 403, 404])('does not retry or cache HTTP %i', async (status) => { |
| 110 | + expect.assertions(3) |
| 111 | + const fetchMock = vi.fn().mockResolvedValueOnce(new Response(svg, { status })).mockResolvedValue(new Response(svg)) |
| 112 | + vi.stubGlobal('fetch', fetchMock) |
| 113 | + const { getIconifySvg } = await import('./iconify') |
| 114 | + await expect(getIconifySvg('ph', 'robot-duotone')).rejects.toThrow() |
| 115 | + expect(fetchMock).toHaveBeenCalledTimes(1) |
| 116 | + expect(await getIconifySvg('ph', 'robot-duotone')).toContain('<svg') |
| 117 | + }) |
| 118 | + |
| 119 | + it.each(['', '<html>Unavailable</html>', '<svg xmlns="http://www.w3.org/2000/svg"><script>alert(1)</script></svg>'])('rejects invalid SVG without caching it', async (body) => { |
| 120 | + expect.assertions(3) |
| 121 | + const fetchMock = vi.fn().mockResolvedValueOnce(new Response(body)).mockResolvedValue(new Response(svg)) |
| 122 | + vi.stubGlobal('fetch', fetchMock) |
| 123 | + const { getIconifySvg } = await import('./iconify') |
| 124 | + await expect(getIconifySvg('ph', 'robot-duotone')).rejects.toThrow() |
| 125 | + expect(fetchMock).toHaveBeenCalledTimes(1) |
| 126 | + expect(await getIconifySvg('ph', 'robot-duotone')).toContain('<svg') |
| 127 | + }) |
| 128 | + |
| 129 | + it('bounds each attempt to ten seconds', async () => { |
| 130 | + expect.assertions(3) |
| 131 | + const timeout = vi.spyOn(AbortSignal, 'timeout') |
| 132 | + const fetchMock = vi.fn().mockRejectedValueOnce(new DOMException('timeout', 'TimeoutError')).mockResolvedValue(new Response(svg)) |
| 133 | + vi.stubGlobal('fetch', fetchMock) |
| 134 | + const { getIconifySvg } = await import('./iconify') |
| 135 | + const result = getIconifySvg('ph', 'robot-duotone') |
| 136 | + await vi.advanceTimersByTimeAsync(500) |
| 137 | + expect(await result).toContain('<svg') |
| 138 | + expect(timeout).toHaveBeenCalledTimes(2) |
| 139 | + expect(timeout).toHaveBeenNthCalledWith(2, 10_000) |
| 140 | + }) |
| 141 | + |
| 142 | + it('sanitizes executable content while preserving currentColor', async () => { |
| 143 | + expect.assertions(3) |
| 144 | + vi.stubGlobal('fetch', vi.fn().mockResolvedValue(new Response(svg.replace('<path ', '<script>alert(1)</script><path onclick="alert(1)" ')))) |
| 145 | + const { getIconifySvg } = await import('./iconify') |
| 146 | + const result = await getIconifySvg('ph', 'robot-duotone') |
| 147 | + expect(result).toContain('currentColor') |
| 148 | + expect(result).not.toContain('<script') |
| 149 | + expect(result).not.toContain('onclick') |
| 150 | + }) |
| 151 | +}) |
0 commit comments