@@ -332,6 +332,55 @@ describe('GraphDataUtils', () => {
332332 // Should not throw, chart should render with regular values
333333 expect ( result ) . toHaveProperty ( 'datasets' ) ;
334334 } ) ;
335+
336+ it ( 'should render chart when a referenced field is undefined on some rows' , ( ) => {
337+ // Reproduces the bug where a formula referencing a field that is
338+ // undefined on a row caused the entire chart to be empty. Undefined
339+ // fields should be treated as 0 instead.
340+ const dataWithMissingField = [
341+ { attributes : { month : 'Jan' , price : 10 } } , // quantity undefined
342+ { attributes : { month : 'Feb' , price : 20 , quantity : 3 } } ,
343+ { attributes : { month : 'Mar' , price : 15 } } , // quantity undefined
344+ ] ;
345+
346+ const calculatedValues = [ {
347+ name : 'Total' ,
348+ operator : 'formula' ,
349+ formula : 'price * quantity' ,
350+ } ] ;
351+
352+ const result = processBarLineData ( dataWithMissingField , 'month' , [ ] , null , calculatedValues ) ;
353+
354+ expect ( result ) . toHaveProperty ( 'datasets' ) ;
355+ expect ( result . datasets . length ) . toBe ( 1 ) ;
356+ expect ( result . datasets [ 0 ] . label ) . toBe ( 'Total' ) ;
357+ // Jan: 10 * 0 = 0, Feb: 20 * 3 = 60, Mar: 15 * 0 = 0
358+ expect ( result . datasets [ 0 ] . data ) . toContain ( 60 ) ;
359+ } ) ;
360+
361+ it ( 'should render chart when the referenced field is undefined on every row' , ( ) => {
362+ // Even more extreme case: the field exists in the schema but no row
363+ // has a value for it. The chart should still render (using 0 for the
364+ // missing field) rather than disappearing entirely.
365+ const dataWithAllMissing = [
366+ { attributes : { month : 'Jan' , price : 10 } } ,
367+ { attributes : { month : 'Feb' , price : 20 } } ,
368+ ] ;
369+
370+ const calculatedValues = [ {
371+ name : 'Total' ,
372+ operator : 'formula' ,
373+ formula : 'price + quantity' ,
374+ } ] ;
375+
376+ const result = processBarLineData ( dataWithAllMissing , 'month' , [ ] , null , calculatedValues ) ;
377+
378+ expect ( result ) . toHaveProperty ( 'datasets' ) ;
379+ expect ( result . datasets . length ) . toBe ( 1 ) ;
380+ // Jan: 10 + 0 = 10, Feb: 20 + 0 = 20
381+ expect ( result . datasets [ 0 ] . data ) . toContain ( 10 ) ;
382+ expect ( result . datasets [ 0 ] . data ) . toContain ( 20 ) ;
383+ } ) ;
335384 } ) ;
336385
337386 describe ( 'processPieData with formula' , ( ) => {
0 commit comments