файл пакета→ .shtukbase/packages/icons/test/render.test.ts
test/render.test.ts
80 строк, 4.6 КБ.
Файл как есть в пакете: код и комментарии в нём не переводятся.
- // The renderer lib/render.js: the flat form, canonical (bringing a drawing in), snap and render in a style.
- import { describe, expect, test } from 'bun:test';
- import { validate } from '../lib/svg.ts';
- // eslint-disable-next-line @typescript-eslint/no-require-imports
- const R = require('../lib/render.js');
- const PAL = {
- violet: ['#975CDC', '#5D3C85', '#D6BCFF'],
- amber: ['#F0AD15', '#694900', '#EBC482'],
- blue: ['#2383E2', '#12508E', '#A5CEFF'],
- ink: '#2B2B2E', grey: '#8A8680', greyLight: '#C9C6BF', white: '#FFFFFF',
- };
- 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';
- const S79 = { outline: 'hueink', outlineWidth: 1.75, rim: 5, rimColor: '#FFFFFF', shadow: 0.13, scale: 0.88 };
- describe('render', () => {
- test('style 79: outline in the dark step of the hue, a white rim, a soft shadow, scaled', () => {
- const out = R.render(FLAT, S79, PAL);
- expect(out).toContain('stroke="#5D3C85" stroke-width="1.75"'); // violet's dark step around the violet rect
- expect(out).toContain('stroke="#694900" stroke-width="11.5"'); // amber line: 8 + 2 × 1.75
- expect(out).toContain('opacity="0.13" fill="#000000"');
- expect(out).toContain('fill="#FFFFFF" stroke="#FFFFFF"');
- expect(out).toContain('scale(0.88)');
- expect(out).toContain('<defs><rect id="g0"');
- });
- test('the rendered icon keeps the SVG contract of ICN-R01', () => {
- const out = R.render(FLAT, S79, PAL);
- expect(validate(out, 4096).filter((f) => f.level === 'error')).toEqual([]);
- });
- test('ink and deep outlines; no outline, no rim, no shadow when the style has none', () => {
- expect(R.render(FLAT, { outline: 'ink', outlineWidth: 1.5 }, PAL)).toContain('stroke="#2B2B2E" stroke-width="1.5"');
- expect(R.render(FLAT, { outline: 'deep', outlineWidth: 1.75 }, PAL)).toContain(`stroke="${R.mix('#975CDC', '#5D3C85', 0.55)}"`);
- const plain = R.render(FLAT, {}, PAL);
- expect(plain).not.toContain('opacity=');
- expect(plain).not.toContain('scale(');
- });
- test('the id prefix keeps two inlined icons apart', () => {
- expect(R.render(FLAT, S79, PAL, { idPrefix: 'box-' })).toContain('id="box-0"');
- });
- test('parse refuses what is not the flat form', () => {
- 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');
- expect(() => R.parse('<div/>')).toThrow();
- });
- });
- describe('snap and canonical', () => {
- test('snap: a palette colour stays, a near one goes to its hue and step', () => {
- expect(R.snap('#975CDC', PAL)).toEqual({ fill: '#975CDC', hue: 'violet', tone: 'main' });
- expect(R.snap('#9A6DD7', PAL).hue).toBe('violet');
- expect(R.snap('#FFFFFF', PAL).tone).toBe('white');
- expect(R.snap('#191919', PAL).fill).toBe('#2B2B2E');
- expect(R.snap('none', PAL)).toBeNull();
- });
- test('canonical: groups dissolved, the tile dropped, white lines on it take its colour, colours snapped', () => {
- 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>'
- + '<rect x="12" y="12" width="72" height="72" rx="14" fill="#529CCA"/>'
- + '<g fill="#9A6DD7"><circle cx="30" cy="30" r="8"/></g>'
- + '<path d="M38 28H28V68" stroke="#FFFFFF" stroke-width="7" fill="none"/>'
- + '<rect x="40" y="40" width="10" height="10" style="fill:#FFDC49"/></svg>';
- const { svg, notes } = R.canonical(src, PAL);
- expect(svg).not.toContain('<g');
- expect(svg).not.toContain('width="72"');
- expect(svg).not.toContain('style=');
- expect(svg).not.toContain('width="96"');
- expect(svg).toContain('<circle cx="30" cy="30" r="8" fill="#975CDC"/>');
- expect(svg).toContain('stroke="#2383E2"'); // the white line took the (snapped) tile colour
- expect(notes.join(' ')).toContain('tile');
- expect(R.parse(svg).els.length).toBe(3);
- });
- test('canonical keeps a big shape that is not the backmost one (a calendar body over its rings)', () => {
- 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>';
- expect(R.parse(R.canonical(src, PAL).svg).els.length).toBe(2);
- });
- test('flatColors: every step of every hue, ink, greys and white', () => {
- const c = R.flatColors(PAL);
- expect(c).toContain('#5D3C85');
- expect(c).toContain(R.mix('#975CDC', '#5D3C85', 0.42));
- expect(c).toContain('#C9C6BF');
- });
- });