@@ -2522,134 +2522,245 @@ describe('Test lib.js:', function () {
25222522 } ) ;
25232523
25242524 describe ( 'hovertemplateString' , function ( ) {
2525- var locale = false ;
25262525 it ( 'evaluates attributes' , function ( ) {
2527- expect ( Lib . hovertemplateString ( 'foo %{bar}' , { } , locale , { bar : 'baz' } ) ) . toEqual ( 'foo baz' ) ;
2526+ expect (
2527+ Lib . hovertemplateString ( {
2528+ args : [ { bar : 'baz' } ] ,
2529+ fallback : '' ,
2530+ string : 'foo %{bar}'
2531+ } )
2532+ ) . toEqual ( 'foo baz' ) ;
25282533 } ) ;
25292534
25302535 it ( 'evaluates attributes with a dot in their name' , function ( ) {
25312536 expect (
2532- Lib . hovertemplateString ( '%{marker.size}' , { } , locale , { 'marker.size' : 12 } , { marker : { size : 14 } } )
2537+ Lib . hovertemplateString ( {
2538+ args : [ { 'marker.size' : 12 } , { marker : { size : 14 } } ] ,
2539+ fallback : '' ,
2540+ string : '%{marker.size}'
2541+ } )
25332542 ) . toEqual ( '12' ) ;
25342543 } ) ;
25352544
25362545 it ( 'evaluates nested properties' , function ( ) {
2537- expect ( Lib . hovertemplateString ( 'foo %{bar.baz}' , { } , locale , { bar : { baz : 'asdf' } } ) ) . toEqual ( 'foo asdf' ) ;
2546+ expect (
2547+ Lib . hovertemplateString ( {
2548+ args : [ { bar : { baz : 'asdf' } } ] ,
2549+ fallback : '' ,
2550+ string : 'foo %{bar.baz}'
2551+ } )
2552+ ) . toEqual ( 'foo asdf' ) ;
25382553 } ) ;
25392554
25402555 it ( 'evaluates array nested properties' , function ( ) {
2541- expect ( Lib . hovertemplateString ( 'foo %{bar[0].baz}' , { } , locale , { bar : [ { baz : 'asdf' } ] } ) ) . toEqual (
2542- 'foo asdf'
2543- ) ;
2556+ expect (
2557+ Lib . hovertemplateString ( {
2558+ args : [ { bar : [ { baz : 'asdf' } ] } ] ,
2559+ fallback : '' ,
2560+ string : 'foo %{bar[0].baz}'
2561+ } )
2562+ ) . toEqual ( 'foo asdf' ) ;
25442563 } ) ;
25452564
25462565 it ( 'should work with the number *0*' , function ( ) {
2547- expect ( Lib . hovertemplateString ( '%{group}' , { } , locale , { group : 0 } ) ) . toEqual ( '0' ) ;
2566+ expect (
2567+ Lib . hovertemplateString ( {
2568+ args : [ { group : 0 } ] ,
2569+ fallback : '' ,
2570+ string : '%{group}'
2571+ } )
2572+ ) . toEqual ( '0' ) ;
25482573 } ) ;
25492574
25502575 it ( 'should work with the number *0* (nested case)' , function ( ) {
2551- expect ( Lib . hovertemplateString ( '%{x.y}' , { } , locale , { x : { y : 0 } } ) ) . toEqual ( '0' ) ;
2576+ expect (
2577+ Lib . hovertemplateString ( {
2578+ args : [ { x : { y : 0 } } ] ,
2579+ fallback : '' ,
2580+ string : '%{x.y}'
2581+ } )
2582+ ) . toEqual ( '0' ) ;
25522583 } ) ;
25532584
25542585 it ( 'preserves null and NaN' , function ( ) {
25552586 expect (
2556- Lib . hovertemplateString ( '%{a} %{b} %{c.d} %{c.e} %{f[0]} %{f[1]}' , { } , locale , {
2557- a : null ,
2558- b : NaN ,
2559- c : { d : null , e : NaN } ,
2560- f : [ null , NaN ]
2587+ Lib . hovertemplateString ( {
2588+ args : [ { a : null , b : NaN , c : { d : null , e : NaN } , f : [ null , NaN ] } ] ,
2589+ fallback : '' ,
2590+ string : '%{a} %{b} %{c.d} %{c.e} %{f[0]} %{f[1]}'
25612591 } )
25622592 ) . toEqual ( 'null NaN null NaN null NaN' ) ;
25632593 } ) ;
25642594
25652595 it ( 'subtitutes multiple matches' , function ( ) {
25662596 expect (
2567- Lib . hovertemplateString ( 'foo %{group} %{trace}' , { } , locale , { group : 'asdf' , trace : 'jkl;' } )
2597+ Lib . hovertemplateString ( {
2598+ args : [ { group : 'asdf' , trace : 'jkl;' } ] ,
2599+ fallback : '' ,
2600+ string : 'foo %{group} %{trace}'
2601+ } )
25682602 ) . toEqual ( 'foo asdf jkl;' ) ;
25692603 } ) ;
25702604
2571- it ( 'replaces missing matches with template string' , function ( ) {
2572- expect ( Lib . hovertemplateString ( 'foo %{group} %{trace}' , { } , locale , { group : 1 } ) ) . toEqual (
2573- 'foo 1 %{trace}'
2574- ) ;
2605+ it ( 'replaces missing matches with fallback value' , function ( ) {
2606+ expect (
2607+ Lib . hovertemplateString ( {
2608+ args : [ { group : 1 } ] ,
2609+ fallback : '' ,
2610+ string : 'foo %{group} %{trace}'
2611+ } )
2612+ ) . toEqual ( 'foo 1 ' ) ;
25752613 } ) ;
25762614
25772615 it ( 'uses the value from the first object with the specified key' , function ( ) {
25782616 var obj1 = { a : 'first' } ;
25792617 var obj2 = { a : 'second' , foo : { bar : 'bar' } } ;
25802618
25812619 // Simple key
2582- expect ( Lib . hovertemplateString ( 'foo %{a}' , { } , locale , obj1 , obj2 ) ) . toEqual ( 'foo first' ) ;
2583- expect ( Lib . hovertemplateString ( 'foo %{a}' , { } , locale , obj2 , obj1 ) ) . toEqual ( 'foo second' ) ;
2620+ expect (
2621+ Lib . hovertemplateString ( {
2622+ args : [ obj1 , obj2 ] ,
2623+ fallback : '' ,
2624+ string : 'foo %{a}'
2625+ } )
2626+ ) . toEqual ( 'foo first' ) ;
2627+ expect (
2628+ Lib . hovertemplateString ( {
2629+ args : [ obj2 , obj1 ] ,
2630+ fallback : '' ,
2631+ string : 'foo %{a}'
2632+ } )
2633+ ) . toEqual ( 'foo second' ) ;
25842634
25852635 // Nested Keys
2586- expect ( Lib . hovertemplateString ( 'foo %{foo.bar}' , { } , locale , obj1 , obj2 ) ) . toEqual ( 'foo bar' ) ;
2636+ expect (
2637+ Lib . hovertemplateString ( {
2638+ args : [ obj1 , obj2 ] ,
2639+ fallback : '' ,
2640+ string : 'foo %{foo.bar}'
2641+ } )
2642+ ) . toEqual ( 'foo bar' ) ;
25872643
25882644 // Nested keys with 0
2589- expect ( Lib . hovertemplateString ( 'y: %{y}' , { } , locale , { y : 0 } , { y : 1 } ) ) . toEqual ( 'y: 0' ) ;
2645+ expect (
2646+ Lib . hovertemplateString ( {
2647+ args : [ { y : 0 } , { y : 1 } ] ,
2648+ fallback : '' ,
2649+ string : 'y: %{y}'
2650+ } )
2651+ ) . toEqual ( 'y: 0' ) ;
25902652 } ) ;
25912653
25922654 it ( 'formats numbers using d3-format mini-language when `:`' , function ( ) {
2593- expect ( Lib . hovertemplateString ( 'a: %{a:.0%}' , { } , locale , { a : 0.123 } ) ) . toEqual ( 'a: 12%' ) ;
2594- expect ( Lib . hovertemplateString ( 'a: %{a:0.2%}' , { } , locale , { a : 0.123 } ) ) . toEqual ( 'a: 12.30%' ) ;
2595- expect ( Lib . hovertemplateString ( 'b: %{b:2.2f}' , { } , locale , { b : 43 } ) ) . toEqual ( 'b: 43.00' ) ;
2655+ expect (
2656+ Lib . hovertemplateString ( {
2657+ args : [ { a : 0.123 } ] ,
2658+ fallback : '' ,
2659+ string : 'a: %{a:.0%}'
2660+ } )
2661+ ) . toEqual ( 'a: 12%' ) ;
2662+ expect (
2663+ Lib . hovertemplateString ( {
2664+ args : [ { a : 0.123 } ] ,
2665+ fallback : '' ,
2666+ string : 'a: %{a:0.2%}'
2667+ } )
2668+ ) . toEqual ( 'a: 12.30%' ) ;
2669+ expect (
2670+ Lib . hovertemplateString ( {
2671+ args : [ { b : 43 } ] ,
2672+ fallback : '' ,
2673+ string : 'b: %{b:2.2f}'
2674+ } )
2675+ ) . toEqual ( 'b: 43.00' ) ;
25962676 } ) ;
25972677
25982678 it ( 'formats date using d3-time-format mini-language `|`' , function ( ) {
2599- expect ( Lib . hovertemplateString ( 'a: %{a|%A}' , { } , locale , { a : '2019-05-22' } ) ) . toEqual ( 'a: Wednesday' ) ;
2600- expect ( Lib . hovertemplateString ( '%{x|%b %-d, %Y}' , { } , locale , { x : '2019-01-01' } ) ) . toEqual ( 'Jan 1, 2019' ) ;
2679+ expect (
2680+ Lib . hovertemplateString ( {
2681+ args : [ { a : '2019-05-22' } ] ,
2682+ fallback : '' ,
2683+ string : 'a: %{a|%A}'
2684+ } )
2685+ ) . toEqual ( 'a: Wednesday' ) ;
2686+ expect (
2687+ Lib . hovertemplateString ( {
2688+ args : [ { x : '2019-01-01' } ] ,
2689+ fallback : '' ,
2690+ string : '%{x|%b %-d, %Y}'
2691+ } )
2692+ ) . toEqual ( 'Jan 1, 2019' ) ;
26012693 } ) ;
26022694
26032695 it ( 'looks for default label if no format is provided' , function ( ) {
2604- expect ( Lib . hovertemplateString ( 'y: %{y}' , { yLabel : '0.1' } , locale , { y : 0.123 } ) ) . toEqual ( 'y: 0.1' ) ;
2696+ expect (
2697+ Lib . hovertemplateString ( {
2698+ args : [ { y : 0.123 } ] ,
2699+ fallback : '' ,
2700+ labels : { yLabel : '0.1' } ,
2701+ string : 'y: %{y}'
2702+ } )
2703+ ) . toEqual ( 'y: 0.1' ) ;
26052704 } ) ;
26062705
26072706 it ( 'warns user up to 10 times if a variable cannot be found' , function ( ) {
26082707 spyOn ( Lib , 'warn' ) . and . callThrough ( ) ;
2609- Lib . hovertemplateString ( '%{idontexist}' , { } ) ;
2708+ Lib . hovertemplateString ( {
2709+ fallback : '' ,
2710+ string : '%{idontexist}'
2711+ } ) ;
26102712 expect ( Lib . warn . calls . count ( ) ) . toBe ( 1 ) ;
26112713
26122714 for ( var i = 0 ; i < 15 ; i ++ ) {
2613- Lib . hovertemplateString ( '%{idontexist}' , { } ) ;
2715+ Lib . hovertemplateString ( {
2716+ fallback : '' ,
2717+ string : '%{idontexist}'
2718+ } ) ;
26142719 }
26152720 expect ( Lib . warn . calls . count ( ) ) . toBe ( 10 ) ;
26162721 } ) ;
2617-
2618- it ( 'does not error out when arguments are undefined' , function ( ) {
2619- expect ( function ( ) {
2620- Lib . hovertemplateString ( 'y: %{y}' , undefined , locale , undefined ) ;
2621- } ) . not . toThrow ( ) ;
2622- } ) ;
26232722 } ) ;
26242723
26252724 describe ( 'texttemplateString' , function ( ) {
26262725 var locale = false ;
26272726 it ( 'evaluates attributes' , function ( ) {
2628- expect ( Lib . texttemplateString ( 'foo %{bar}' , { } , locale , { bar : 'baz' } ) ) . toEqual ( 'foo baz' ) ;
2727+ expect (
2728+ Lib . texttemplateString ( {
2729+ args : [ { bar : 'baz' } ] ,
2730+ fallback : '' ,
2731+ string : 'foo %{bar}'
2732+ } )
2733+ ) . toEqual ( 'foo baz' ) ;
26292734 } ) ;
26302735
26312736 it ( 'looks for default label if no format is provided' , function ( ) {
2632- expect ( Lib . texttemplateString ( 'y: %{y}' , { yLabel : '0.1' } , locale , { y : 0.123 } ) ) . toEqual ( 'y: 0.1' ) ;
2737+ expect (
2738+ Lib . texttemplateString ( {
2739+ args : [ { y : 0.123 } ] ,
2740+ fallback : '' ,
2741+ labels : { yLabel : '0.1' } ,
2742+ string : 'y: %{y}'
2743+ } )
2744+ ) . toEqual ( 'y: 0.1' ) ;
26332745 } ) ;
26342746
26352747 it ( 'preserves null and NaN' , function ( ) {
26362748 expect (
2637- Lib . texttemplateString ( '%{a} %{b} %{c.d} %{c.e} %{f[0]} %{f[1]}' , { } , locale , {
2638- a : null ,
2639- b : NaN ,
2640- c : { d : null , e : NaN } ,
2641- f : [ null , NaN ]
2749+ Lib . texttemplateString ( {
2750+ args : [ { a : null , b : NaN , c : { d : null , e : NaN } , f : [ null , NaN ] } ] ,
2751+ fallback : '' ,
2752+ string : '%{a} %{b} %{c.d} %{c.e} %{f[0]} %{f[1]}'
26422753 } )
26432754 ) . toEqual ( 'null NaN null NaN null NaN' ) ;
26442755 } ) ;
26452756
26462757 it ( 'warns user up to 10 times if a variable cannot be found' , function ( ) {
26472758 spyOn ( Lib , 'warn' ) . and . callThrough ( ) ;
2648- Lib . texttemplateString ( ' %{idontexist}', { } ) ;
2759+ Lib . texttemplateString ( { fallback : '' , string : ' %{idontexist}' } ) ;
26492760 expect ( Lib . warn . calls . count ( ) ) . toBe ( 1 ) ;
26502761
26512762 for ( var i = 0 ; i < 15 ; i ++ ) {
2652- Lib . texttemplateString ( ' %{idontexist}', { } ) ;
2763+ Lib . texttemplateString ( { fallback : '' , string : ' %{idontexist}' } ) ;
26532764 }
26542765 expect ( Lib . warn . calls . count ( ) ) . toBe ( 11 ) ;
26552766 } ) ;
0 commit comments