ShtukbaseStandard
archivo del paquete→ .shtukbase/packages/icons/test/render.test.ts

test/render.test.ts

80 líneas, 4.6 KB.

El archivo tal como está en el paquete: el código y sus comentarios no se traducen.

  1. // The renderer lib/render.js: the flat form, canonical (bringing a drawing in), snap and render in a style.
  2. import { describe, expect, test } from 'bun:test';
  3. import { validate } from '../lib/svg.ts';
  4. // eslint-disable-next-line @typescript-eslint/no-require-imports
  5. const R = require('../lib/render.js');
  6. const PAL = {
  7. violet: ['#975CDC', '#5D3C85', '#D6BCFF'],
  8. amber: ['#F0AD15', '#694900', '#EBC482'],
  9. blue: ['#2383E2', '#12508E', '#A5CEFF'],
  10. ink: '#2B2B2E', grey: '#8A8680', greyLight: '#C9C6BF', white: '#FFFFFF',
  11. };
  12. const FLAT = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 96 96">\n<rect x="12" y="38" width="72" height="50" rx="9" fill="#975CDC"/>\n<path d="M20 20L76 20" fill="none" stroke="#F0AD15" stroke-width="8" stroke-linecap="round"/>\n</svg>\n';
  13. const S79 = { outline: 'hueink', outlineWidth: 1.75, rim: 5, rimColor: '#FFFFFF', shadow: 0.13, scale: 0.88 };
  14. describe('render', () => {
  15. test('style 79: outline in the dark step of the hue, a white rim, a soft shadow, scaled', () => {
  16. const out = R.render(FLAT, S79, PAL);
  17. expect(out).toContain('stroke="#5D3C85" stroke-width="1.75"'); // violet's dark step around the violet rect
  18. expect(out).toContain('stroke="#694900" stroke-width="11.5"'); // amber line: 8 + 2 × 1.75
  19. expect(out).toContain('opacity="0.13" fill="#000000"');
  20. expect(out).toContain('fill="#FFFFFF" stroke="#FFFFFF"');
  21. expect(out).toContain('scale(0.88)');
  22. expect(out).toContain('<defs><rect id="g0"');
  23. });
  24. test('the rendered icon keeps the SVG contract of ICN-R01', () => {
  25. const out = R.render(FLAT, S79, PAL);
  26. expect(validate(out, 4096).filter((f) => f.level === 'error')).toEqual([]);
  27. });
  28. test('ink and deep outlines; no outline, no rim, no shadow when the style has none', () => {
  29. expect(R.render(FLAT, { outline: 'ink', outlineWidth: 1.5 }, PAL)).toContain('stroke="#2B2B2E" stroke-width="1.5"');
  30. expect(R.render(FLAT, { outline: 'deep', outlineWidth: 1.75 }, PAL)).toContain(`stroke="${R.mix('#975CDC', '#5D3C85', 0.55)}"`);
  31. const plain = R.render(FLAT, {}, PAL);
  32. expect(plain).not.toContain('opacity=');
  33. expect(plain).not.toContain('scale(');
  34. });
  35. test('the id prefix keeps two inlined icons apart', () => {
  36. expect(R.render(FLAT, S79, PAL, { idPrefix: 'box-' })).toContain('id="box-0"');
  37. });
  38. test('parse refuses what is not the flat form', () => {
  39. expect(() => R.parse('<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 96 96"><g><rect width="1" height="1"/></g></svg>')).toThrow('not a drawing element');
  40. expect(() => R.parse('<div/>')).toThrow();
  41. });
  42. });
  43. describe('snap and canonical', () => {
  44. test('snap: a palette colour stays, a near one goes to its hue and step', () => {
  45. expect(R.snap('#975CDC', PAL)).toEqual({ fill: '#975CDC', hue: 'violet', tone: 'main' });
  46. expect(R.snap('#9A6DD7', PAL).hue).toBe('violet');
  47. expect(R.snap('#FFFFFF', PAL).tone).toBe('white');
  48. expect(R.snap('#191919', PAL).fill).toBe('#2B2B2E');
  49. expect(R.snap('none', PAL)).toBeNull();
  50. });
  51. test('canonical: groups dissolved, the tile dropped, white lines on it take its colour, colours snapped', () => {
  52. const src = '<?xml version="1.0"?><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 96 96" width="96" height="96"><title>x</title>'
  53. + '<rect x="12" y="12" width="72" height="72" rx="14" fill="#529CCA"/>'
  54. + '<g fill="#9A6DD7"><circle cx="30" cy="30" r="8"/></g>'
  55. + '<path d="M38 28H28V68" stroke="#FFFFFF" stroke-width="7" fill="none"/>'
  56. + '<rect x="40" y="40" width="10" height="10" style="fill:#FFDC49"/></svg>';
  57. const { svg, notes } = R.canonical(src, PAL);
  58. expect(svg).not.toContain('<g');
  59. expect(svg).not.toContain('width="72"');
  60. expect(svg).not.toContain('style=');
  61. expect(svg).not.toContain('width="96"');
  62. expect(svg).toContain('<circle cx="30" cy="30" r="8" fill="#975CDC"/>');
  63. expect(svg).toContain('stroke="#2383E2"'); // the white line took the (snapped) tile colour
  64. expect(notes.join(' ')).toContain('tile');
  65. expect(R.parse(svg).els.length).toBe(3);
  66. });
  67. test('canonical keeps a big shape that is not the backmost one (a calendar body over its rings)', () => {
  68. const src = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 96 96"><rect x="26" y="4" width="8" height="18" fill="#B4B2AC"/><rect x="8" y="14" width="80" height="72" fill="#DCEEF7"/></svg>';
  69. expect(R.parse(R.canonical(src, PAL).svg).els.length).toBe(2);
  70. });
  71. test('flatColors: every step of every hue, ink, greys and white', () => {
  72. const c = R.flatColors(PAL);
  73. expect(c).toContain('#5D3C85');
  74. expect(c).toContain(R.mix('#975CDC', '#5D3C85', 0.42));
  75. expect(c).toContain('#C9C6BF');
  76. });
  77. });