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
66 changes: 66 additions & 0 deletions src/commands/devup/__tests__/import-devup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,72 @@ describe('import-devup (standalone file)', () => {
expect(loadFontAsync).toHaveBeenCalled()
})

test('removes only variables from the Devup color collection', async () => {
const uploadXlsx = mock(() =>
Promise.resolve({
theme: {
colors: { Light: { primary: '#111111' } },
},
}),
)
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 () => [],
createTextStyle: mock(
() =>
({
name: '',
}) as unknown as TextStyle,
),
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({
Expand Down
14 changes: 12 additions & 2 deletions src/commands/devup/import-devup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ async function importColors(devup: Devup) {
(await getDevupColorCollection()) ??
(await figma.variables.createVariableCollection('Devup Colors'))
const variables = await figma.variables.getLocalVariablesAsync()
const collectionVariableIds = new Set(collection.variableIds)
const variablesByName = new Map<string, Variable>()
for (const variable of variables) {
if (!variablesByName.has(variable.name)) {
if (
collectionVariableIds.has(variable.id) &&
!variablesByName.has(variable.name)
) {
variablesByName.set(variable.name, variable)
}
}
Expand All @@ -51,6 +55,7 @@ async function importColors(devup: Devup) {
variable = figma.variables.createVariable(colorKey, collection, 'COLOR')
variablesByName.set(colorKey, variable)
variables.push(variable)
collectionVariableIds.add(variable.id)
}

variable.setValueForMode(modeId, figma.util.rgba(colorValue))
Expand All @@ -66,7 +71,12 @@ async function importColors(devup: Devup) {
}

for (const variable of variables) {
if (colorNames.has(variable.name)) continue
if (
!collectionVariableIds.has(variable.id) ||
colorNames.has(variable.name)
) {
continue
}
variable.remove()
}
}
Expand Down