1- import { describe , expect , test } from 'bun:test'
1+ import { afterEach , describe , expect , test } from 'bun:test'
22import { textStyleToTypography } from '../text-style-to-typography'
33
4- function makeStyle ( styleName : string ) : TextStyle {
4+ function makeStyle (
5+ styleName : string ,
6+ boundVariables ?: Record < string , any > ,
7+ ) : TextStyle {
58 return {
69 id : 'style' ,
710 name : 'style' ,
@@ -19,10 +22,15 @@ function makeStyle(styleName: string): TextStyle {
1922 textAlignVertical : 'TOP' ,
2023 lineHeight : { unit : 'AUTO' } ,
2124 letterSpacing : { unit : 'PIXELS' , value : 0 } ,
25+ boundVariables,
2226 } as unknown as TextStyle
2327}
2428
2529describe ( 'textStyleToTypography' , ( ) => {
30+ afterEach ( ( ) => {
31+ ; ( globalThis as any ) . figma = undefined
32+ } )
33+
2634 test . each ( [
2735 [ 'Thin' , 100 ] ,
2836 [ 'Extra Light' , 200 ] ,
@@ -40,8 +48,63 @@ describe('textStyleToTypography', () => {
4048 [ 'Heavy' , 900 ] ,
4149 [ '750' , 750 ] ,
4250 [ 'UnknownWeight' , 400 ] ,
43- ] ) ( 'maps %s to fontWeight %d' , ( styleName , expected ) => {
44- const result = textStyleToTypography ( makeStyle ( styleName ) )
51+ ] ) ( 'maps %s to fontWeight %d' , async ( styleName , expected ) => {
52+ const result = await textStyleToTypography ( makeStyle ( styleName ) )
4553 expect ( result . fontWeight ) . toBe ( expected )
4654 } )
55+
56+ test ( 'returns base typography when no boundVariables' , async ( ) => {
57+ const result = await textStyleToTypography ( makeStyle ( 'Regular' ) )
58+ expect ( result . fontFamily ) . toBe ( 'Pretendard' )
59+ expect ( result . fontSize ) . toBe ( '16px' )
60+ } )
61+
62+ test ( 'overrides fields with bound variable references' , async ( ) => {
63+ ; ( globalThis as any ) . figma = {
64+ variables : {
65+ getVariableByIdAsync : async ( id : string ) => {
66+ const vars : Record < string , any > = {
67+ v1 : { name : 'heading/size' } ,
68+ v2 : { name : 'heading/line-height' } ,
69+ v3 : { name : 'heading/spacing' } ,
70+ v4 : { name : 'heading/weight' } ,
71+ v5 : { name : 'heading/family' } ,
72+ v6 : { name : 'heading/style' } ,
73+ }
74+ return vars [ id ] ?? null
75+ } ,
76+ } ,
77+ }
78+ const result = await textStyleToTypography (
79+ makeStyle ( 'Regular' , {
80+ fontSize : { type : 'VARIABLE_ALIAS' , id : 'v1' } ,
81+ lineHeight : { type : 'VARIABLE_ALIAS' , id : 'v2' } ,
82+ letterSpacing : { type : 'VARIABLE_ALIAS' , id : 'v3' } ,
83+ fontWeight : { type : 'VARIABLE_ALIAS' , id : 'v4' } ,
84+ fontFamily : { type : 'VARIABLE_ALIAS' , id : 'v5' } ,
85+ fontStyle : { type : 'VARIABLE_ALIAS' , id : 'v6' } ,
86+ } ) ,
87+ )
88+ expect ( result . fontSize ) . toBe ( '$headingSize' )
89+ expect ( result . lineHeight ) . toBe ( '$headingLineHeight' )
90+ expect ( result . letterSpacing ) . toBe ( '$headingSpacing' )
91+ expect ( result . fontWeight ) . toBe ( '$headingWeight' )
92+ expect ( result . fontFamily ) . toBe ( '$headingFamily' )
93+ expect ( result . fontStyle ) . toBe ( '$headingStyle' )
94+ } )
95+
96+ test ( 'keeps base value when variable not found' , async ( ) => {
97+ ; ( globalThis as any ) . figma = {
98+ variables : {
99+ getVariableByIdAsync : async ( ) => null ,
100+ } ,
101+ }
102+ const result = await textStyleToTypography (
103+ makeStyle ( 'Bold' , {
104+ fontSize : { type : 'VARIABLE_ALIAS' , id : 'missing' } ,
105+ } ) ,
106+ )
107+ expect ( result . fontSize ) . toBe ( '16px' )
108+ expect ( result . fontWeight ) . toBe ( 700 )
109+ } )
47110} )
0 commit comments