diff --git a/frontend/src/components/settings/PairingQrCode.test.tsx b/frontend/src/components/settings/PairingQrCode.test.tsx index d8839e97..0eca2543 100644 --- a/frontend/src/components/settings/PairingQrCode.test.tsx +++ b/frontend/src/components/settings/PairingQrCode.test.tsx @@ -1,8 +1,28 @@ -import { describe, expect, it } from 'vitest' +import { afterEach, describe, expect, it, vi } from 'vitest' import { render, screen } from '@testing-library/react' import { PairingQrCode } from './PairingQrCode' describe('PairingQrCode', () => { + afterEach(() => { + vi.doUnmock('uqr') + vi.resetModules() + }) + + it('reports a renderer that fails to load', async () => { + vi.resetModules() + vi.doMock('uqr', () => { + throw new Error('chunk failed') + }) + const { PairingQrCode: FreshPairingQrCode } = await import('./PairingQrCode') + + render() + + expect( + await screen.findByText('The QR code could not be loaded. Reload the page and try again.'), + ).toBeInTheDocument() + expect(screen.queryByAltText('Pairing QR code')).not.toBeInTheDocument() + }) + it('renders the QR code once uqr has loaded', async () => { render() diff --git a/frontend/src/components/settings/PairingQrCode.tsx b/frontend/src/components/settings/PairingQrCode.tsx index f4cf6f23..d7ce3b7c 100644 --- a/frontend/src/components/settings/PairingQrCode.tsx +++ b/frontend/src/components/settings/PairingQrCode.tsx @@ -11,12 +11,19 @@ interface PairingQrCodeProps { export function PairingQrCode({ value, label, blurred = false }: PairingQrCodeProps) { const [renderSvg, setRenderSvg] = useState(null) + const [loadFailed, setLoadFailed] = useState(false) useEffect(() => { let active = true - void import('uqr').then((module) => { - if (active) setRenderSvg(() => module.renderSVG) - }) + const loadRenderer = async () => { + try { + const module = await import('uqr') + if (active) setRenderSvg(() => module.renderSVG) + } catch { + if (active) setLoadFailed(true) + } + } + void loadRenderer() return () => { active = false } @@ -31,6 +38,14 @@ export function PairingQrCode({ value, label, blurred = false }: PairingQrCodePr } }, [renderSvg, value]) + if (loadFailed) { + return ( +

+ The QR code could not be loaded. Reload the page and try again. +

+ ) + } + if (!renderSvg) return null if (!source) {