Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
247 changes: 247 additions & 0 deletions src/commands/devup/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,253 @@ describe('devup commands', () => {
)
})

test('exportDevup treeshake true handles mixed-style text nodes', async () => {
getColorCollectionSpy = spyOn(
getColorCollectionModule,
'getDevupColorCollection',
).mockResolvedValue(null)
styleNameToTypographySpy = spyOn(
styleNameToTypographyModule,
'styleNameToTypography',
).mockReturnValue({ level: 0, name: 'heading' })
textStyleToTypographySpy = spyOn(
textStyleToTypographyModule,
'textStyleToTypography',
).mockReturnValue({ fontFamily: 'Inter' } as unknown as DevupTypography)

const mixedSymbol = Symbol('mixed')
const mixedTextNode = {
type: 'TEXT',
textStyleId: mixedSymbol,
getStyledTextSegments: () => [
{ textStyleId: 'style1' },
{ textStyleId: 'style2' },
],
} as unknown as TextNode

;(globalThis as { figma?: unknown }).figma = {
util: { rgba: (v: unknown) => v },
loadAllPagesAsync: async () => {},
getLocalTextStylesAsync: async () => [
{ id: 'style1', name: 'heading/1' } as unknown as TextStyle,
],
root: { findAllWithCriteria: () => [mixedTextNode] },
mixed: mixedSymbol,
variables: { getVariableByIdAsync: async () => null },
} as unknown as typeof figma

await exportDevup('json', true)

expect(downloadFileMock).toHaveBeenCalledWith(
'devup.json',
expect.stringContaining('"typography"'),
)
})

test('exportDevup treeshake true stops within current page subtree and skips later pages', async () => {
getColorCollectionSpy = spyOn(
getColorCollectionModule,
'getDevupColorCollection',
).mockResolvedValue(null)
styleNameToTypographySpy = spyOn(
styleNameToTypographyModule,
'styleNameToTypography',
).mockImplementation((name: string) =>
name.includes('2')
? ({ level: 1, name: 'heading' } as const)
: ({ level: 0, name: 'heading' } as const),
)
textStyleToTypographySpy = spyOn(
textStyleToTypographyModule,
'textStyleToTypography',
).mockReturnValue({ fontFamily: 'Inter' } as unknown as DevupTypography)

const currentTextNode = {
type: 'TEXT',
textStyleId: 'style1',
getStyledTextSegments: () => [{ textStyleId: 'style1' }],
} as unknown as TextNode
const firstSectionFindAllWithCriteria = mock(() => [currentTextNode])
const secondSectionFindAllWithCriteria = mock(() => [])
const otherPageLoadAsync = mock(async () => {})
const firstSection = {
type: 'SECTION',
findAllWithCriteria: firstSectionFindAllWithCriteria,
} as unknown as SectionNode
const secondSection = {
type: 'SECTION',
findAllWithCriteria: secondSectionFindAllWithCriteria,
} as unknown as SectionNode
const currentPage = {
id: 'page-current',
children: [firstSection, secondSection],
} as unknown as PageNode
const otherPage = {
id: 'page-other',
children: [],
loadAsync: otherPageLoadAsync,
} as unknown as PageNode

;(globalThis as { figma?: unknown }).figma = {
util: { rgba: (v: unknown) => v },
currentPage,
getLocalTextStylesAsync: async () => [
{ id: 'style1', name: 'heading/1' } as unknown as TextStyle,
{ id: 'style2', name: 'heading/2' } as unknown as TextStyle,
],
root: {
children: [otherPage, currentPage],
},
mixed: Symbol('mixed'),
variables: { getVariableByIdAsync: async () => null },
} as unknown as typeof figma

await exportDevup('json', true)

expect(firstSectionFindAllWithCriteria).toHaveBeenCalledTimes(1)
expect(secondSectionFindAllWithCriteria).not.toHaveBeenCalled()
expect(otherPageLoadAsync).not.toHaveBeenCalled()
expect(downloadFileMock).toHaveBeenCalledWith(
'devup.json',
expect.stringContaining('"typography"'),
)
})

