@@ -10,15 +10,75 @@ describe("parseData", () => {
1010 expect ( parseData ( staticData ) ) . toEqual ( [ { x : [ 1 ] , y : [ 2 ] } ] ) ;
1111 } ) ;
1212
13- it ( "parses sampleData when attributeData and staticData are empty " , ( ) => {
14- const sampleData = JSON . stringify ( [ { x : [ 3 ] , y : [ 4 ] } ] ) ;
15- expect ( parseData ( undefined , undefined , sampleData ) ) . toEqual ( [ { x : [ 3 ] , y : [ 4 ] } ] ) ;
13+ it ( "parses attributeData only " , ( ) => {
14+ const attributeData = JSON . stringify ( [ { x : [ 5 ] , y : [ 6 ] } ] ) ;
15+ expect ( parseData ( undefined , attributeData ) ) . toEqual ( [ { x : [ 5 ] , y : [ 6 ] } ] ) ;
1616 } ) ;
1717
18- it ( "parses attributeData and ignores sampleData if attributeData is present" , ( ) => {
19- const attributeData = JSON . stringify ( [ { x : [ 5 ] , y : [ 6 ] } ] ) ;
20- const sampleData = JSON . stringify ( [ { x : [ 7 ] , y : [ 8 ] } ] ) ;
21- expect ( parseData ( undefined , attributeData , sampleData ) ) . toEqual ( [ { x : [ 5 ] , y : [ 6 ] } ] ) ;
18+ it ( "merges static and attribute traces by index with equal lengths" , ( ) => {
19+ const staticData = JSON . stringify ( [
20+ { type : "bar" , x : [ 1 , 2 , 3 ] } ,
21+ { type : "line" , x : [ 4 , 5 , 6 ] }
22+ ] ) ;
23+ const attributeData = JSON . stringify ( [ { y : [ 10 , 20 , 30 ] } , { y : [ 40 , 50 , 60 ] } ] ) ;
24+ expect ( parseData ( staticData , attributeData ) ) . toEqual ( [
25+ { type : "bar" , x : [ 1 , 2 , 3 ] , y : [ 10 , 20 , 30 ] } ,
26+ { type : "line" , x : [ 4 , 5 , 6 ] , y : [ 40 , 50 , 60 ] }
27+ ] ) ;
28+ } ) ;
29+
30+ it ( "attribute data overrides static properties" , ( ) => {
31+ const staticData = JSON . stringify ( [ { name : "static" , x : [ 1 , 2 ] } ] ) ;
32+ const attributeData = JSON . stringify ( [ { name : "attribute" , y : [ 3 , 4 ] } ] ) ;
33+ expect ( parseData ( staticData , attributeData ) ) . toEqual ( [ { name : "attribute" , x : [ 1 , 2 ] , y : [ 3 , 4 ] } ] ) ;
34+ } ) ;
35+
36+ it ( "appends extra static traces when static has more traces" , ( ) => {
37+ const staticData = JSON . stringify ( [
38+ { type : "bar" , x : [ 1 ] } ,
39+ { type : "line" , x : [ 2 ] } ,
40+ { type : "scatter" , x : [ 3 ] }
41+ ] ) ;
42+ const attributeData = JSON . stringify ( [ { y : [ 10 ] } ] ) ;
43+ expect ( parseData ( staticData , attributeData ) ) . toEqual ( [
44+ { type : "bar" , x : [ 1 ] , y : [ 10 ] } ,
45+ { type : "line" , x : [ 2 ] } ,
46+ { type : "scatter" , x : [ 3 ] }
47+ ] ) ;
48+ } ) ;
49+
50+ it ( "appends extra attribute traces when attribute has more traces" , ( ) => {
51+ const staticData = JSON . stringify ( [ { type : "bar" , x : [ 1 ] } ] ) ;
52+ const attributeData = JSON . stringify ( [ { y : [ 10 ] } , { y : [ 20 ] } , { y : [ 30 ] } ] ) ;
53+ expect ( parseData ( staticData , attributeData ) ) . toEqual ( [
54+ { type : "bar" , x : [ 1 ] , y : [ 10 ] } ,
55+ { y : [ 20 ] } ,
56+ { y : [ 30 ] }
57+ ] ) ;
58+ } ) ;
59+
60+ it ( "returns empty array on invalid JSON" , ( ) => {
61+ expect ( parseData ( "invalid json" ) ) . toEqual ( [ ] ) ;
62+ } ) ;
63+
64+ it ( "merges sampleData with static when attributeData is empty" , ( ) => {
65+ const staticData = JSON . stringify ( [ { type : "bar" , x : [ 1 , 2 , 3 ] } ] ) ;
66+ const sampleData = JSON . stringify ( [ { y : [ 10 , 20 , 30 ] } ] ) ;
67+ expect ( parseData ( staticData , undefined , sampleData ) ) . toEqual ( [ { type : "bar" , x : [ 1 , 2 , 3 ] , y : [ 10 , 20 , 30 ] } ] ) ;
68+ } ) ;
69+
70+ it ( "ignores sampleData when attributeData is present" , ( ) => {
71+ const staticData = JSON . stringify ( [ { type : "bar" , x : [ 1 ] } ] ) ;
72+ const attributeData = JSON . stringify ( [ { y : [ 10 ] } ] ) ;
73+ const sampleData = JSON . stringify ( [ { y : [ 99 ] , name : "sample" } ] ) ;
74+ expect ( parseData ( staticData , attributeData , sampleData ) ) . toEqual ( [ { type : "bar" , x : [ 1 ] , y : [ 10 ] } ] ) ;
75+ } ) ;
76+
77+ it ( "uses sampleData only when attributeData is empty array string" , ( ) => {
78+ const staticData = JSON . stringify ( [ { type : "line" , x : [ 1 ] } ] ) ;
79+ const attributeData = JSON . stringify ( [ ] ) ;
80+ const sampleData = JSON . stringify ( [ { y : [ 5 ] } ] ) ;
81+ expect ( parseData ( staticData , attributeData , sampleData ) ) . toEqual ( [ { type : "line" , x : [ 1 ] , y : [ 5 ] } ] ) ;
2282 } ) ;
2383} ) ;
2484
0 commit comments