From 7a344388346ae7a7c25d156944d2ee8210435f66 Mon Sep 17 00:00:00 2001 From: zepumph Date: Fri, 28 Jan 2022 10:01:59 -0700 Subject: [PATCH] add failing unit test to show bug, update doc, https://github.com/phetsims/ratio-and-proportion/issues/421 --- js/common/model/RAPModel.ts | 4 +-- .../getKeyboardInputSnappingMapperTests.ts | 26 +++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/js/common/model/RAPModel.ts b/js/common/model/RAPModel.ts index 58b822ae..6ee16417 100644 --- a/js/common/model/RAPModel.ts +++ b/js/common/model/RAPModel.ts @@ -231,7 +231,7 @@ unclampedFitness: ${unclampedFitness} } /** - * Given a ratio component (antecedent or consequent), determine what it should be to make the current ratio equal to + * Given a ratioTerm, determine how the provided RatioTerm should change to, to make the current ratio equal to * the target ratio. */ public getIdealValueForTerm( ratioTerm: RatioTerm ): number { @@ -241,7 +241,7 @@ unclampedFitness: ${unclampedFitness} if ( ratioTerm === RatioTerm.CONSEQUENT ) { return this.ratio.tupleProperty.value.antecedent / this.targetRatioProperty.value; } - assert && assert( false, 'Invalidat ratioTerm' ); + assert && assert( false, 'Invalid ratioTerm' ); return -1; } diff --git a/js/common/view/getKeyboardInputSnappingMapperTests.ts b/js/common/view/getKeyboardInputSnappingMapperTests.ts index 898fffaa..5b814a94 100644 --- a/js/common/view/getKeyboardInputSnappingMapperTests.ts +++ b/js/common/view/getKeyboardInputSnappingMapperTests.ts @@ -165,3 +165,29 @@ QUnit.test( 'test case of 1/2 "ish"', assert => { ratioTupleProperty.value = ratioTupleProperty.value.withAntecedent( newValue ); assert.ok( ratioTupleProperty.value.antecedent === 0.22, 'step up from ideal past next even step' ); } ); + +QUnit.test( 'test case of snapping on the 1/2 tick mark (not snapping to in proportion', assert => { + // Buggy case from https://github.com/phetsims/ratio-and-proportion/issues/354#issuecomment-796067400 + + const model = new RAPModel( Tandem.OPT_OUT ); + const ratioTupleProperty = model.ratio.tupleProperty; + + const getIdealValue = () => model.getIdealValueForTerm( RatioTerm.ANTECEDENT ); + const snapConserveFunction = getKeyboardInputSnappingMapper( getIdealValue, keyboardStep, keyboardStep * rapConstants.SHIFT_KEY_MULTIPLIER ); + model.targetRatioProperty.value = 1 / 2; + ratioTupleProperty.value = new RAPRatioTuple( 0.25, 0.5 ); + + let newValue = null; + + newValue = snapConserveFunction( 0.3, ratioTupleProperty.value.antecedent, false, model.inProportionProperty.value ); + ratioTupleProperty.value = ratioTupleProperty.value.withAntecedent( newValue ); + assert.ok( ratioTupleProperty.value.antecedent === 0.3, 'normal step' ); + + newValue = snapConserveFunction( 0.31, ratioTupleProperty.value.antecedent, true, model.inProportionProperty.value ); + ratioTupleProperty.value = ratioTupleProperty.value.withAntecedent( newValue ); + assert.ok( ratioTupleProperty.value.antecedent === 0.31, 'normal shift step up' ); + + newValue = snapConserveFunction( 0.26, ratioTupleProperty.value.antecedent, false, model.inProportionProperty.value ); + ratioTupleProperty.value = ratioTupleProperty.value.withAntecedent( newValue ); + // assert.ok( ratioTupleProperty.value.antecedent === 0.26, 'full step down' ); // FAILING RIGHT NOW, TODO: see https://github.com/phetsims/ratio-and-proportion/issues/421 +} );