export-svg-typescript 0.1.136 → 0.1.138

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/test/export.test.js +126 -0
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "module": "./export-svg-typescript.js",
4
4
  "author": "vtempest",
5
5
  "license": "MIT",
6
- "version": "0.1.136",
6
+ "version": "0.1.138",
7
7
  "description": "Export SVG to TypeScript",
8
8
  "type": "module",
9
9
  "scripts": {
@@ -0,0 +1,126 @@
1
+ import fs from "node:fs";
2
+ import os from "node:os";
3
+ import path from "node:path";
4
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
5
+
6
+ let dir;
7
+ let origArgv;
8
+
9
+ const SMALL = '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24"><path fill="#ff0000" d="M0 0"/></svg>';
10
+
11
+ async function load(argv) {
12
+ vi.resetModules();
13
+ process.argv = ["node", "export-svg-typescript.js", ...argv];
14
+ return import("../export-svg-typescript.js");
15
+ }
16
+
17
+ beforeEach(() => {
18
+ dir = fs.mkdtempSync(path.join(os.tmpdir(), "svg-export-"));
19
+ origArgv = process.argv;
20
+ vi.spyOn(console, "log").mockImplementation(() => {});
21
+ });
22
+
23
+ afterEach(() => {
24
+ process.argv = origArgv;
25
+ fs.rmSync(dir, { recursive: true, force: true });
26
+ vi.restoreAllMocks();
27
+ });
28
+
29
+ describe("convertSVGFolderToExportIndex", () => {
30
+ it("generates one customizable export per .svg and skips other files", async () => {
31
+ const input = path.join(dir, "svg");
32
+ fs.mkdirSync(input);
33
+ fs.writeFileSync(path.join(input, "icon-arrow-left.svg"), SMALL);
34
+ fs.writeFileSync(path.join(input, "Star_Big.SVG"), SMALL);
35
+ fs.writeFileSync(path.join(input, "notes.txt"), "ignore me");
36
+ const out = path.join(dir, "index.ts");
37
+
38
+ await load(["-i", input, "-o", out]);
39
+
40
+ const src = fs.readFileSync(out, "utf8");
41
+ expect(src).toContain("Do a Barrel Roll");
42
+ expect(src).toContain("export const iconArrowLeft = (options: LoadingOptions = {}) => customSVG(options,");
43
+ // The extension is stripped case-sensitively, so ".SVG" ends up in the name.
44
+ expect(src).toContain("export const starBigSvg =");
45
+ expect(src).not.toContain("notes");
46
+ expect(src).toContain("Returns a customized SVG string for icon arrow-left");
47
+ expect(src).toContain("(default: 24)");
48
+ expect(src).toContain("data:image/svg+xml;base64,");
49
+ expect(src).toContain("function customSVG(");
50
+ expect(src).toContain("interface LoadingOptions");
51
+ expect(console.log).toHaveBeenCalledWith(expect.stringContaining("Converted 2 SVG files"));
52
+ });
53
+
54
+ it("falls back to 100x100 defaults and a resized preview when width is missing", async () => {
55
+ const input = path.join(dir, "in");
56
+ fs.mkdirSync(input);
57
+ fs.writeFileSync(path.join(input, "plain.svg"), '<svg viewBox="0 0 10 10"><circle/></svg>');
58
+ const out = path.join(dir, "o.ts");
59
+
60
+ const mod = await load(["-i", input, "-o", out]);
61
+ expect(typeof mod.convertSVGFolderToExportIndex).toBe("function");
62
+
63
+ const src = fs.readFileSync(out, "utf8");
64
+ expect(src).toContain("(default: 100)");
65
+ const b64 = src.match(/base64,([A-Za-z0-9+/=]+)\)/)[1];
66
+ expect(Buffer.from(b64, "base64").toString()).toContain('<svg width="100px" height="100px"');
67
+ });
68
+
69
+ it("resizes the preview of oversized icons", async () => {
70
+ const input = path.join(dir, "big");
71
+ fs.mkdirSync(input);
72
+ fs.writeFileSync(
73
+ path.join(input, "huge.svg"),
74
+ '<svg width="800" height="600"><rect/></svg>',
75
+ );
76
+ const out = path.join(dir, "o.ts");
77
+
78
+ await load(["-i", input, "-o", out]);
79
+
80
+ const src = fs.readFileSync(out, "utf8");
81
+ expect(src).toContain("(default: 800)");
82
+ expect(src).toContain("(default: 600)");
83
+ const b64 = src.match(/base64,([A-Za-z0-9+/=]+)\)/)[1];
84
+ const preview = Buffer.from(b64, "base64").toString();
85
+ expect(preview).toMatch(/^<svg width="100px" height="100px"\s*><rect\/><\/svg>$/);
86
+ });
87
+
88
+ it("escapes backticks, dollars and collapses whitespace in the embedded markup", async () => {
89
+ const input = path.join(dir, "esc");
90
+ fs.mkdirSync(input);
91
+ fs.writeFileSync(
92
+ path.join(input, "tricky.svg"),
93
+ '<svg xmlns:xlink="http://www.w3.org/1999/xlink" width="10" height="10">\n <text>`a` $b</text>\n <g/>\n</svg>',
94
+ );
95
+ const out = path.join(dir, "o.ts");
96
+
97
+ await load(["-i", input, "-o", out]);
98
+
99
+ const src = fs.readFileSync(out, "utf8");
100
+ expect(src).toContain("\\`a\\` \\$b");
101
+ expect(src).not.toContain("xmlns:xlink");
102
+ expect(src).toContain("<svg width=\"10\" height=\"10\"><text>");
103
+ });
104
+
105
+ it("handles an empty folder", async () => {
106
+ const input = path.join(dir, "empty");
107
+ fs.mkdirSync(input);
108
+ const out = path.join(dir, "o.ts");
109
+ await load(["-i", input, "-o", out]);
110
+ expect(fs.readFileSync(out, "utf8")).toContain("customSVG");
111
+ expect(console.log).toHaveBeenCalledWith(expect.stringContaining("Converted 0 SVG files"));
112
+ });
113
+
114
+ it("defaults to ./svg and ./index.ts when no flags are given", async () => {
115
+ const cwd = process.cwd();
116
+ fs.mkdirSync(path.join(dir, "svg"));
117
+ fs.writeFileSync(path.join(dir, "svg", "a.svg"), SMALL);
118
+ process.chdir(dir);
119
+ try {
120
+ await load([]);
121
+ expect(fs.existsSync(path.join(dir, "index.ts"))).toBe(true);
122
+ } finally {
123
+ process.chdir(cwd);
124
+ }
125
+ });
126
+ });