test('exportDevup treeshake true lazily loads later pages when needed', async () => {
getColorCollectionSpy = spyOn(
getColorCollectionModule,
'getDevupColorCollection',
).mockResolvedValue(null)
styleNameToTypographySpy = spyOn(
styleNameToTypographyModule,
'styleNameToTypography',
).mockImplementation((name: string) =>
name.includes('2')
? ({ level: 1, name: 'body' } as const)
: ({ level: 0, name: 'heading' } as const),
)
textStyleToTypographySpy = spyOn(
textStyleToTypographyModule,
'textStyleToTypography',
).mockReturnValue({ fontFamily: 'Inter' } as unknown as DevupTypography)

const currentSectionFindAllWithCriteria = mock(() => [])
const otherTextNode = {
type: 'TEXT',
textStyleId: 'style2',
getStyledTextSegments: () => [{ textStyleId: 'style2' }],
} as unknown as TextNode
const otherSectionFindAllWithCriteria = mock(() => [otherTextNode])
const otherPageLoadAsync = mock(async () => {})
const currentPage = {
id: 'page-current',
children: [
{
type: 'SECTION',
findAllWithCriteria: currentSectionFindAllWithCriteria,
} as unknown as SectionNode,
],
} as unknown as PageNode
const otherPage = {
id: 'page-other',
children: [
{
type: 'SECTION',
findAllWithCriteria: otherSectionFindAllWithCriteria,
} as unknown as SectionNode,
],
loadAsync: otherPageLoadAsync,
} as unknown as PageNode

;(globalThis as { figma?: unknown }).figma = {
util: { rgba: (v: unknown) => v },
currentPage,
getLocalTextStylesAsync: async () => [
{ id: 'style1', name: 'heading/1' } as unknown as TextStyle,
{ id: 'style2', name: 'body/2' } as unknown as TextStyle,
],
root: {
children: [currentPage, otherPage],
},
mixed: Symbol('mixed'),
variables: { getVariableByIdAsync: async () => null },
} as unknown as typeof figma

await exportDevup('json', true)

expect(currentSectionFindAllWithCriteria).toHaveBeenCalledTimes(1)
expect(otherPageLoadAsync).toHaveBeenCalledTimes(1)
expect(otherSectionFindAllWithCriteria).toHaveBeenCalledTimes(1)
expect(downloadFileMock).toHaveBeenCalledWith(
'devup.json',
expect.stringContaining('"typography"'),
)
})

test('exportDevup treeshake true handles direct text children and recursive fallback nodes', async () => {
getColorCollectionSpy = spyOn(
getColorCollectionModule,
'getDevupColorCollection',
).mockResolvedValue(null)
styleNameToTypographySpy = spyOn(
styleNameToTypographyModule,
'styleNameToTypography',
).mockImplementation((name: string) =>
name.includes('2')
? ({ level: 1, name: 'body' } as const)
: ({ level: 0, name: 'heading' } as const),
)
textStyleToTypographySpy = spyOn(
textStyleToTypographyModule,
'textStyleToTypography',
).mockImplementation(
(style: TextStyle) => ({ id: style.id }) as unknown as DevupTypography,
)

const directTextNode = {
type: 'TEXT',
textStyleId: 'style1',
getStyledTextSegments: () => [{ textStyleId: 'style1' }],
} as unknown as TextNode
const nestedTextNode = {
type: 'TEXT',
textStyleId: 'style2',
getStyledTextSegments: () => [{ textStyleId: 'style2' }],
} as unknown as TextNode
const recursiveNode = {
type: 'GROUP',
children: [nestedTextNode],
} as unknown as GroupNode
const currentPage = {
id: 'page-current',
children: [directTextNode, recursiveNode],
} as unknown as PageNode

;(globalThis as { figma?: unknown }).figma = {
util: { rgba: (v: unknown) => v },
currentPage,
getLocalTextStylesAsync: async () => [
{ id: 'style1', name: 'heading/1' } as unknown as TextStyle,
{ id: 'style2', name: 'body/2' } as unknown as TextStyle,
],
root: {
children: [currentPage],
},
mixed: Symbol('mixed'),
variables: { getVariableByIdAsync: async () => null },
} as unknown as typeof figma

await exportDevup('json', true)

const firstCall = downloadFileMock.mock.calls[0] as unknown[] | undefined
const data = (firstCall?.[1] as string) ?? '{}'
const parsed = JSON.parse(data) as {
theme?: { typography?: Record<string, unknown> }
}
expect(parsed.theme?.typography?.heading).toBeDefined()
expect(parsed.theme?.typography?.body).toBeDefined()
})

test('exportDevup fills missing typography levels from styles map', async () => {
getColorCollectionSpy = spyOn(
getColorCollectionModule,
Expand Down
Loading
Loading