@@ -115,9 +115,15 @@ describe("FieldToolbarComponent", () => {
115115 let mockEventDetails : VisualBuilderCslpEventDetails ;
116116
117117 beforeAll ( ( ) => {
118- // Mock FieldSchemaMap once for all tests
119- vi . spyOn ( FieldSchemaMap , "getFieldSchema" ) . mockResolvedValue (
120- mockMultipleLinkFieldSchema
118+ // Pre-cache field schema so getFieldSchema resolves synchronously from cache
119+ // This is much faster than async resolution
120+ // Use the actual content_type_uid from mockMultipleFieldMetadata (empty string)
121+ FieldSchemaMap . setFieldSchema (
122+ mockMultipleFieldMetadata . content_type_uid || "" ,
123+ {
124+ [ mockMultipleFieldMetadata . fieldPath ] :
125+ mockMultipleLinkFieldSchema ,
126+ }
121127 ) ;
122128 } ) ;
123129
@@ -139,8 +145,15 @@ describe("FieldToolbarComponent", () => {
139145 isDisabled : false ,
140146 reason : "" ,
141147 } ) ;
142- vi . mocked ( FieldSchemaMap . getFieldSchema ) . mockResolvedValue (
143- mockMultipleLinkFieldSchema
148+
149+ // Ensure field schema is in cache for immediate resolution
150+ // Don't use vi.spyOn - let the actual getFieldSchema use the cache
151+ FieldSchemaMap . setFieldSchema (
152+ mockMultipleFieldMetadata . content_type_uid || "" ,
153+ {
154+ [ mockMultipleFieldMetadata . fieldPath ] :
155+ mockMultipleLinkFieldSchema ,
156+ }
144157 ) ;
145158 } ) ;
146159
@@ -161,7 +174,8 @@ describe("FieldToolbarComponent", () => {
161174 />
162175 ) ;
163176
164- // Use queryByTestId + waitFor for faster detection (fails immediately if not found)
177+ // Wait for button to appear after async operations complete
178+ // FieldSchema is pre-cached, so it resolves immediately, but React still needs to render
165179 const moveLeftButton = await waitFor (
166180 ( ) => {
167181 const button = container . querySelector (
@@ -170,7 +184,7 @@ describe("FieldToolbarComponent", () => {
170184 if ( ! button ) throw new Error ( "Button not found" ) ;
171185 return button ;
172186 } ,
173- { timeout : 500 , interval : 10 } // Reduced to 500ms - mocks resolve immediately
187+ { timeout : 500 , interval : 10 } // Reduced timeout - cached schemas resolve immediately
174188 ) ;
175189
176190 fireEvent . click ( moveLeftButton ) ;
@@ -189,7 +203,7 @@ describe("FieldToolbarComponent", () => {
189203 />
190204 ) ;
191205
192- // Use queryByTestId + waitFor for faster detection
206+ // Wait for button to appear after async operations complete
193207 const moveRightButton = await waitFor (
194208 ( ) => {
195209 const button = container . querySelector (
@@ -198,7 +212,7 @@ describe("FieldToolbarComponent", () => {
198212 if ( ! button ) throw new Error ( "Button not found" ) ;
199213 return button ;
200214 } ,
201- { timeout : 500 , interval : 10 } // Reduced to 500ms - mocks resolve immediately
215+ { timeout : 1000 , interval : 20 } // Reduced timeout - cached schemas resolve immediately
202216 ) ;
203217
204218 fireEvent . click ( moveRightButton ) ;
@@ -217,7 +231,7 @@ describe("FieldToolbarComponent", () => {
217231 />
218232 ) ;
219233
220- // Use queryByTestId + waitFor for faster detection
234+ // Wait for button to appear after async operations complete
221235 const deleteButton = await waitFor (
222236 ( ) => {
223237 const button = container . querySelector (
@@ -226,7 +240,7 @@ describe("FieldToolbarComponent", () => {
226240 if ( ! button ) throw new Error ( "Button not found" ) ;
227241 return button ;
228242 } ,
229- { timeout : 500 , interval : 10 } // Reduced to 500ms - mocks resolve immediately
243+ { timeout : 1000 , interval : 20 } // Reduced timeout - cached schemas resolve immediately
230244 ) ;
231245
232246 fireEvent . click ( deleteButton ) ;
@@ -255,6 +269,7 @@ describe("FieldToolbarComponent", () => {
255269
256270 // Wait for async operations to complete (fieldSchema and variantStatus)
257271 // The icon appears after both FieldSchemaMap.getFieldSchema() and getFieldVariantStatus() complete
272+ // Pre-cached field schema resolves immediately, but React still needs time to re-render
258273 await waitFor (
259274 ( ) => {
260275 const icon = container . querySelector (
@@ -263,15 +278,19 @@ describe("FieldToolbarComponent", () => {
263278 if ( ! icon ) throw new Error ( "Variant icon not found" ) ;
264279 expect ( icon ) . toBeInTheDocument ( ) ;
265280 } ,
266- { timeout : 500 , interval : 10 } // Reduced to 500ms - mocks resolve immediately
281+ { timeout : 1000 , interval : 20 } // Reduced timeout - cached schemas resolve immediately
267282 ) ;
268283 } ) ;
269284
270285 describe ( "'Replace button' visibility for multiple file fields" , ( ) => {
271286 beforeEach ( ( ) => {
272- // Override the mock for this describe block (must be beforeEach to override outer beforeEach)
273- vi . mocked ( FieldSchemaMap . getFieldSchema ) . mockResolvedValue (
274- mockMultipleFileFieldSchema
287+ // Pre-cache file field schema for immediate resolution
288+ FieldSchemaMap . setFieldSchema (
289+ mockMultipleFieldMetadata . content_type_uid || "" ,
290+ {
291+ [ mockMultipleFieldMetadata . fieldPath ] :
292+ mockMultipleFileFieldSchema ,
293+ }
275294 ) ;
276295 } ) ;
277296
@@ -308,13 +327,13 @@ describe("FieldToolbarComponent", () => {
308327 '[data-testid="visual-builder__focused-toolbar__multiple-field-toolbar"]'
309328 ) ;
310329 if ( ! toolbar ) throw new Error ( "Toolbar not found" ) ;
311-
330+
312331 const replaceButton = container . querySelector (
313332 '[data-testid="visual-builder-replace-file"]'
314333 ) ;
315334 expect ( replaceButton ) . not . toBeInTheDocument ( ) ;
316335 } ,
317- { timeout : 500 , interval : 10 } // Reduced to 500ms - mocks resolve immediately
336+ { timeout : 1000 , interval : 20 } // Reduced timeout - cached schemas resolve immediately
318337 ) ;
319338 } ) ;
320339
@@ -345,10 +364,11 @@ describe("FieldToolbarComponent", () => {
345364 const replaceButton = container . querySelector (
346365 '[data-testid="visual-builder-replace-file"]'
347366 ) ;
348- if ( ! replaceButton ) throw new Error ( "Replace button not found" ) ;
367+ if ( ! replaceButton )
368+ throw new Error ( "Replace button not found" ) ;
349369 expect ( replaceButton ) . toBeInTheDocument ( ) ;
350370 } ,
351- { timeout : 500 , interval : 10 } // Reduced to 500ms - mocks resolve immediately
371+ { timeout : 1000 , interval : 20 } // Reduced timeout - cached schemas resolve immediately
352372 ) ;
353373 } ) ;
354374
@@ -408,7 +428,7 @@ describe("FieldToolbarComponent", () => {
408428 expect ( replaceButton ) . toBeDisabled ( ) ;
409429 }
410430 } ,
411- { timeout : 500 , interval : 10 } // Reduced to 500ms - mocks resolve immediately
431+ { timeout : 1000 , interval : 20 } // Reduced timeout - cached schemas resolve immediately
412432 ) ;
413433 } ) ;
414434 } ) ;
0 commit comments