@@ -139,6 +139,49 @@ describe('MetricUnit utils', () => {
139139 expect ( getMetricUnitOptions ( 'bad' ) . length ) . toBe ( 18 ) ;
140140 } ) ;
141141
142+ test ( 'getMetricUnitOptions with filterFn' , ( ) => {
143+ let options = getMetricUnitOptions ( 'kg' , false ) ;
144+ expect ( options ) . toEqual ( [
145+ { label : 'g' , value : 'g' } ,
146+ { label : 'mg' , value : 'mg' } ,
147+ { label : 'kg' , value : 'kg' } ,
148+ { label : 'ug' , value : 'ug' } ,
149+ { label : 'ng' , value : 'ng' } ,
150+ ] ) ;
151+
152+ let filterFn = option => option . value !== 'mg' ;
153+ options = getMetricUnitOptions ( 'kg' , false , filterFn ) ;
154+ expect ( options ) . toEqual ( [
155+ { label : 'g' , value : 'g' } ,
156+ { label : 'kg' , value : 'kg' } ,
157+ { label : 'ug' , value : 'ug' } ,
158+ { label : 'ng' , value : 'ng' } ,
159+ ] ) ;
160+
161+ filterFn = option => option . value === 'mg' ;
162+ options = getMetricUnitOptions ( 'kg' , false , filterFn ) ;
163+ expect ( options ) . toEqual ( [ { label : 'mg' , value : 'mg' } ] ) ;
164+
165+ filterFn = option => option . value === 'mg' ;
166+ options = getMetricUnitOptions ( 'mg' , false , filterFn ) ;
167+ expect ( options ) . toEqual ( [ { label : 'mg' , value : 'mg' } ] ) ;
168+
169+ // incompatible units
170+ filterFn = option => option . value === 'mg' ;
171+ options = getMetricUnitOptions ( 'mL' , false , filterFn ) ;
172+ expect ( options ) . toEqual ( [ ] ) ;
173+
174+ // no unit
175+ filterFn = option => option . value === 'mg' ;
176+ options = getMetricUnitOptions ( undefined , false , filterFn ) ;
177+ expect ( options ) . toEqual ( [ { label : 'mg' , value : 'mg' } ] ) ;
178+
179+ // bogus unit will be treated as no unit
180+ filterFn = option => option . value === 'mg' ;
181+ options = getMetricUnitOptions ( 'bogus' , false , filterFn ) ;
182+ expect ( options ) . toEqual ( [ { label : 'mg' , value : 'mg' } ] ) ;
183+ } ) ;
184+
142185 test ( 'getAltUnitKeys' , ( ) => {
143186 const expectedUlOptions = [ 'mL' , 'uL' , 'L' ] ;
144187 expect ( getAltUnitKeys ( 'uL' ) ) . toEqual ( expectedUlOptions ) ;
@@ -234,4 +277,20 @@ describe('areUnitsCompatible', () => {
234277 expect ( areUnitsCompatible ( 'mL' , 'bogus' ) ) . toBeFalsy ( ) ;
235278 expect ( areUnitsCompatible ( 'kg' , 'bogus' ) ) . toBeFalsy ( ) ;
236279 } ) ;
280+
281+ test ( 'comparison of Count units with different labels but same kind' , ( ) => {
282+ expect ( areUnitsCompatible ( 'count' , 'count' ) ) . toBeTruthy ( ) ;
283+ expect ( areUnitsCompatible ( 'blocks' , 'blocks' ) ) . toBeTruthy ( ) ;
284+ expect ( areUnitsCompatible ( 'boxes' , 'cells' ) ) . toBeTruthy ( ) ;
285+ expect ( areUnitsCompatible ( 'kits' , 'packs' ) ) . toBeTruthy ( ) ;
286+ expect ( areUnitsCompatible ( 'pieces' , 'unit' ) ) . toBeTruthy ( ) ;
287+ expect ( areUnitsCompatible ( 'unit' , 'unit' ) ) . toBeTruthy ( ) ;
288+
289+ expect ( areUnitsCompatible ( 'count' , 'count' , true ) ) . toBeTruthy ( ) ;
290+ expect ( areUnitsCompatible ( 'blocks' , 'blocks' , true ) ) . toBeTruthy ( ) ;
291+ expect ( areUnitsCompatible ( 'boxes' , 'cells' , true ) ) . toBeFalsy ( ) ;
292+ expect ( areUnitsCompatible ( 'kits' , 'packs' , true ) ) . toBeFalsy ( ) ;
293+ expect ( areUnitsCompatible ( 'pieces' , 'unit' , true ) ) . toBeFalsy ( ) ;
294+ expect ( areUnitsCompatible ( 'unit' , 'unit' , true ) ) . toBeTruthy ( ) ;
295+ } ) ;
237296} ) ;
0 commit comments