import { describe, it, expect, beforeEach, vi } from 'vitest' import { createSpreadDownloadConfig } from './download' import type { SpreadWidgetData } from './types' const data: SpreadWidgetData = [ { min: 1, max: 9, prefix: '$', suffix: 'k', note: 'q1', series: { name: 'Revenue' }, }, { min: 0, max: 10 }, ] 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('createSpreadDownloadConfig', () => { it('CSV-only by default', () => { expect( createSpreadDownloadConfig({ filename: 's', getData: () => data }).map( (i) => i.id, ), ).toEqual(['csv']) }) it('prepends PNG when getCaptureEl is provided', () => { const items = createSpreadDownloadConfig({ filename: 's', getData: () => data, getCaptureEl: () => document.createElement('div'), }) expect(items.map((i) => i.id)).toEqual(['png', 'csv']) }) it('CSV resolve emits one row per item with optional fields blanked', async () => { const items = createSpreadDownloadConfig({ filename: 's', getData: () => data, }) const handle = await items.find((i) => i.id === 'csv')!.resolve() expect(handle.filename).toBe('s.csv') expect(csvText).toBe( 'series,prefix,min,max,suffix,note\nRevenue,$,1,9,k,q1\n,,0,10,,', ) }) })