import { describe, it, expect, beforeEach, vi } from 'vitest' import { createScatterplotDownloadConfig } from './download' import type { ScatterplotWidgetData } from './types' const data: ScatterplotWidgetData = [ [ [1, 10], [2, 20], ], [[3, 30]], ] let csvText = '' beforeEach(() => { csvText = '' vi.spyOn(URL, 'createObjectURL').mockReturnValue('blob:mock') vi.spyOn(URL, 'revokeObjectURL').mockImplementation(() => undefined) const RealBlob = global.Blob vi.stubGlobal( 'Blob', class extends RealBlob { constructor(parts: BlobPart[], opts?: BlobPropertyBag) { csvText = typeof parts[0] === 'string' ? parts[0] : '' super(parts, opts) } }, ) }) describe('createScatterplotDownloadConfig', () => { it('CSV-only by default', () => { expect( createScatterplotDownloadConfig({ filename: 's', getData: () => data, }).map((i) => i.id), ).toEqual(['csv']) }) it('prepends PNG when getCaptureEl is provided', () => { const items = createScatterplotDownloadConfig({ filename: 's', getData: () => data, getCaptureEl: () => document.createElement('div'), }) expect(items.map((i) => i.id)).toEqual(['png', 'csv']) }) it('CSV resolve writes series × point rows with default names', async () => { const items = createScatterplotDownloadConfig({ filename: 's', getData: () => data, }) const handle = await items.find((i) => i.id === 'csv')!.resolve() expect(handle.filename).toBe('s.csv') expect(csvText).toBe( 'series,x,y\nseries_1,1,10\nseries_1,2,20\nseries_2,3,30', ) }) it('CSV honours supplied seriesNames', async () => { const items = createScatterplotDownloadConfig({ filename: 's', getData: () => data, seriesNames: ['A', 'B'], }) await items.find((i) => i.id === 'csv')!.resolve() expect(csvText.split('\n')[1]).toBe('A,1,10') }) })