|
| 1 | +import { describe, expect, mock, spyOn, test } from 'bun:test' |
| 2 | +import * as uploadFileModule from '../../../utils/upload-file' |
| 3 | +import { importDevup } from '../import-devup' |
| 4 | +import * as uploadXlsxModule from '../utils/upload-devup-xlsx' |
| 5 | + |
| 6 | +describe('import-devup (standalone file)', () => { |
| 7 | + test('returns early when theme is missing', async () => { |
| 8 | + const uploadFile = mock(() => Promise.resolve('{}')) |
| 9 | + spyOn(uploadFileModule, 'uploadFile').mockImplementation(uploadFile) |
| 10 | + await importDevup('json') |
| 11 | + expect(uploadFile).toHaveBeenCalledWith('.json') |
| 12 | + }) |
| 13 | + |
| 14 | + test('imports colors and typography from excel payload', async () => { |
| 15 | + const uploadXlsx = mock(() => |
| 16 | + Promise.resolve({ |
| 17 | + theme: { |
| 18 | + colors: { Light: { primary: '#111111' } }, |
| 19 | + typography: { |
| 20 | + heading: { |
| 21 | + fontFamily: 'Inter', |
| 22 | + fontStyle: 'italic', |
| 23 | + fontSize: '12', |
| 24 | + letterSpacing: '0.1em', |
| 25 | + lineHeight: 'normal', |
| 26 | + textTransform: 'uppercase', |
| 27 | + textDecoration: 'underline', |
| 28 | + }, |
| 29 | + }, |
| 30 | + }, |
| 31 | + }), |
| 32 | + ) |
| 33 | + spyOn(uploadXlsxModule, 'uploadDevupXlsx').mockImplementation(uploadXlsx) |
| 34 | + |
| 35 | + const setValueForMode = mock(() => {}) |
| 36 | + const createVariable = mock( |
| 37 | + () => |
| 38 | + ({ |
| 39 | + name: 'primary', |
| 40 | + setValueForMode, |
| 41 | + remove: mock(() => {}), |
| 42 | + }) as unknown as Variable, |
| 43 | + ) |
| 44 | + const addMode = mock((name: string) => `${name}-id`) |
| 45 | + const collection = { |
| 46 | + modes: [] as { modeId: string; name: string }[], |
| 47 | + addMode, |
| 48 | + removeMode: mock(() => {}), |
| 49 | + } as unknown as VariableCollection |
| 50 | + |
| 51 | + const createTextStyle = mock( |
| 52 | + () => |
| 53 | + ({ |
| 54 | + name: '', |
| 55 | + }) as unknown as TextStyle, |
| 56 | + ) |
| 57 | + const loadFontAsync = mock(() => Promise.resolve()) |
| 58 | + |
| 59 | + ;(globalThis as { figma?: unknown }).figma = { |
| 60 | + util: { rgba: (v: unknown) => v }, |
| 61 | + variables: { |
| 62 | + getLocalVariableCollectionsAsync: async () => [], |
| 63 | + getLocalVariablesAsync: async () => [], |
| 64 | + createVariableCollection: () => collection, |
| 65 | + createVariable, |
| 66 | + }, |
| 67 | + getLocalTextStylesAsync: async () => [], |
| 68 | + createTextStyle, |
| 69 | + loadFontAsync, |
| 70 | + } as unknown as typeof figma |
| 71 | + |
| 72 | + await importDevup('excel') |
| 73 | + |
| 74 | + expect(addMode).toHaveBeenCalledWith('Light') |
| 75 | + expect(setValueForMode).toHaveBeenCalledWith('Light-id', '#111111') |
| 76 | + expect(createTextStyle).toHaveBeenCalled() |
| 77 | + expect(loadFontAsync).toHaveBeenCalled() |
| 78 | + }) |
| 79 | + |
| 80 | + test('imports array typography and skips nulls', async () => { |
| 81 | + const uploadXlsx = mock(() => |
| 82 | + Promise.resolve({ |
| 83 | + theme: { |
| 84 | + typography: { |
| 85 | + title: [ |
| 86 | + null, |
| 87 | + { |
| 88 | + fontFamily: 'Inter', |
| 89 | + fontSize: '14', |
| 90 | + letterSpacing: '1', |
| 91 | + lineHeight: 120, |
| 92 | + }, |
| 93 | + ], |
| 94 | + }, |
| 95 | + }, |
| 96 | + }), |
| 97 | + ) |
| 98 | + spyOn(uploadXlsxModule, 'uploadDevupXlsx').mockImplementation(uploadXlsx) |
| 99 | + |
| 100 | + const createTextStyle = mock( |
| 101 | + () => |
| 102 | + ({ |
| 103 | + name: '', |
| 104 | + }) as unknown as TextStyle, |
| 105 | + ) |
| 106 | + const loadFontAsync = mock(() => Promise.resolve()) |
| 107 | + |
| 108 | + ;(globalThis as { figma?: unknown }).figma = { |
| 109 | + util: { rgba: (v: unknown) => v }, |
| 110 | + variables: { |
| 111 | + getLocalVariableCollectionsAsync: async () => [], |
| 112 | + getLocalVariablesAsync: async () => [], |
| 113 | + createVariableCollection: () => |
| 114 | + ({ |
| 115 | + modes: [], |
| 116 | + addMode: mock(() => 'm1'), |
| 117 | + removeMode: mock(() => {}), |
| 118 | + }) as unknown as VariableCollection, |
| 119 | + createVariable: mock( |
| 120 | + () => |
| 121 | + ({ |
| 122 | + name: 'primary', |
| 123 | + setValueForMode: mock(() => {}), |
| 124 | + remove: mock(() => {}), |
| 125 | + }) as unknown as Variable, |
| 126 | + ), |
| 127 | + }, |
| 128 | + getLocalTextStylesAsync: async () => [], |
| 129 | + createTextStyle, |
| 130 | + loadFontAsync, |
| 131 | + } as unknown as typeof figma |
| 132 | + |
| 133 | + await importDevup('excel') |
| 134 | + |
| 135 | + expect(createTextStyle).toHaveBeenCalled() |
| 136 | + expect(loadFontAsync).toHaveBeenCalled() |
| 137 | + }) |
| 138 | +}) |
0 commit comments