|
| 1 | +import React from 'react'; |
| 2 | +import { render } from '@testing-library/react'; |
| 3 | +import { |
| 4 | + Icon, |
| 5 | + SpriteContextProvider, |
| 6 | + IconsCache, |
| 7 | + initOnClient, |
| 8 | + renderSpriteSheetToString, |
| 9 | +} from '../src/index'; |
| 10 | +import { act } from 'react-dom/test-utils'; |
| 11 | +import { renderToString } from 'react-dom/server'; |
| 12 | + |
| 13 | +const svg1 = `<svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24"> |
| 14 | +<path d="M0 0h24v24H0z" fill="none"/> |
| 15 | +</svg>`; |
| 16 | +const svg2 = `<svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24"> |
| 17 | +<path d="M0 0h24v24H0z" fill="none"/> |
| 18 | +</svg>`; |
| 19 | + |
| 20 | +const loadSVG = async (url: string) => { |
| 21 | + switch (url) { |
| 22 | + case '1': { |
| 23 | + return svg1; |
| 24 | + } |
| 25 | + case '2': |
| 26 | + default: |
| 27 | + return svg2; |
| 28 | + } |
| 29 | +}; |
| 30 | + |
| 31 | +test('should fill the cache when an icon is rendered', async () => { |
| 32 | + const cache: IconsCache = new Map(); |
| 33 | + const { rerender } = render( |
| 34 | + <SpriteContextProvider knownIcons={cache} loadSVG={loadSVG}> |
| 35 | + <Icon url={'1'}></Icon> |
| 36 | + </SpriteContextProvider> |
| 37 | + ); |
| 38 | + expect(cache.size).toBe(1); |
| 39 | + await act(async () => { |
| 40 | + const iconData = await cache.get('1'); |
| 41 | + |
| 42 | + expect(iconData?.attributes.viewBox).toBe('0 0 24 24'); |
| 43 | + expect(iconData?.svgString.__html).toBe( |
| 44 | + '<path d="M0 0h24v24H0z" fill="none"/>' |
| 45 | + ); |
| 46 | + }); |
| 47 | + |
| 48 | + rerender( |
| 49 | + <SpriteContextProvider knownIcons={cache} loadSVG={loadSVG}> |
| 50 | + <Icon url={'2'}></Icon> |
| 51 | + </SpriteContextProvider> |
| 52 | + ); |
| 53 | + await act(async () => { |
| 54 | + expect(cache.size).toBe(2); |
| 55 | + const iconData = await cache.get('2'); |
| 56 | + expect(iconData?.attributes.viewBox).toBe('0 0 24 24'); |
| 57 | + expect(iconData?.svgString.__html).toBe( |
| 58 | + '<path d="M0 0h24v24H0z" fill="none"/>' |
| 59 | + ); |
| 60 | + }); |
| 61 | +}); |
| 62 | + |
| 63 | +test('render loaded svgs to a svg sprite sheet string', async () => { |
| 64 | + const cache: IconsCache = new Map(); |
| 65 | + const renderedString = renderToString( |
| 66 | + <SpriteContextProvider knownIcons={cache} loadSVG={loadSVG}> |
| 67 | + <Icon url={'1'}></Icon> |
| 68 | + </SpriteContextProvider> |
| 69 | + ); |
| 70 | + |
| 71 | + const renderedSpriteSheet = await renderSpriteSheetToString( |
| 72 | + renderedString, |
| 73 | + cache |
| 74 | + ); |
| 75 | + |
| 76 | + expect(renderedSpriteSheet).toMatchInlineSnapshot( |
| 77 | + `"<svg><use xlink:href=\\"#1\\"></use></svg><svg id=\\"__SVG_SPRITE_SHEET__\\" style=\\"display:none\\"><symbol id=\\"1\\" xmlns=\\"http://www.w3.org/2000/svg\\" viewBox=\\"0 0 24 24\\"><path d=\\"M0 0h24v24H0z\\" fill=\\"none\\"/></symbol></svg>"` |
| 78 | + ); |
| 79 | +}); |
| 80 | + |
| 81 | +test('client should be able to initiate the cache from a rendered dom', async () => { |
| 82 | + const cache: IconsCache = new Map(); |
| 83 | + document.body.innerHTML = `<svg id="__SVG_SPRITE_SHEET__" style="display:none"> |
| 84 | + <symbol |
| 85 | + id="1" |
| 86 | + xmlns="http://www.w3.org/2000/svg" |
| 87 | + viewBox="0 0 24 24" |
| 88 | + > |
| 89 | + <path d="M0 0h24v24H0z" fill="none" /> |
| 90 | + </symbol> |
| 91 | + </svg>`; |
| 92 | + |
| 93 | + initOnClient(cache); |
| 94 | + |
| 95 | + expect(cache.size).toBe(1); |
| 96 | + const iconData = await cache.get('1'); |
| 97 | + |
| 98 | + expect(iconData?.id).toBe('1'); |
| 99 | + expect(iconData?.attributes.height).toBe(undefined); |
| 100 | + expect(iconData?.attributes.width).toBe(undefined); |
| 101 | + expect(iconData?.attributes.viewBox).toBe('0 0 24 24'); |
| 102 | + expect(iconData?.svgString.__html).toBe( |
| 103 | + '<path d="M0 0h24v24H0z" fill="none"></path>' |
| 104 | + ); |
| 105 | +}); |
0 commit comments