url-parser.test.ts
4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { describe, it, expect } from 'vitest';
import { parseFigmaUrl } from '../../src/utils/url-parser';
describe('parseFigmaUrl', () => {
it('should parse valid Figma URL with node-id', () => {
const url = 'https://www.figma.com/design/aONcu8L82l1PdcT304Q8Za/Intern?node-id=0-495';
const result = parseFigmaUrl(url);
expect(result).toEqual({
fileId: 'aONcu8L82l1PdcT304Q8Za',
name: 'intern',
nodeId: '0:495',
projectName: 'intern_0_495',
});
});
it('should convert node-id hyphens to colons', () => {
const url = 'https://www.figma.com/design/abc123/MyProject?node-id=123-456';
const result = parseFigmaUrl(url);
expect(result.nodeId).toBe('123:456');
});
it('should decode URL-encoded characters in project name (no double encoding)', () => {
const url = 'https://www.figma.com/design/abc123/My%20Project?node-id=0-1';
const result = parseFigmaUrl(url);
// The name is fully decoded and slugified, not double-encoded.
expect(result.name).toBe('my_project');
});
it('should convert project name to lowercase', () => {
const url = 'https://www.figma.com/design/abc123/MyProject?node-id=0-1';
const result = parseFigmaUrl(url);
expect(result.name).toBe('myproject');
});
it('should truncate long project names to 20 characters', () => {
const longName = 'a'.repeat(30);
const url = `https://www.figma.com/design/abc123/${longName}?node-id=0-1`;
const result = parseFigmaUrl(url);
expect(result.name).toHaveLength(20);
expect(result.name).toBe('a'.repeat(20));
});
it('should handle empty project name slot', () => {
// When the project name slot is empty but pathParts length is still >= 3
const url = 'https://www.figma.com/design/abc123?node-id=0-1';
// This will throw because fileId extraction expects at least 3 path parts
expect(() => parseFigmaUrl(url)).toThrow('Invalid Figma URL');
});
it('should throw error for URL without fileId', () => {
const url = 'https://www.figma.com/design/?node-id=0-1';
expect(() => parseFigmaUrl(url)).toThrow('Invalid Figma URL');
});
it('should throw error for URL without node-id', () => {
const url = 'https://www.figma.com/design/abc123/MyProject';
expect(() => parseFigmaUrl(url)).toThrow('Invalid Figma URL');
});
it('should throw error for invalid URL format', () => {
const url = 'not-a-valid-url';
expect(() => parseFigmaUrl(url)).toThrow('Invalid Figma URL');
});
it('should handle file URLs', () => {
const url = 'https://www.figma.com/file/abc123/MyProject?node-id=0-1';
const result = parseFigmaUrl(url);
expect(result.fileId).toBe('abc123');
expect(result.nodeId).toBe('0:1');
});
it('should handle complex node-id with multiple hyphens', () => {
const url = 'https://www.figma.com/design/abc123/MyProject?node-id=123-456-789';
const result = parseFigmaUrl(url);
expect(result.nodeId).toBe('123:456:789');
});
it('should generate correct projectName', () => {
const url = 'https://www.figma.com/design/abc123/TestProject?node-id=1-2';
const result = parseFigmaUrl(url);
expect(result.projectName).toBe('testproject_1_2');
});
it('should handle URLs with additional query parameters', () => {
const url = 'https://www.figma.com/design/abc123/MyProject?node-id=0-1&mode=design&t=xyz123';
const result = parseFigmaUrl(url);
expect(result.fileId).toBe('abc123');
expect(result.nodeId).toBe('0:1');
expect(result.name).toBe('myproject');
});
it('should handle URLs with hash fragments', () => {
const url = 'https://www.figma.com/design/abc123/MyProject?node-id=0-1#section';
const result = parseFigmaUrl(url);
expect(result.fileId).toBe('abc123');
expect(result.nodeId).toBe('0:1');
});
it('should handle proto URLs', () => {
const url = 'https://www.figma.com/proto/abc123/MyProject?node-id=0-1';
const result = parseFigmaUrl(url);
expect(result.fileId).toBe('abc123');
expect(result.name).toBe('myproject');
});
});