Skip to content

Commit 123b6b8

Browse files
committed
Add case
1 parent 319f50e commit 123b6b8

File tree

2 files changed

+58
-15
lines changed

2 files changed

+58
-15
lines changed

src/commands/devup/__tests__/index.test.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,56 @@ describe('devup commands', () => {
370370
expect(Array.isArray(parsed.theme?.typography?.heading)).toBe(true)
371371
})
372372

373+
test('exportDevup filters out empty typography entries', async () => {
374+
getColorCollectionSpy = spyOn(
375+
getColorCollectionModule,
376+
'getDevupColorCollection',
377+
).mockResolvedValue(null)
378+
styleNameToTypographySpy = spyOn(
379+
styleNameToTypographyModule,
380+
'styleNameToTypography',
381+
).mockReturnValue({ level: 0, name: 'heading' })
382+
textSegmentToTypographySpy = spyOn(
383+
textSegmentToTypographyModule,
384+
'textSegmentToTypography',
385+
).mockReturnValue(null)
386+
387+
;(globalThis as { figma?: unknown }).figma = {
388+
util: { rgba: (v: unknown) => v },
389+
variables: {},
390+
loadAllPagesAsync: async () => {},
391+
getLocalTextStylesAsync: async () => [
392+
{
393+
id: 'id',
394+
name: 'heading/1',
395+
fontName: { family: 'Inter', style: 'Regular' },
396+
} as unknown as TextStyle,
397+
],
398+
root: {
399+
findAllWithCriteria: () => [
400+
{
401+
textStyleId: 'id',
402+
getStyledTextSegments: () => [{ textStyleId: 'id' }],
403+
} as unknown as TextNode,
404+
],
405+
},
406+
getStyleByIdAsync: async () =>
407+
({
408+
id: 'id',
409+
name: 'heading/1',
410+
fontName: { family: 'Inter', style: 'Regular' },
411+
}) as unknown as TextStyle,
412+
} as unknown as typeof figma
413+
414+
await exportDevup('json')
415+
416+
const payload = JSON.parse(
417+
(downloadFileMock.mock.calls[0] as unknown as { args: [string, string] })
418+
?.args?.[1] ?? '{}',
419+
)
420+
expect(payload.theme?.typography).toBeUndefined()
421+
})
422+
373423
test('importDevup creates colors and typography from json', async () => {
374424
getColorCollectionSpy = spyOn(
375425
getColorCollectionModule,

src/commands/devup/index.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -120,27 +120,20 @@ export async function exportDevup(
120120
devup.theme.typography = Object.entries(typography).reduce(
121121
(acc, [key, value]) => {
122122
const filtered = value.filter((v) => v !== null)
123-
if (filtered.length === 0) return acc
123+
if (filtered.length === 0) {
124+
return acc
125+
}
124126
if (filtered.length === 1) {
125127
acc[key] = filtered[0]
126128
return acc
127129
}
128130
if (value[0] === null) {
129131
acc[key] = [filtered[0]]
130-
let init = false
131-
for (let i = 0; i < value.length; i += 1) {
132-
if (value[i] === null) {
133-
if (init) {
134-
acc[key].push(null)
135-
} else {
136-
if (!init) {
137-
acc[key].push(null)
138-
init = true
139-
} else {
140-
acc[key].push(value[i])
141-
}
142-
}
143-
}
132+
for (let i = 1; i < value.length; i += 1) {
133+
acc[key].push(value[i])
134+
}
135+
while (acc[key][acc[key].length - 1] === null) {
136+
acc[key].pop()
144137
}
145138
return acc
146139
}

0 commit comments

Comments
 (0)