@@ -11,12 +11,13 @@ import {
1111} from '../utils/specs' ;
1212import { LIGHT_THEME } from '../../../utils/themes/light_theme' ;
1313import { mergeWithDefaultTheme } from '../../../utils/themes/theme' ;
14- import { getAnnotationId , getAxisId , getGroupId , getSpecId } from '../../../utils/ids' ;
14+ import { getAnnotationId , getAxisId , getGroupId , getSpecId , AxisId } from '../../../utils/ids' ;
1515import { TooltipType , TooltipValue } from '../utils/interactions' ;
1616import { ScaleBand } from '../../../utils/scales/scale_band' ;
1717import { ScaleContinuous } from '../../../utils/scales/scale_continuous' ;
1818import { ScaleType } from '../../../utils/scales/scales' ;
19- import { ChartStore } from './chart_state' ;
19+ import { ChartStore , isDuplicateAxis } from './chart_state' ;
20+ import { AxisTicksDimensions } from '../utils/axis_utils' ;
2021
2122describe ( 'Chart Store' , ( ) => {
2223 let store = new ChartStore ( ) ;
@@ -71,6 +72,148 @@ describe('Chart Store', () => {
7172 store . computeChart ( ) ;
7273 } ) ;
7374
75+ describe ( 'isDuplicateAxis' , ( ) => {
76+ const AXIS_1_ID = getAxisId ( 'spec_1' ) ;
77+ const AXIS_2_ID = getAxisId ( 'spec_1' ) ;
78+ const axis1 : AxisSpec = {
79+ id : AXIS_1_ID ,
80+ groupId : getGroupId ( 'group_1' ) ,
81+ hide : false ,
82+ showOverlappingTicks : false ,
83+ showOverlappingLabels : false ,
84+ position : Position . Left ,
85+ tickSize : 30 ,
86+ tickPadding : 10 ,
87+ tickFormat : ( value : any ) => `${ value } %` ,
88+ } ;
89+ const axis2 : AxisSpec = {
90+ ...axis1 ,
91+ id : AXIS_2_ID ,
92+ groupId : getGroupId ( 'group_2' ) ,
93+ } ;
94+ const axisTicksDimensions : AxisTicksDimensions = {
95+ tickValues : [ ] ,
96+ tickLabels : [ '10' , '20' , '30' ] ,
97+ maxLabelBboxWidth : 1 ,
98+ maxLabelBboxHeight : 1 ,
99+ maxLabelTextWidth : 1 ,
100+ maxLabelTextHeight : 1 ,
101+ } ;
102+ let tickMap : Map < AxisId , AxisTicksDimensions > ;
103+ let specMap : Map < AxisId , AxisSpec > ;
104+
105+ beforeEach ( ( ) => {
106+ tickMap = new Map < AxisId , AxisTicksDimensions > ( ) ;
107+ specMap = new Map < AxisId , AxisSpec > ( ) ;
108+ } ) ;
109+
110+ it ( 'should return true if axisSpecs and ticks match' , ( ) => {
111+ tickMap . set ( AXIS_2_ID , axisTicksDimensions ) ;
112+ specMap . set ( AXIS_2_ID , axis2 ) ;
113+ const result = isDuplicateAxis ( axis1 , axisTicksDimensions , tickMap , specMap ) ;
114+
115+ expect ( result ) . toBe ( true ) ;
116+ } ) ;
117+
118+ it ( 'should return false if axisSpecs, ticks AND title match' , ( ) => {
119+ tickMap . set ( AXIS_2_ID , axisTicksDimensions ) ;
120+ specMap . set ( AXIS_2_ID , {
121+ ...axis2 ,
122+ title : 'TESTING' ,
123+ } ) ;
124+ const result = isDuplicateAxis (
125+ {
126+ ...axis1 ,
127+ title : 'TESTING' ,
128+ } ,
129+ axisTicksDimensions ,
130+ tickMap ,
131+ specMap ,
132+ ) ;
133+
134+ expect ( result ) . toBe ( true ) ;
135+ } ) ;
136+
137+ it ( 'should return true with single tick' , ( ) => {
138+ const newAxisTicksDimensions = {
139+ ...axisTicksDimensions ,
140+ tickLabels : [ '10' ] ,
141+ } ;
142+ tickMap . set ( AXIS_2_ID , newAxisTicksDimensions ) ;
143+ specMap . set ( AXIS_2_ID , axis2 ) ;
144+
145+ const result = isDuplicateAxis ( axis1 , newAxisTicksDimensions , tickMap , specMap ) ;
146+
147+ expect ( result ) . toBe ( true ) ;
148+ } ) ;
149+
150+ it ( 'should return false if axisSpecs and ticks match but title is different' , ( ) => {
151+ tickMap . set ( AXIS_2_ID , axisTicksDimensions ) ;
152+ specMap . set ( AXIS_2_ID , {
153+ ...axis2 ,
154+ title : 'TESTING' ,
155+ } ) ;
156+ const result = isDuplicateAxis (
157+ {
158+ ...axis1 ,
159+ title : 'NOT TESTING' ,
160+ } ,
161+ axisTicksDimensions ,
162+ tickMap ,
163+ specMap ,
164+ ) ;
165+
166+ expect ( result ) . toBe ( false ) ;
167+ } ) ;
168+
169+ it ( 'should return false if axisSpecs and ticks match but position is different' , ( ) => {
170+ tickMap . set ( AXIS_2_ID , axisTicksDimensions ) ;
171+ specMap . set ( AXIS_2_ID , axis2 ) ;
172+ const result = isDuplicateAxis (
173+ {
174+ ...axis1 ,
175+ position : Position . Top ,
176+ } ,
177+ axisTicksDimensions ,
178+ tickMap ,
179+ specMap ,
180+ ) ;
181+
182+ expect ( result ) . toBe ( false ) ;
183+ } ) ;
184+
185+ it ( 'should return false if tickFormat is different' , ( ) => {
186+ tickMap . set ( AXIS_2_ID , {
187+ ...axisTicksDimensions ,
188+ tickLabels : [ '10%' , '20%' , '30%' ] ,
189+ } ) ;
190+ specMap . set ( AXIS_2_ID , axis2 ) ;
191+
192+ const result = isDuplicateAxis ( axis1 , axisTicksDimensions , tickMap , specMap ) ;
193+
194+ expect ( result ) . toBe ( false ) ;
195+ } ) ;
196+
197+ it ( 'should return false if tick label count is different' , ( ) => {
198+ tickMap . set ( AXIS_2_ID , {
199+ ...axisTicksDimensions ,
200+ tickLabels : [ '10' , '20' , '25' , '30' ] ,
201+ } ) ;
202+ specMap . set ( AXIS_2_ID , axis2 ) ;
203+
204+ const result = isDuplicateAxis ( axis1 , axisTicksDimensions , tickMap , specMap ) ;
205+
206+ expect ( result ) . toBe ( false ) ;
207+ } ) ;
208+
209+ it ( "should return false if can't find spec" , ( ) => {
210+ tickMap . set ( AXIS_2_ID , axisTicksDimensions ) ;
211+ const result = isDuplicateAxis ( axis1 , axisTicksDimensions , tickMap , specMap ) ;
212+
213+ expect ( result ) . toBe ( false ) ;
214+ } ) ;
215+ } ) ;
216+
74217 test ( 'can add a single spec' , ( ) => {
75218 store . addSeriesSpec ( spec ) ;
76219 store . updateParentDimensions ( 600 , 600 , 0 , 0 ) ;
0 commit comments