Skip to content

Commit

Permalink
add failing unit test to show bug, update doc, #421
Browse files Browse the repository at this point in the history
  • Loading branch information
zepumph committed Jan 28, 2022
1 parent 2ade1be commit 7a34438
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
4 changes: 2 additions & 2 deletions js/common/model/RAPModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
}

Expand Down
26 changes: 26 additions & 0 deletions js/common/view/getKeyboardInputSnappingMapperTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
} );

0 comments on commit 7a34438

Please sign in to comment.