|
| 1 | +import { fileURLToPath } from 'node:url' |
| 2 | +import { expect, test } from '@playwright/test' |
| 3 | + |
| 4 | +const fixtureUrl = `/@fs${fileURLToPath(new URL('./icons.fixture.ts', import.meta.url))}` |
| 5 | +const svg = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="100%" height="100%"><path fill="currentColor" d="M4 12l5 5L20 6"/><script>alert(1)</script></svg>' |
| 6 | + |
| 7 | +test.beforeEach(async ({ page }) => { |
| 8 | + await page.route('https://api.iconify.design/**', route => route.fulfill({ body: svg, contentType: 'image/svg+xml' })) |
| 9 | + await page.goto('/') |
| 10 | + await expect(page.getByRole('button', { name: 'No Renderer', exact: true })).toBeVisible() |
| 11 | +}) |
| 12 | + |
| 13 | +test('shares pending requests, sanitizes SVG and caches success', async ({ page }) => { |
| 14 | + let requests = 0 |
| 15 | + await page.route('**/test/shared.svg?*', async (route) => { |
| 16 | + requests++ |
| 17 | + await route.fulfill({ body: svg }) |
| 18 | + }) |
| 19 | + const results = await page.evaluate(async (url) => { |
| 20 | + const { getIconifySvg } = await import(url) |
| 21 | + const values = await Promise.all([getIconifySvg('test', 'shared'), getIconifySvg('test', 'shared')]) |
| 22 | + return [...values, await getIconifySvg('test', 'shared')] |
| 23 | + }, fixtureUrl) |
| 24 | + expect(requests).toBe(1) |
| 25 | + expect(new Set(results).size).toBe(1) |
| 26 | + expect(results[0]).toContain('currentColor') |
| 27 | + expect(results[0]).not.toContain('<script') |
| 28 | +}) |
| 29 | + |
| 30 | +for (const failure of [404, 429, 500, 'invalid', 'empty', 'network', 'timeout'] as const) { |
| 31 | + test(`evicts ${failure} failures and makes no automatic retry`, async ({ page }) => { |
| 32 | + const result = await page.evaluate(async ({ url, failure, svg }) => { |
| 33 | + const { getIconifySvg } = await import(url) |
| 34 | + const originalFetch = window.fetch |
| 35 | + let calls = 0 |
| 36 | + let timeout = 0 |
| 37 | + const originalTimeout = AbortSignal.timeout |
| 38 | + AbortSignal.timeout = (milliseconds) => { |
| 39 | + timeout = milliseconds |
| 40 | + return originalTimeout(1) |
| 41 | + } |
| 42 | + window.fetch = async (_input, options) => { |
| 43 | + if (++calls > 1) |
| 44 | + return new Response(svg) |
| 45 | + if (failure === 'network') |
| 46 | + throw new TypeError('offline') |
| 47 | + if (failure === 'timeout') { |
| 48 | + await new Promise(resolve => options!.signal!.addEventListener('abort', resolve, { once: true })) |
| 49 | + throw options!.signal!.reason |
| 50 | + } |
| 51 | + return typeof failure === 'number' |
| 52 | + ? new Response(svg, { status: failure }) |
| 53 | + : new Response(failure === 'empty' ? '<svg xmlns="http://www.w3.org/2000/svg"/>' : '<html>error</html>') |
| 54 | + } |
| 55 | + try { |
| 56 | + const first = await getIconifySvg('test', 'failure').then(() => 'success', () => 'failed') |
| 57 | + const callsAfterFailure = calls |
| 58 | + const second = await getIconifySvg('test', 'failure') |
| 59 | + return { first, callsAfterFailure, calls, timeout, second } |
| 60 | + } |
| 61 | + finally { |
| 62 | + window.fetch = originalFetch |
| 63 | + AbortSignal.timeout = originalTimeout |
| 64 | + } |
| 65 | + }, { url: fixtureUrl, failure, svg }) |
| 66 | + expect(result.first).toBe('failed') |
| 67 | + expect(result.callsAfterFailure).toBe(1) |
| 68 | + expect(result.calls).toBe(2) |
| 69 | + expect(result.timeout).toBe(10_000) |
| 70 | + expect(result.second).toContain('<svg') |
| 71 | + }) |
| 72 | +} |
| 73 | + |
| 74 | +test('ignores stale component results without cancelling shared requests', async ({ page }) => { |
| 75 | + const result = await page.evaluate(async ({ url, svg }) => { |
| 76 | + const { createApp, h, ref, nextTick, IconifyIcon, getIconifySvg } = await import(url) |
| 77 | + const originalFetch = window.fetch |
| 78 | + let resolveFetch!: (response: Response) => void |
| 79 | + window.fetch = () => new Promise<Response>((resolve) => { |
| 80 | + resolveFetch = resolve |
| 81 | + }) |
| 82 | + const source = ref('test:pending') |
| 83 | + const firstContainer = document.createElement('div') |
| 84 | + const secondContainer = document.createElement('div') |
| 85 | + const removedContainer = document.createElement('div') |
| 86 | + const first = createApp({ render: () => h(IconifyIcon, { icon: source.value }) }) |
| 87 | + const second = createApp(IconifyIcon, { icon: 'test:pending' }) |
| 88 | + const removed = createApp(IconifyIcon, { icon: 'test:pending' }) |
| 89 | + removed.mount(removedContainer) |
| 90 | + removed.unmount() |
| 91 | + first.mount(firstContainer) |
| 92 | + second.mount(secondContainer) |
| 93 | + source.value = 'data:image/svg+xml,<svg/>' |
| 94 | + await nextTick() |
| 95 | + resolveFetch(new Response(svg)) |
| 96 | + await getIconifySvg('test', 'pending') |
| 97 | + await nextTick() |
| 98 | + const changed = firstContainer.innerHTML |
| 99 | + const shared = secondContainer.innerHTML |
| 100 | + first.unmount() |
| 101 | + second.unmount() |
| 102 | + window.fetch = originalFetch |
| 103 | + return { changed, shared, removed: removedContainer.innerHTML } |
| 104 | + }, { url: fixtureUrl, svg }) |
| 105 | + expect(result.changed).toContain('<img') |
| 106 | + expect(result.changed).not.toContain('<path') |
| 107 | + expect(result.shared).toContain('<path') |
| 108 | + expect(result.removed).toBe('') |
| 109 | +}) |
| 110 | + |
| 111 | +test('retries on a later mount and replaces the fallback', async ({ page }) => { |
| 112 | + const result = await page.evaluate(async ({ url, svg }) => { |
| 113 | + const { createApp, nextTick, IconifyIcon, getIconifySvg } = await import(url) |
| 114 | + const originalFetch = window.fetch |
| 115 | + let calls = 0 |
| 116 | + window.fetch = async () => { |
| 117 | + if (++calls === 1) |
| 118 | + throw new TypeError('offline') |
| 119 | + return new Response(svg) |
| 120 | + } |
| 121 | + const container = document.createElement('div') |
| 122 | + const first = createApp(IconifyIcon, { icon: 'test:remount' }) |
| 123 | + first.mount(container) |
| 124 | + await getIconifySvg('test', 'remount').catch(() => {}) |
| 125 | + await nextTick() |
| 126 | + const fallback = container.querySelector('svg')?.getAttribute('aria-hidden') |
| 127 | + first.unmount() |
| 128 | + const second = createApp(IconifyIcon, { icon: 'test:remount' }) |
| 129 | + second.mount(container) |
| 130 | + await getIconifySvg('test', 'remount') |
| 131 | + await nextTick() |
| 132 | + const recovered = container.innerHTML |
| 133 | + second.unmount() |
| 134 | + window.fetch = originalFetch |
| 135 | + return { fallback, recovered, calls } |
| 136 | + }, { url: fixtureUrl, svg }) |
| 137 | + expect(result.fallback).toBe('true') |
| 138 | + expect(result.recovered).toContain('M4 12l5 5L20 6') |
| 139 | + expect(result.calls).toBe(2) |
| 140 | +}) |
| 141 | + |
| 142 | +for (const mode of ['standalone', 'embedded']) { |
| 143 | + test(`fallback and reload recovery screenshots: ${mode}`, async ({ page }, testInfo) => { |
| 144 | + await page.route('https://api.iconify.design/**', route => route.fulfill({ status: 503, body: 'Unavailable' })) |
| 145 | + await page.goto(mode === 'embedded' ? '/?embedded' : '/') |
| 146 | + const button = page.getByRole('button', { name: 'Settings', exact: true }) |
| 147 | + await expect(button.locator('svg[aria-hidden="true"]')).toBeVisible() |
| 148 | + await button.click() |
| 149 | + await expect(button).toBeEnabled() |
| 150 | + await page.screenshot({ animations: 'disabled', path: testInfo.outputPath(`${mode}-failure.png`) }) |
| 151 | + await page.unroute('https://api.iconify.design/**') |
| 152 | + await page.route('https://api.iconify.design/**', route => route.fulfill({ body: svg, contentType: 'image/svg+xml' })) |
| 153 | + await page.reload() |
| 154 | + await expect(button.locator('svg path')).toHaveAttribute('d', 'M4 12l5 5L20 6') |
| 155 | + await button.click() |
| 156 | + await expect(page.getByText('Color mode', { exact: true })).toBeVisible() |
| 157 | + await expect(button.locator('svg')).toBeVisible() |
| 158 | + await page.screenshot({ animations: 'disabled', path: testInfo.outputPath(`${mode}-recovered.png`) }) |
| 159 | + }) |
| 160 | +} |
0 commit comments