@@ -189,6 +189,145 @@ describe('svgDraw', function () {
189189 expect ( text . style ) . toHaveBeenCalledWith ( 'font-weight' , '500' ) ;
190190 } ) ;
191191 } ) ;
192+ describe ( 'drawLoop' , function ( ) {
193+ /**
194+ * Extended MockD3 that supports getBBox (needed by drawLoop's text measurement).
195+ */
196+ const MockD3WithBBox = ( name , parent ) => {
197+ const children = [ ] ;
198+ const elem = {
199+ get __children ( ) {
200+ return children ;
201+ } ,
202+ get __name ( ) {
203+ return name ;
204+ } ,
205+ get __parent ( ) {
206+ return parent ;
207+ } ,
208+ _groups : [ [ { getBBox : ( ) => ( { x : 0 , y : 0 , width : 100 , height : 20 } ) } ] ] ,
209+ } ;
210+ elem . append = ( childName ) => {
211+ const mockElem = MockD3WithBBox ( childName , elem ) ;
212+ children . push ( mockElem ) ;
213+ return mockElem ;
214+ } ;
215+ elem . lower = vi . fn ( ( ) => elem ) ;
216+ elem . attr = vi . fn ( ( ) => elem ) ;
217+ elem . text = vi . fn ( ( ) => elem ) ;
218+ elem . style = vi . fn ( ( ) => elem ) ;
219+ elem . node = vi . fn ( ( ) => ( {
220+ querySelectorAll : ( selector ) => {
221+ // Collect all matching children recursively
222+ const results = [ ] ;
223+ const walk = ( node ) => {
224+ for ( const child of node . __children || [ ] ) {
225+ if ( selector === '.labelBox' && child . __name === 'polygon' ) {
226+ // Check if it was called with class labelBox
227+ const calls = child . attr . mock . calls ;
228+ if ( calls . some ( ( c ) => c [ 0 ] === 'class' && c [ 1 ] === 'labelBox' ) ) {
229+ results . push ( child ) ;
230+ }
231+ }
232+ walk ( child ) ;
233+ }
234+ } ;
235+ walk ( elem ) ;
236+ return results ;
237+ } ,
238+ } ) ) ;
239+ return elem ;
240+ } ;
241+
242+ it ( 'should create a labelBox polygon for the main loop label' , async function ( ) {
243+ const svg = MockD3WithBBox ( 'svg' ) ;
244+ const loopModel = {
245+ startx : 10 ,
246+ starty : 10 ,
247+ stopx : 200 ,
248+ stopy : 200 ,
249+ title : 'Yes' ,
250+ wrap : false ,
251+ sectionTitles : [ ] ,
252+ sections : [ ] ,
253+ } ;
254+ const conf = {
255+ boxMargin : 10 ,
256+ boxTextMargin : 5 ,
257+ labelBoxHeight : 20 ,
258+ labelBoxWidth : 50 ,
259+ messageFontFamily : 'Arial' ,
260+ messageFontSize : 12 ,
261+ messageFontWeight : 'normal' ,
262+ } ;
263+ const msg = { id : 'test1' } ;
264+
265+ const g = await svgDraw . drawLoop ( svg , loopModel , 'alt' , conf , msg ) ;
266+ const labelBoxes = g . node ( ) . querySelectorAll ( '.labelBox' ) ;
267+ expect ( labelBoxes . length ) . toBeGreaterThanOrEqual ( 1 ) ;
268+ } ) ;
269+
270+ it ( 'should create labelBox polygons for section titles (else labels)' , async function ( ) {
271+ const svg = MockD3WithBBox ( 'svg' ) ;
272+ const loopModel = {
273+ startx : 10 ,
274+ starty : 10 ,
275+ stopx : 200 ,
276+ stopy : 200 ,
277+ title : 'Yes' ,
278+ wrap : false ,
279+ sectionTitles : [ { message : 'else Command' } ] ,
280+ sections : [ { y : 100 , height : 50 } ] ,
281+ } ;
282+ const conf = {
283+ boxMargin : 10 ,
284+ boxTextMargin : 5 ,
285+ labelBoxHeight : 20 ,
286+ labelBoxWidth : 50 ,
287+ messageFontFamily : 'Arial' ,
288+ messageFontSize : 12 ,
289+ messageFontWeight : 'normal' ,
290+ } ;
291+ const msg = { id : 'test2' } ;
292+
293+ const g = await svgDraw . drawLoop ( svg , loopModel , 'alt' , conf , msg ) ;
294+ const labelBoxes = g . node ( ) . querySelectorAll ( '.labelBox' ) ;
295+ // Should have 2 labelBox elements: one for "alt" label, one for "else Command"
296+ expect ( labelBoxes . length ) . toBe ( 2 ) ;
297+ } ) ;
298+
299+ it ( 'should create labelBox for each section title when multiple exist' , async function ( ) {
300+ const svg = MockD3WithBBox ( 'svg' ) ;
301+ const loopModel = {
302+ startx : 10 ,
303+ starty : 10 ,
304+ stopx : 300 ,
305+ stopy : 300 ,
306+ title : 'Yes' ,
307+ wrap : false ,
308+ sectionTitles : [ { message : 'else A' } , { message : 'else B' } ] ,
309+ sections : [
310+ { y : 100 , height : 50 } ,
311+ { y : 200 , height : 50 } ,
312+ ] ,
313+ } ;
314+ const conf = {
315+ boxMargin : 10 ,
316+ boxTextMargin : 5 ,
317+ labelBoxHeight : 20 ,
318+ labelBoxWidth : 50 ,
319+ messageFontFamily : 'Arial' ,
320+ messageFontSize : 12 ,
321+ messageFontWeight : 'normal' ,
322+ } ;
323+ const msg = { id : 'test3' } ;
324+
325+ const g = await svgDraw . drawLoop ( svg , loopModel , 'alt' , conf , msg ) ;
326+ const labelBoxes = g . node ( ) . querySelectorAll ( '.labelBox' ) ;
327+ // 1 main label + 2 section titles = 3 labelBox elements
328+ expect ( labelBoxes . length ) . toBe ( 3 ) ;
329+ } ) ;
330+ } ) ;
192331 describe ( 'drawBackgroundRect' , function ( ) {
193332 it ( 'should append a rect before the previous element within a given bound' , function ( ) {
194333 const svg = MockD3 ( 'svg' ) ;
0 commit comments