|
| 1 | +import { afterEach, describe, expect, mock, test } from 'bun:test' |
| 2 | +import { applyTypography } from '../apply-typography' |
| 3 | + |
| 4 | +describe('applyTypography', () => { |
| 5 | + afterEach(() => { |
| 6 | + ;(globalThis as { figma?: unknown }).figma = undefined |
| 7 | + }) |
| 8 | + |
| 9 | + test('applies typography to a newly created style', async () => { |
| 10 | + const styleObj = { name: '' } as unknown as TextStyle |
| 11 | + const createTextStyle = mock(() => styleObj) |
| 12 | + const loadFontAsync = mock(() => Promise.resolve()) |
| 13 | + const notify = mock(() => {}) |
| 14 | + |
| 15 | + ;(globalThis as { figma?: unknown }).figma = { |
| 16 | + createTextStyle, |
| 17 | + loadFontAsync, |
| 18 | + notify, |
| 19 | + } as unknown as typeof figma |
| 20 | + |
| 21 | + await applyTypography( |
| 22 | + 'mobile/title', |
| 23 | + { |
| 24 | + fontFamily: 'Inter', |
| 25 | + fontStyle: 'italic', |
| 26 | + fontSize: '14', |
| 27 | + letterSpacing: '0.1em', |
| 28 | + lineHeight: 'normal', |
| 29 | + textTransform: 'uppercase', |
| 30 | + textDecoration: 'underline', |
| 31 | + }, |
| 32 | + [], |
| 33 | + ) |
| 34 | + |
| 35 | + expect(createTextStyle).toHaveBeenCalled() |
| 36 | + expect(loadFontAsync).toHaveBeenCalled() |
| 37 | + expect((styleObj as unknown as { fontName: FontName }).fontName).toEqual({ |
| 38 | + family: 'Inter', |
| 39 | + style: 'Italic', |
| 40 | + }) |
| 41 | + expect((styleObj as unknown as { fontSize: number }).fontSize).toBe(14) |
| 42 | + expect( |
| 43 | + (styleObj as unknown as { letterSpacing: LetterSpacing }).letterSpacing, |
| 44 | + ).toEqual({ unit: 'PERCENT', value: 0.1 }) |
| 45 | + expect( |
| 46 | + (styleObj as unknown as { lineHeight: LineHeight }).lineHeight, |
| 47 | + ).toEqual({ unit: 'AUTO' }) |
| 48 | + expect((styleObj as unknown as { textCase: string }).textCase).toBe( |
| 49 | + 'UPPERCASE', |
| 50 | + ) |
| 51 | + expect( |
| 52 | + (styleObj as unknown as { textDecoration: TextDecoration }) |
| 53 | + .textDecoration, |
| 54 | + ).toBe('UNDERLINE') |
| 55 | + expect(notify).not.toHaveBeenCalled() |
| 56 | + }) |
| 57 | + |
| 58 | + test('notifies on font load failure and leaves style untouched', async () => { |
| 59 | + const styleObj = { name: '' } as unknown as TextStyle |
| 60 | + const createTextStyle = mock(() => styleObj) |
| 61 | + const loadFontAsync = mock(() => Promise.reject(new Error('font'))) |
| 62 | + const notify = mock(() => {}) |
| 63 | + |
| 64 | + ;(globalThis as { figma?: unknown }).figma = { |
| 65 | + createTextStyle, |
| 66 | + loadFontAsync, |
| 67 | + notify, |
| 68 | + } as unknown as typeof figma |
| 69 | + |
| 70 | + await applyTypography( |
| 71 | + 'mobile/title', |
| 72 | + { |
| 73 | + fontFamily: 'Inter', |
| 74 | + fontSize: '12', |
| 75 | + letterSpacing: '2', // triggers PIXELS branch if it were to run |
| 76 | + lineHeight: 120, |
| 77 | + }, |
| 78 | + [], |
| 79 | + ) |
| 80 | + |
| 81 | + expect(loadFontAsync).toHaveBeenCalled() |
| 82 | + expect(notify).toHaveBeenCalledWith( |
| 83 | + expect.stringContaining('Failed to create text style'), |
| 84 | + { error: true }, |
| 85 | + ) |
| 86 | + }) |
| 87 | + |
| 88 | + test('applies px letterSpacing and percent lineHeight on existing style', async () => { |
| 89 | + const styleObj = { |
| 90 | + name: 'mobile/body', |
| 91 | + } as unknown as TextStyle |
| 92 | + const loadFontAsync = mock(() => Promise.resolve()) |
| 93 | + const notify = mock(() => {}) |
| 94 | + |
| 95 | + ;(globalThis as { figma?: unknown }).figma = { |
| 96 | + loadFontAsync, |
| 97 | + notify, |
| 98 | + createTextStyle: mock(() => styleObj), |
| 99 | + } as unknown as typeof figma |
| 100 | + |
| 101 | + await applyTypography( |
| 102 | + 'mobile/body', |
| 103 | + { |
| 104 | + fontFamily: 'Inter', |
| 105 | + fontSize: '16', |
| 106 | + letterSpacing: '2', |
| 107 | + lineHeight: 120, |
| 108 | + }, |
| 109 | + [styleObj], |
| 110 | + ) |
| 111 | + |
| 112 | + expect( |
| 113 | + (styleObj as unknown as { letterSpacing: LetterSpacing }).letterSpacing, |
| 114 | + ).toEqual({ unit: 'PIXELS', value: 200 }) |
| 115 | + expect( |
| 116 | + (styleObj as unknown as { lineHeight: LineHeight }).lineHeight, |
| 117 | + ).toEqual({ |
| 118 | + unit: 'PERCENT', |
| 119 | + value: 1.2, |
| 120 | + }) |
| 121 | + expect(loadFontAsync).toHaveBeenCalled() |
| 122 | + expect(notify).not.toHaveBeenCalled() |
| 123 | + }) |
| 124 | + |
| 125 | + test('applies string lineHeight as pixels', async () => { |
| 126 | + const styleObj = { name: '' } as unknown as TextStyle |
| 127 | + const createTextStyle = mock(() => styleObj) |
| 128 | + const loadFontAsync = mock(() => Promise.resolve()) |
| 129 | + |
| 130 | + ;(globalThis as { figma?: unknown }).figma = { |
| 131 | + createTextStyle, |
| 132 | + loadFontAsync, |
| 133 | + notify: mock(() => {}), |
| 134 | + } as unknown as typeof figma |
| 135 | + |
| 136 | + await applyTypography( |
| 137 | + 'mobile/caption', |
| 138 | + { |
| 139 | + fontFamily: 'Inter', |
| 140 | + fontSize: '11', |
| 141 | + lineHeight: '24', |
| 142 | + }, |
| 143 | + [], |
| 144 | + ) |
| 145 | + |
| 146 | + expect( |
| 147 | + (styleObj as unknown as { lineHeight: LineHeight }).lineHeight, |
| 148 | + ).toEqual({ unit: 'PIXELS', value: 24 }) |
| 149 | + }) |
| 150 | +}) |
0 commit comments