-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimport-devup.test.ts
More file actions
226 lines (209 loc) · 6.82 KB
/
import-devup.test.ts
File metadata and controls
226 lines (209 loc) · 6.82 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
import { afterEach, describe, expect, mock, spyOn, test } from 'bun:test'
import * as uploadFileModule from '../../../utils/upload-file'
import { importDevup } from '../import-devup'
import * as uploadXlsxModule from '../utils/upload-devup-xlsx'
describe('import-devup (standalone file)', () => {
const spies: ReturnType<typeof spyOn>[] = []
afterEach(() => {
for (const s of spies) s.mockRestore()
spies.length = 0
;(globalThis as { figma?: unknown }).figma = undefined
})
test('returns early when theme is missing', async () => {
const uploadFile = mock(() => Promise.resolve('{}'))
spies.push(
spyOn(uploadFileModule, 'uploadFile').mockImplementation(uploadFile),
)
await importDevup('json')
expect(uploadFile).toHaveBeenCalledWith('.json')
})
test('imports colors and typography from excel payload', async () => {
const uploadXlsx = mock(() =>
Promise.resolve({
theme: {
colors: { Light: { primary: '#111111' } },
typography: {
heading: {
fontFamily: 'Inter',
fontStyle: 'italic',
fontSize: '12',
letterSpacing: '0.1em',
lineHeight: 'normal',
textTransform: 'uppercase',
textDecoration: 'underline',
},
},
},
}),
)
spies.push(
spyOn(uploadXlsxModule, 'uploadDevupXlsx').mockImplementation(uploadXlsx),
)
const setValueForMode = mock(() => {})
const createVariable = mock(
() =>
({
name: 'primary',
setValueForMode,
remove: mock(() => {}),
}) as unknown as Variable,
)
const addMode = mock((name: string) => `${name}-id`)
const getLocalVariablesAsync = mock(() => Promise.resolve([] as Variable[]))
const collection = {
modes: [] as { modeId: string; name: string }[],
addMode,
removeMode: mock(() => {}),
} as unknown as VariableCollection
const createTextStyle = mock(
() =>
({
name: '',
}) as unknown as TextStyle,
)
const loadFontAsync = mock(() => Promise.resolve())
;(globalThis as { figma?: unknown }).figma = {
util: { rgba: (v: unknown) => v },
variables: {
getLocalVariableCollectionsAsync: async () => [],
getLocalVariablesAsync,
createVariableCollection: () => collection,
createVariable,
},
getLocalTextStylesAsync: async () => [],
getLocalEffectStylesAsync: async () => [],
createTextStyle,
createEffectStyle: () => ({ name: '', effects: [] }),
loadFontAsync,
} as unknown as typeof figma
await importDevup('excel')
expect(addMode).toHaveBeenCalledWith('Light')
expect(getLocalVariablesAsync).toHaveBeenCalledTimes(1)
expect(setValueForMode).toHaveBeenCalledWith('Light-id', '#111111')
expect(createTextStyle).toHaveBeenCalled()
expect(loadFontAsync).toHaveBeenCalled()
})
test('removes only variables from the Devup color collection', async () => {
const uploadXlsx = mock(() =>
Promise.resolve({
theme: {
colors: { Light: { primary: '#111111' } },
},
}),
)
spies.push(
spyOn(uploadXlsxModule, 'uploadDevupXlsx').mockImplementation(uploadXlsx),
)
const removeDevupVariable = mock(() => {})
const removeOtherVariable = mock(() => {})
const devupVariable = {
id: 'devup-primary',
name: 'legacy',
setValueForMode: mock(() => {}),
remove: removeDevupVariable,
} as unknown as Variable
const otherCollectionVariable = {
id: 'other-primary',
name: 'legacy',
setValueForMode: mock(() => {}),
remove: removeOtherVariable,
} as unknown as Variable
const createdVariable = {
id: 'created-primary',
name: 'primary',
setValueForMode: mock(() => {}),
remove: mock(() => {}),
} as unknown as Variable
const createVariable = mock(() => createdVariable)
const collection = {
variableIds: ['devup-primary'],
modes: [{ modeId: 'light-id', name: 'Light' }],
addMode: mock(() => 'light-id'),
removeMode: mock(() => {}),
} as unknown as VariableCollection
;(globalThis as { figma?: unknown }).figma = {
util: { rgba: (v: unknown) => v },
variables: {
getLocalVariableCollectionsAsync: async () => [collection],
getLocalVariablesAsync: async () => [
devupVariable,
otherCollectionVariable,
],
createVariableCollection: () => collection,
createVariable,
},
getLocalTextStylesAsync: async () => [],
getLocalEffectStylesAsync: async () => [],
createTextStyle: mock(
() =>
({
name: '',
}) as unknown as TextStyle,
),
createEffectStyle: () => ({ name: '', effects: [] }),
loadFontAsync: mock(() => Promise.resolve()),
} as unknown as typeof figma
await importDevup('excel')
expect(removeDevupVariable).toHaveBeenCalledTimes(1)
expect(removeOtherVariable).not.toHaveBeenCalled()
expect(createVariable).toHaveBeenCalledWith('primary', collection, 'COLOR')
})
test('imports array typography and skips nulls', async () => {
const uploadXlsx = mock(() =>
Promise.resolve({
theme: {
typography: {
title: [
null,
{
fontFamily: 'Inter',
fontSize: '14',
letterSpacing: '1',
lineHeight: 120,
},
],
},
},
}),
)
spies.push(
spyOn(uploadXlsxModule, 'uploadDevupXlsx').mockImplementation(uploadXlsx),
)
const createTextStyle = mock(
() =>
({
name: '',
}) as unknown as TextStyle,
)
const loadFontAsync = mock(() => Promise.resolve())
;(globalThis as { figma?: unknown }).figma = {
util: { rgba: (v: unknown) => v },
variables: {
getLocalVariableCollectionsAsync: async () => [],
getLocalVariablesAsync: async () => [],
createVariableCollection: () =>
({
modes: [],
addMode: mock(() => 'm1'),
removeMode: mock(() => {}),
}) as unknown as VariableCollection,
createVariable: mock(
() =>
({
name: 'primary',
setValueForMode: mock(() => {}),
remove: mock(() => {}),
}) as unknown as Variable,
),
},
getLocalTextStylesAsync: async () => [],
getLocalEffectStylesAsync: async () => [],
createTextStyle,
createEffectStyle: () => ({ name: '', effects: [] }),
loadFontAsync,
} as unknown as typeof figma
await importDevup('excel')
expect(createTextStyle).toHaveBeenCalled()
expect(loadFontAsync).toHaveBeenCalled()
})
})