import { defaultIconProps } from '@iconify/utils/lib/icon/defaults'; import { cleanupGlobals, setupDOM, waitDOMReady } from './helpers'; import { scanRootNode } from '../src/scanner/find'; import { renderInlineSVG } from '../src/render/svg'; import type { IconifyIcon } from '@iconify/utils/lib/icon/defaults'; import { elementDataProperty, IconifyElement } from '../src/scanner/config'; describe('Testing rendering nodes', () => { afterEach(cleanupGlobals); const testIcon = async ( placeholder: string, data: IconifyIcon, expected: string ): Promise => { setupDOM(placeholder); await waitDOMReady(); // Find node const root = document.body; const items = scanRootNode(root); expect(items.length).toBe(1); // Get node and render it const { node, props } = items[0]; const svg = renderInlineSVG(node, props, { ...defaultIconProps, ...data, }); // Find SVG in DOM expect(root.childNodes.length).toBe(1); const expectedSVG = root.childNodes[0]; expect(expectedSVG).toBe(svg); // Get HTML const html = root.innerHTML; expect(html).toBe(expected); return svg; }; it('Rendering simple icon', async () => { const svg = await testIcon( '', { body: '', width: 24, height: 24, }, '' ); const data = svg[elementDataProperty]; expect(data.status).toBe('loaded'); expect(data.addedClasses).toEqual(['iconify--mdi']); expect(data.addedStyles).toEqual([]); }); it('Inline icon and transformation', async () => { const svg = await testIcon( '', { body: '', width: 24, height: 24, }, '' ); const data = svg[elementDataProperty]; expect(data.status).toBe('loaded'); expect(data.addedClasses).toEqual(['iconify', 'iconify--mdi']); expect(data.addedStyles).toEqual(['vertical-align']); }); it('Passing attributes and style', async () => { const svg = await testIcon( '', { body: '', width: 24, height: 24, }, '' ); const data = svg[elementDataProperty]; expect(data.status).toBe('loaded'); expect(data.addedClasses).toEqual([]); // All classes already existed on placeholder expect(data.addedStyles).toEqual([]); // Overwritten by entry in placeholder }); it('Inline icon and vertical-align', async () => { const svg = await testIcon( '', { body: '', width: 24, height: 24, }, '' ); const data = svg[elementDataProperty]; expect(data.status).toBe('loaded'); expect(data.addedClasses).toEqual(['iconify', 'iconify--mdi']); expect(data.addedStyles).toEqual([]); }); it('Inline icon and custom style without ;', async () => { const svg = await testIcon( '', { body: '', width: 24, height: 24, }, '' ); const data = svg[elementDataProperty]; expect(data.status).toBe('loaded'); expect(data.addedClasses).toEqual([ 'iconify', 'iconify--provider', 'iconify--mdi-light', ]); expect(data.addedStyles).toEqual(['vertical-align']); }); it('Identical prefix and provider', async () => { const svg = await testIcon( '', { body: '', width: 24, height: 24, }, '' ); const data = svg[elementDataProperty]; expect(data.status).toBe('loaded'); expect(data.addedClasses).toEqual(['iconify--test']); expect(data.addedStyles).toEqual([]); }); });