import { afterEach, beforeEach, describe, expect, it, vi, type Mock } from 'vitest' import { chartToSvgString, chartToPngBlob, downloadChartSvg, downloadChartPng, } from './chart-export' const SVG_NS = 'http://www.w3.org/2000/svg' /** * Build a small but realistic chart SVG matching the structure * chart-export expects: `svg.sv-grid-chart-svg` with a viewBox, an * interaction hit layer (`.sv-grid-chart-hit`) that the exporter must * strip, plus a few themed nodes. */ function makeChartSvg(opts: { viewBox?: string } = {}): SVGSVGElement { const svg = document.createElementNS(SVG_NS, 'svg') as SVGSVGElement svg.setAttribute('class', 'sv-grid-chart-svg') svg.setAttribute('viewBox', opts.viewBox ?? '0 0 400 250') const axis = document.createElementNS(SVG_NS, 'text') axis.setAttribute('class', 'sv-grid-chart-axis') axis.textContent = 'A' svg.appendChild(axis) const hit = document.createElementNS(SVG_NS, 'rect') hit.setAttribute('class', 'sv-grid-chart-hit') svg.appendChild(hit) const bar = document.createElementNS(SVG_NS, 'rect') bar.setAttribute('class', 'sv-grid-chart-bar') svg.appendChild(bar) return svg } describe('chartToSvgString', () => { it('serializes the chart svg passed directly as an SVGSVGElement', () => { const svg = makeChartSvg() const out = chartToSvgString(svg) expect(out).toContain(' { const wrap = document.createElement('div') wrap.appendChild(makeChartSvg()) const out = chartToSvgString(wrap) expect(out).toContain(' when the class svg is absent', () => { const wrap = document.createElement('div') const svg = document.createElementNS(SVG_NS, 'svg') as SVGSVGElement svg.setAttribute('viewBox', '0 0 100 100') wrap.appendChild(svg) const out = chartToSvgString(wrap) expect(out).toContain(' can be found', () => { const div = document.createElement('div') expect(() => chartToSvgString(div)).toThrow(/no found/) }) it('inlines a