-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexportComponents.test.ts
More file actions
255 lines (240 loc) · 6.62 KB
/
exportComponents.test.ts
File metadata and controls
255 lines (240 loc) · 6.62 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import {
afterAll,
afterEach,
beforeEach,
describe,
expect,
mock,
spyOn,
test,
} from 'bun:test'
import * as downloadFileModule from '../../utils/download-file'
import { exportComponents } from '../exportComponents'
const zipFileMock = mock(
(_name: string, _data: unknown, _options?: unknown) => {},
)
const zipGenerateAsyncMock = mock((_options?: unknown) =>
Promise.resolve(new Uint8Array([1, 2, 3])),
)
// mock jszip
mock.module('jszip', () => ({
default: class JSZipMock {
files: Record<string, unknown> = {}
file(name: string, data: unknown, options?: unknown) {
zipFileMock(name, data, options)
this.files[name] = data
}
async generateAsync(options?: unknown) {
return zipGenerateAsyncMock(options)
}
},
}))
const runMock = mock(() => Promise.resolve())
const getComponentsCodesMock = mock(
(): ReadonlyArray<readonly [string, string]> => [],
)
mock.module('../codegen/Codegen', () => ({
Codegen: class {
node: SceneNode
constructor(node: SceneNode) {
this.node = node
}
run = runMock
getComponentsCodes = getComponentsCodesMock
},
}))
const downloadFileMock = mock(() => Promise.resolve(undefined))
beforeEach(() => {
spyOn(downloadFileModule, 'downloadFile').mockImplementation(downloadFileMock)
})
afterAll(() => {
spyOn(downloadFileModule, 'downloadFile').mockRestore()
})
function createNode(
type: SceneNode['type'],
{
characters,
children,
textStyleId,
name,
fills,
parent,
layoutPositioning = 'AUTO',
layoutSizingHorizontal,
styledTextSegments = [],
variantProperties,
visible = true,
...props
}: {
[_: string]: unknown
characters?: string
name?: string
textStyleId?: string
children?: SceneNode[]
layoutPositioning?: string
styledTextSegments?: unknown[]
variantProperties?: Record<string, string>
} = {},
): SceneNode {
const ret = {
type,
getCSSAsync: async () => props,
exportAsync: async () => '<svg>\n<path/>\n</svg>',
getStyledTextSegments: () => styledTextSegments,
layoutSizingHorizontal,
textStyleId,
parent,
characters,
visible,
layoutPositioning,
width: props.width ? parseInt(props.width as string, 10) : undefined,
height: props.height ? parseInt(props.height as string, 10) : undefined,
name,
fills,
variantProperties,
children: children ?? [],
} as unknown as SceneNode
const retWithChildren = ret as SceneNode & { children: SceneNode[] }
for (const child of retWithChildren.children) {
const childWithParent = child as Omit<SceneNode, 'parent'> & {
parent?: SceneNode
}
childWithParent.parent = ret as SceneNode & { parent?: SceneNode }
}
return ret
}
const notifyMock = mock(() => {})
const showUIMock = mock(() => {})
const postMessageMock = mock(() => {})
describe('exportComponents', () => {
beforeEach(() => {
;(globalThis as { figma?: unknown }).figma = {
currentPage: {
selection: [],
name: 'TestPage',
},
notify: notifyMock,
showUI: showUIMock,
ui: { postMessage: postMessageMock, onmessage: null },
} as unknown as typeof figma
notifyMock.mockClear()
})
afterEach(() => {
notifyMock.mockClear()
showUIMock.mockClear()
postMessageMock.mockClear()
downloadFileMock.mockClear()
runMock.mockClear()
getComponentsCodesMock.mockClear()
zipFileMock.mockClear()
zipGenerateAsyncMock.mockClear()
})
test('should notify and return if no components found', async () => {
const node = createNode('RECTANGLE', {
fills: [],
})
;(
(globalThis as { figma?: { currentPage?: { selection?: SceneNode[] } } })
.figma?.currentPage as { selection: SceneNode[] }
).selection = [node]
getComponentsCodesMock.mockReturnValueOnce([])
await exportComponents()
expect(notifyMock).toHaveBeenCalledWith('No components found')
})
test('should not export components if all children are invisible', async () => {
const node = createNode('GROUP', {
fills: [],
children: [
createNode('VECTOR', {
fills: [],
visible: false,
}),
],
})
;(
(globalThis as { figma?: { currentPage?: { selection?: SceneNode[] } } })
.figma?.currentPage as { selection: SceneNode[] }
).selection = [node]
getComponentsCodesMock.mockReturnValueOnce([])
await exportComponents()
expect(downloadFileMock).not.toHaveBeenCalled()
})
test('should export components and call downloadFile', async () => {
const node = createNode('COMPONENT', {
fills: [],
name: 'Component',
children: [
createNode('VECTOR', {
fills: [],
width: '100px',
height: '100px',
}),
createNode('RECTANGLE', {
fills: [
{
type: 'IMAGE',
imageRef: {
id: '123',
},
scaleMode: 'FILL',
},
],
background: 'red',
width: '100px',
height: '100px',
name: 'image.png',
}),
],
})
;(
(globalThis as { figma?: { currentPage?: { selection?: SceneNode[] } } })
.figma?.currentPage as { selection: SceneNode[] }
).selection = [node]
getComponentsCodesMock.mockReturnValueOnce([
['Component.tsx', '<Component />'],
])
await exportComponents()
expect(downloadFileMock).toHaveBeenCalledWith(
'TestPage.zip',
expect.any(Uint8Array),
)
expect(zipFileMock).toHaveBeenCalledWith(
expect.any(String),
expect.any(String),
{ compression: 'DEFLATE' },
)
expect(zipGenerateAsyncMock).toHaveBeenCalledWith({
type: 'uint8array',
compression: 'DEFLATE',
compressionOptions: { level: 1 },
streamFiles: true,
})
expect(notifyMock).toHaveBeenCalledWith(
'Components exported',
expect.any(Object),
)
})
test('should raise error', async () => {
const node = createNode('COMPONENT', {
children: [
createNode('RECTANGLE', {
fills: [],
}),
],
})
;(
(globalThis as { figma?: { currentPage?: { selection?: SceneNode[] } } })
.figma?.currentPage as { selection: SceneNode[] }
).selection = [node]
getComponentsCodesMock.mockImplementation(() => {
throw new Error('boom')
})
const consoleErrorSpy = spyOn(console, 'error').mockImplementation(() => {})
await exportComponents()
expect(notifyMock).toHaveBeenCalledWith('Error exporting components', {
timeout: 3000,
error: true,
})
consoleErrorSpy.mockRestore()
})
})