import { isValidElement, type ReactElement } from 'react' import { iconFromSvg } from '../src/icon' import type { IconDefinition } from 'json-edit-react' // Pull the dangerouslySetInnerHTML payload off a string-built glyph's content. const innerHtml = (def: IconDefinition): string | undefined => (def.content as ReactElement<{ dangerouslySetInnerHTML?: { __html: string } }>).props .dangerouslySetInnerHTML?.__html // ─── A — string parsing (real-world fixtures) ──────────────────────────────── describe('iconFromSvg — string parsing', () => { it('lifts viewBox + stroke svgProps from a full (Lucide-style), dropping size/xmlns', () => { const def = iconFromSvg( '' ) expect(def.viewBox).toBe('0 0 24 24') expect(def.svgProps).toEqual({ fill: 'none', stroke: 'currentColor', strokeWidth: '2', strokeLinecap: 'round', strokeLinejoin: 'round', }) expect(innerHtml(def)).toBe('') }) it('parses a Boxicons-style fill (no extra root attrs → no svgProps)', () => { const def = iconFromSvg('') expect(def.viewBox).toBe('0 0 24 24') expect(def.svgProps).toBeUndefined() expect(innerHtml(def)).toBe('') }) it('keeps a FontAwesome-style 512 viewBox', () => { expect(iconFromSvg('').viewBox).toBe( '0 0 512 512' ) }) it('ignores a leading /comment and accepts single-quoted attrs', () => { const def = iconFromSvg( "" ) expect(def.viewBox).toBe('0 0 24 24') expect(def.svgProps).toEqual({ fill: 'red' }) expect(innerHtml(def)).toBe("") }) it('strips width/height so core controls the size', () => { const def = iconFromSvg( '' ) expect(def.svgProps).toBeUndefined() }) it('passes nested inner markup through verbatim', () => { const def = iconFromSvg('') expect(innerHtml(def)).toBe('') }) it('treats bare inner markup (no wrapper) as content with defaults', () => { const def = iconFromSvg('') expect(def.viewBox).toBeUndefined() expect(def.svgProps).toBeUndefined() expect(innerHtml(def)).toBe('') }) }) // ─── B — React element input ───────────────────────────────────────────────── describe('iconFromSvg — element input', () => { it('unwraps a React via props/children, dropping size (nested-svg prevention)', () => { const def = iconFromSvg( ) expect(def.viewBox).toBe('0 0 24 24') expect(def.svgProps).toEqual({ fill: 'none', stroke: 'currentColor', strokeWidth: 2 }) // content is the children, NOT the — so core won't double-wrap. const content = def.content as ReactElement expect(content.type).toBe('path') }) it('routes a non- element to content directly', () => { const path = const def = iconFromSvg(path) expect(def.content).toBe(path) expect(def.viewBox).toBeUndefined() expect(def.svgProps).toBeUndefined() }) }) // ─── C — passthrough & interning ───────────────────────────────────────────── describe('iconFromSvg — passthrough & interning', () => { it('returns an IconDefinition unchanged', () => { const def: IconDefinition = { content: , viewBox: '0 0 10 10' } expect(iconFromSvg(def)).toBe(def) expect(isValidElement(def)).toBe(false) // sanity: a definition isn't an element }) it('interns string inputs — same string returns the same reference', () => { const s = '' expect(iconFromSvg(s)).toBe(iconFromSvg(s)) }) it('returns distinct references for different strings', () => { expect(iconFromSvg('')).not.toBe( iconFromSvg('') ) }) })