import { defaultIconProps } from '@iconify/utils/lib/icon/defaults'; import { cleanupGlobals, setupDOM, waitDOMReady } from './helpers'; import { scanRootNode } from '../src/scanner/find'; import { renderBackground } from '../src/render/bg'; import type { IconifyIcon } from '@iconify/utils/lib/icon/defaults'; import { elementDataProperty, IconifyElement } from '../src/scanner/config'; describe('Testing rendering nodes as background', () => { afterEach(cleanupGlobals); const expectedBackgroundStyles: string[] = [ '--svg', 'width', 'height', 'display', 'background-color', 'background-image', 'background-repeat', 'background-size', ]; 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 result = renderBackground( node, props, { ...defaultIconProps, ...data, }, data.body.indexOf('currentColor') !== -1 ); // Make sure node did not change expect(result).toBe(node); // Get HTML const html = root.innerHTML; expect(html).toBe(expected); return node; }; 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([...expectedBackgroundStyles]); }); 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([ ...expectedBackgroundStyles, '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([...expectedBackgroundStyles]); // 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([...expectedBackgroundStyles]); }); 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([ ...expectedBackgroundStyles, '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([...expectedBackgroundStyles]); }); it('Custom dimensions', async () => { await testIcon( '', { body: '', width: 24, height: 24, }, `` ); }); });