@@ -3,7 +3,7 @@ import type { Devup } from '../../types'
33import { uploadDevupXlsx } from '../upload-devup-xlsx'
44
55describe ( 'uploadDevupXlsx' , ( ) => {
6- function createMockFigma ( ) {
6+ function setupMockFigma ( ) {
77 const showUIMock = mock ( ( ) => { } )
88 const closeMock = mock ( ( ) => { } )
99 let onmessageHandler : ( ( message : string ) => void ) | null = null
@@ -28,61 +28,80 @@ describe('uploadDevupXlsx', () => {
2828 ui : uiObj ,
2929 } as unknown as typeof figma
3030
31+ // Also set globalThis.figma as fallback — guards against Bun's parallel
32+ // test runner potentially resolving a cached module without the ctx param.
33+ ; ( globalThis as { figma ?: unknown } ) . figma = ctx
34+
3135 return { ctx, showUIMock, closeMock, getHandler : ( ) => onmessageHandler }
3236 }
3337
38+ function teardown ( ) {
39+ ; ( globalThis as { figma ?: unknown } ) . figma = undefined
40+ }
41+
3442 test ( 'should call showUI with correct HTML string' , ( ) => {
35- const { ctx, showUIMock } = createMockFigma ( )
36- uploadDevupXlsx ( ctx )
37- expect ( showUIMock ) . toHaveBeenCalledWith (
38- expect . stringContaining ( 'accept=".xlsx"' ) ,
39- )
40- expect ( showUIMock ) . toHaveBeenCalledWith (
41- expect . stringContaining ( 'xlsx-0.20.3' ) ,
42- )
43+ const { ctx, showUIMock } = setupMockFigma ( )
44+ try {
45+ uploadDevupXlsx ( ctx )
46+ expect ( showUIMock ) . toHaveBeenCalledWith (
47+ expect . stringContaining ( 'accept=".xlsx"' ) ,
48+ )
49+ expect ( showUIMock ) . toHaveBeenCalledWith (
50+ expect . stringContaining ( 'xlsx-0.20.3' ) ,
51+ )
52+ } finally {
53+ teardown ( )
54+ }
4355 } )
4456
4557 test ( 'should resolve with parsed JSON when message is received' , async ( ) => {
46- const { ctx, closeMock, getHandler } = createMockFigma ( )
47- const testData = { theme : { colors : { } , typography : { } } }
48- const promise = uploadDevupXlsx ( ctx )
58+ const { ctx, closeMock, getHandler } = setupMockFigma ( )
59+ try {
60+ const testData = { theme : { colors : { } , typography : { } } }
61+ const promise = uploadDevupXlsx ( ctx )
4962
50- // Simulate message from UI
51- const handler = getHandler ( )
52- if ( handler ) {
53- handler ( JSON . stringify ( testData ) )
54- }
63+ const handler = getHandler ( )
64+ if ( handler ) {
65+ handler ( JSON . stringify ( testData ) )
66+ }
5567
56- const result = await promise
57- expect ( closeMock ) . toHaveBeenCalled ( )
58- expect ( result ) . toEqual ( testData )
68+ const result = await promise
69+ expect ( closeMock ) . toHaveBeenCalled ( )
70+ expect ( result ) . toEqual ( testData )
71+ } finally {
72+ teardown ( )
73+ }
5974 } )
6075
6176 test ( 'should handle message with colors and typography' , async ( ) => {
62- const { ctx, getHandler } = createMockFigma ( )
63- const testData = {
64- theme : {
65- colors : {
66- light : {
67- primary : '#000000' ,
77+ const { ctx, getHandler } = setupMockFigma ( )
78+ try {
79+ const testData = {
80+ theme : {
81+ colors : {
82+ light : {
83+ primary : '#000000' ,
84+ } ,
6885 } ,
69- } ,
70- typography : {
71- heading : {
72- fontFamily : 'Arial' ,
73- fontSize : 24 ,
86+ typography : {
87+ heading : {
88+ fontFamily : 'Arial' ,
89+ fontSize : 24 ,
90+ } ,
7491 } ,
7592 } ,
76- } ,
77- }
78- const promise = uploadDevupXlsx ( ctx )
93+ }
94+ const promise = uploadDevupXlsx ( ctx )
7995
80- const handler = getHandler ( )
81- if ( handler ) {
82- handler ( JSON . stringify ( testData ) )
83- }
96+ const handler = getHandler ( )
97+ if ( handler ) {
98+ handler ( JSON . stringify ( testData ) )
99+ }
84100
85- const result = await promise
86- expect ( result ) . toEqual ( testData as unknown as Devup )
101+ const result = await promise
102+ expect ( result ) . toEqual ( testData as unknown as Devup )
103+ } finally {
104+ teardown ( )
105+ }
87106 } )
88107} )
0 commit comments