@@ -2,6 +2,7 @@ var Legend = require('@src/components/legend');
22var Plots = require ( '@src/plots/plots' ) ;
33
44var helpers = require ( '@src/components/legend/helpers' ) ;
5+ var anchorUtils = require ( '@src/components/legend/anchor_utils' ) ;
56describe ( 'Test legend:' , function ( ) {
67 'use strict' ;
78
@@ -361,4 +362,90 @@ describe('Test legend:', function() {
361362 } ) ;
362363 } ) ;
363364
365+ describe ( 'isRightAnchor anchor util' , function ( ) {
366+ var isRightAnchor = anchorUtils . isRightAnchor ;
367+ var threshold = 2 / 3 ;
368+
369+ it ( 'should return true when \'xanchor\' is set to \'right\'' , function ( ) {
370+ expect ( isRightAnchor ( { xanchor : 'left' } ) ) . toBe ( false ) ;
371+ expect ( isRightAnchor ( { xanchor : 'center' } ) ) . toBe ( false ) ;
372+ expect ( isRightAnchor ( { xanchor : 'right' } ) ) . toBe ( true ) ;
373+ } ) ;
374+
375+ it ( 'should return true when \'xanchor\' is set to \'auto\' and \'x\' >= 2/3' , function ( ) {
376+ var opts = { xanchor : 'auto' } ;
377+
378+ [ 0 , 0.4 , 0.7 , 1 ] . forEach ( function ( v ) {
379+ opts . x = v ;
380+ expect ( isRightAnchor ( opts ) )
381+ . toBe ( v > threshold , 'case ' + v ) ;
382+ } ) ;
383+ } ) ;
384+ } ) ;
385+
386+ describe ( 'isCenterAnchor anchor util' , function ( ) {
387+ var isCenterAnchor = anchorUtils . isCenterAnchor ;
388+ var threshold0 = 1 / 3 ;
389+ var threshold1 = 2 / 3 ;
390+
391+ it ( 'should return true when \'xanchor\' is set to \'center\'' , function ( ) {
392+ expect ( isCenterAnchor ( { xanchor : 'left' } ) ) . toBe ( false ) ;
393+ expect ( isCenterAnchor ( { xanchor : 'center' } ) ) . toBe ( true ) ;
394+ expect ( isCenterAnchor ( { xanchor : 'right' } ) ) . toBe ( false ) ;
395+ } ) ;
396+
397+ it ( 'should return true when \'xanchor\' is set to \'auto\' and 1/3 < \'x\' < 2/3' , function ( ) {
398+ var opts = { xanchor : 'auto' } ;
399+
400+ [ 0 , 0.4 , 0.7 , 1 ] . forEach ( function ( v ) {
401+ opts . x = v ;
402+ expect ( isCenterAnchor ( opts ) )
403+ . toBe ( v > threshold0 && v < threshold1 , 'case ' + v ) ;
404+ } ) ;
405+ } ) ;
406+ } ) ;
407+
408+ describe ( 'isBottomAnchor anchor util' , function ( ) {
409+ var isBottomAnchor = anchorUtils . isBottomAnchor ;
410+ var threshold = 1 / 3 ;
411+
412+ it ( 'should return true when \'yanchor\' is set to \'right\'' , function ( ) {
413+ expect ( isBottomAnchor ( { yanchor : 'top' } ) ) . toBe ( false ) ;
414+ expect ( isBottomAnchor ( { yanchor : 'middle' } ) ) . toBe ( false ) ;
415+ expect ( isBottomAnchor ( { yanchor : 'bottom' } ) ) . toBe ( true ) ;
416+ } ) ;
417+
418+ it ( 'should return true when \'yanchor\' is set to \'auto\' and \'y\' <= 1/3' , function ( ) {
419+ var opts = { yanchor : 'auto' } ;
420+
421+ [ 0 , 0.4 , 0.7 , 1 ] . forEach ( function ( v ) {
422+ opts . y = v ;
423+ expect ( isBottomAnchor ( opts ) )
424+ . toBe ( v < threshold , 'case ' + v ) ;
425+ } ) ;
426+ } ) ;
427+ } ) ;
428+
429+ describe ( 'isMiddleAnchor anchor util' , function ( ) {
430+ var isMiddleAnchor = anchorUtils . isMiddleAnchor ;
431+ var threshold0 = 1 / 3 ;
432+ var threshold1 = 2 / 3 ;
433+
434+ it ( 'should return true when \'yanchor\' is set to \'center\'' , function ( ) {
435+ expect ( isMiddleAnchor ( { yanchor : 'top' } ) ) . toBe ( false ) ;
436+ expect ( isMiddleAnchor ( { yanchor : 'middle' } ) ) . toBe ( true ) ;
437+ expect ( isMiddleAnchor ( { yanchor : 'bottom' } ) ) . toBe ( false ) ;
438+ } ) ;
439+
440+ it ( 'should return true when \'yanchor\' is set to \'auto\' and 1/3 < \'y\' < 2/3' , function ( ) {
441+ var opts = { yanchor : 'auto' } ;
442+
443+ [ 0 , 0.4 , 0.7 , 1 ] . forEach ( function ( v ) {
444+ opts . y = v ;
445+ expect ( isMiddleAnchor ( opts ) )
446+ . toBe ( v > threshold0 && v < threshold1 , 'case ' + v ) ;
447+ } ) ;
448+ } ) ;
449+ } ) ;
450+
364451} ) ;
0 commit comments