-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.test.ts
More file actions
235 lines (214 loc) · 7.47 KB
/
utils.test.ts
File metadata and controls
235 lines (214 loc) · 7.47 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import { afterEach, describe, expect, it, test } from 'bun:test'
import {
getComponentName,
propsToPropsWithTypography,
resetTextStyleCache,
space,
} from '../utils'
// Minimal figma global for propsToPropsWithTypography tests
if (!(globalThis as { figma?: unknown }).figma) {
;(globalThis as { figma?: unknown }).figma = {
getLocalTextStylesAsync: () => Promise.resolve([]),
getStyleByIdAsync: () => Promise.resolve(null),
} as unknown as typeof figma
}
describe('space', () => {
it('should create space', () => {
expect(space(0)).toEqual('')
expect(space(1)).toEqual(' ')
expect(space(2)).toEqual(' ')
})
})
describe('propsToPropsWithTypography', () => {
afterEach(() => {
resetTextStyleCache()
})
it('should apply typography from resolved cache (sync fast path)', async () => {
const origGetLocal = figma.getLocalTextStylesAsync
const origGetStyle = figma.getStyleByIdAsync
figma.getLocalTextStylesAsync = () =>
Promise.resolve([{ id: 'ts-1' } as unknown as TextStyle]) as ReturnType<
typeof figma.getLocalTextStylesAsync
>
figma.getStyleByIdAsync = (id: string) =>
Promise.resolve(
id === 'ts-1'
? ({ id: 'ts-1', name: 'mobile/Body' } as unknown as BaseStyle)
: null,
) as ReturnType<typeof figma.getStyleByIdAsync>
// First call: populates async caches + resolved caches via .then()
const r1 = await propsToPropsWithTypography(
{ fontFamily: 'Arial', fontSize: 16, w: 100, h: 50 },
'ts-1',
)
expect(r1.typography).toBe('body')
expect(r1.fontFamily).toBeUndefined()
expect(r1.w).toBeUndefined()
// Second call: hits sync resolved-value cache (lines 71-72)
const r2 = await propsToPropsWithTypography(
{ fontFamily: 'Inter', fontSize: 14, w: 200, h: 60 },
'ts-1',
)
expect(r2.typography).toBe('body')
expect(r2.fontFamily).toBeUndefined()
expect(r2.w).toBeUndefined()
figma.getLocalTextStylesAsync = origGetLocal
figma.getStyleByIdAsync = origGetStyle
})
it('should preserve scoped (non-breakpoint) prefix in typography key', async () => {
const origGetLocal = figma.getLocalTextStylesAsync
const origGetStyle = figma.getStyleByIdAsync
figma.getLocalTextStylesAsync = () =>
Promise.resolve([{ id: 'ts-cms' } as unknown as TextStyle]) as ReturnType<
typeof figma.getLocalTextStylesAsync
>
figma.getStyleByIdAsync = (id: string) =>
Promise.resolve(
id === 'ts-cms'
? ({
id: 'ts-cms',
name: 'cms/bodyLgBold',
} as unknown as BaseStyle)
: null,
) as ReturnType<typeof figma.getStyleByIdAsync>
// Async path — "cms/" is not a breakpoint, so the full name must be
// converted to camelCase to match the devup.json export key.
const r1 = await propsToPropsWithTypography(
{ fontFamily: 'Arial', fontSize: 18 },
'ts-cms',
)
expect(r1.typography).toBe('cmsBodyLgBold')
expect(r1.fontFamily).toBeUndefined()
expect(r1.fontSize).toBeUndefined()
// Sync fast path — same assertion via the resolved cache branch.
const r2 = await propsToPropsWithTypography(
{ fontFamily: 'Inter', fontSize: 20 },
'ts-cms',
)
expect(r2.typography).toBe('cmsBodyLgBold')
expect(r2.fontFamily).toBeUndefined()
figma.getLocalTextStylesAsync = origGetLocal
figma.getStyleByIdAsync = origGetStyle
})
it('should return early from sync path when textStyleId not in resolved set', async () => {
const origGetLocal = figma.getLocalTextStylesAsync
const origGetStyle = figma.getStyleByIdAsync
figma.getLocalTextStylesAsync = () =>
Promise.resolve([{ id: 'ts-1' } as unknown as TextStyle]) as ReturnType<
typeof figma.getLocalTextStylesAsync
>
figma.getStyleByIdAsync = () =>
Promise.resolve(null) as ReturnType<typeof figma.getStyleByIdAsync>
// First call: populates resolved cache
await propsToPropsWithTypography(
{ fontFamily: 'Arial', w: 100, h: 50 },
'ts-1',
)
// Second call with unknown textStyleId — hits else branch (lines 75-76)
const r = await propsToPropsWithTypography(
{ fontFamily: 'Inter', w: 200, h: 60 },
'ts-unknown',
)
expect(r.fontFamily).toBe('Inter')
expect(r.typography).toBeUndefined()
expect(r.w).toBeUndefined()
// Third call with empty textStyleId — also hits else branch
const r2 = await propsToPropsWithTypography(
{ fontFamily: 'Mono', w: 300, h: 70 },
'',
)
expect(r2.fontFamily).toBe('Mono')
expect(r2.typography).toBeUndefined()
figma.getLocalTextStylesAsync = origGetLocal
figma.getStyleByIdAsync = origGetStyle
})
it('should return props without typography when style resolves to null', async () => {
const origGetLocal = figma.getLocalTextStylesAsync
const origGetStyle = figma.getStyleByIdAsync
figma.getLocalTextStylesAsync = () =>
Promise.resolve([
{ id: 'ts-null' } as unknown as TextStyle,
]) as ReturnType<typeof figma.getLocalTextStylesAsync>
figma.getStyleByIdAsync = () =>
Promise.resolve(null) as ReturnType<typeof figma.getStyleByIdAsync>
// First call: populates caches, style is null
const r1 = await propsToPropsWithTypography(
{ fontFamily: 'Arial', fontSize: 16, w: 100, h: 50 },
'ts-null',
)
expect(r1.typography).toBeUndefined()
expect(r1.fontFamily).toBe('Arial')
expect(r1.w).toBeUndefined()
// Second call: sync path, styleByIdResolved has null → style is falsy, skip applyTypography
const r2 = await propsToPropsWithTypography(
{ fontFamily: 'Inter', fontSize: 14, w: 200, h: 60 },
'ts-null',
)
expect(r2.typography).toBeUndefined()
expect(r2.fontFamily).toBe('Inter')
expect(r2.w).toBeUndefined()
figma.getLocalTextStylesAsync = origGetLocal
figma.getStyleByIdAsync = origGetStyle
})
})
describe('getComponentName', () => {
test.each([
{
description: 'should return pascal case name for COMPONENT_SET',
node: {
type: 'COMPONENT_SET',
name: 'button-component',
} as unknown as SceneNode,
expected: 'ButtonComponent',
},
{
description:
'should return parent name for COMPONENT with COMPONENT_SET parent',
node: {
type: 'COMPONENT',
name: 'button-variant',
parent: {
type: 'COMPONENT_SET',
name: 'button-set',
} as unknown as SceneNode,
} as unknown as SceneNode,
expected: 'ButtonSet',
},
{
description:
'should return node name for COMPONENT without COMPONENT_SET parent',
node: {
type: 'COMPONENT',
name: 'button-component',
parent: null,
} as unknown as SceneNode,
expected: 'ButtonComponent',
},
{
description: 'should return pascal case name for FRAME',
node: {
type: 'FRAME',
name: 'my-frame',
} as unknown as SceneNode,
expected: 'MyFrame',
},
{
description: 'should return pascal case name for RECTANGLE',
node: {
type: 'RECTANGLE',
name: 'my-rectangle',
} as unknown as SceneNode,
expected: 'MyRectangle',
},
{
description: 'should return pascal case name for TEXT',
node: {
type: 'TEXT',
name: 'my-text',
} as unknown as SceneNode,
expected: 'MyText',
},
])('$description', ({ node, expected }) => {
expect(getComponentName(node)).toBe(expected)
})
})