@@ -2,7 +2,7 @@ import type { DisplayObject } from '@antv/g';
22import { Canvas , CanvasEvent , resetEntityCounter } from '@antv/g' ;
33import { Renderer } from '@antv/g-svg' ;
44import { OffscreenCanvasContext , measureText } from './utils/offscreen-canvas-context' ;
5- import { setMockMeasureTextWidth } from '../../src' ;
5+ import { Layout , setMockMeasureTextWidth } from '../../src' ;
66
77export function createGCanvas ( width : number , height : number ) {
88 resetEntityCounter ( ) ;
@@ -38,6 +38,12 @@ export function sleep(n: number) {
3838 } ) ;
3939}
4040
41+ function nextFrame ( canvas : Canvas ) {
42+ return new Promise < void > ( ( resolve ) => {
43+ canvas . requestAnimationFrame ( ( ) => resolve ( ) ) ;
44+ } ) ;
45+ }
46+
4147export async function renderCanvas ( gshape : DisplayObject , wait = 300 ) {
4248 const bbox = gshape . getBBox ( ) ;
4349 const width = gshape . attributes . width || bbox . x + bbox . width || 400 ;
@@ -48,8 +54,34 @@ export async function renderCanvas(gshape: DisplayObject, wait = 300) {
4854 canvas . addEventListener ( CanvasEvent . READY , async ( ) => {
4955 canvas . appendChild ( gshape ) ;
5056
51- // Wait for the next tick .
57+ // Wait for components, animations and G's mutation queue to settle .
5258 await sleep ( wait ) ;
59+
60+ // Layout nodes are populated before being connected to the canvas, so
61+ // their insertion events can run before layout has measurable children.
62+ // Trigger a final layout after mounting, as G2 does during rendering.
63+ let layoutTriggered = false ;
64+ canvas . getRoot ( ) . forEach ( ( node ) => {
65+ if (
66+ node instanceof Layout &&
67+ node . children . length > 0 &&
68+ node . children . every ( ( child ) => child . attributes . transform === undefined )
69+ ) {
70+ node . layout ( ) ;
71+ layoutTriggered = true ;
72+ }
73+ } ) ;
74+
75+ // Flush the layout mutations before serializing the SVG. The explicit
76+ // render keeps this deterministic in Jest, where RAF scheduling differs
77+ // from a browser's render loop.
78+ if ( layoutTriggered ) {
79+ canvas . render ( ) ;
80+
81+ // Match G2's render completion: wait two frames before taking a snapshot.
82+ await nextFrame ( canvas ) ;
83+ await nextFrame ( canvas ) ;
84+ }
5385 resolve ( canvas ) ;
5486 } ) ;
5587 } ) ;
0 commit comments