Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stokesman committed Apr 25, 2024
1 parent 538e207 commit 701b64d
Showing 1 changed file with 25 additions and 46 deletions.
71 changes: 25 additions & 46 deletions packages/components/src/utils/test/math.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/**
* Internal dependencies
*/
import { add, clamp, subtract, roundClamp } from '../math';

import { add, subtract, clamp, ensureValidStep } from '../math';

describe( 'add', () => {
it( 'should add string and number values', () => {
Expand Down Expand Up @@ -58,61 +59,39 @@ describe( 'clamp', () => {
} );
} );

describe( 'roundClamp', () => {
it( 'should clamp a value between min and max', () => {
expect( roundClamp( 10, 1, 10 ) ).toBe( 10 );
expect( roundClamp( 1, 1, 10 ) ).toBe( 1 );
expect( roundClamp( 0, 1, 10 ) ).toBe( 1 );

expect( roundClamp( 50, 1, 10 ) ).toBe( 10 );
expect( roundClamp( 50, -10, 10 ) ).toBe( 10 );
expect( roundClamp( -50, -10, 10 ) ).toBe( -10 );

expect( roundClamp( '50', 1, 10 ) ).toBe( 10 );
expect( roundClamp( '50', -10, 10 ) ).toBe( 10 );
expect( roundClamp( -50, -10, '10' ) ).toBe( -10 );
describe( 'ensureValidStep', () => {
it( 'should work with number or string values', () => {
expect( ensureValidStep( '49', 0, 10 ) ).toBe( 50 );
expect( ensureValidStep( 49, '0', 10 ) ).toBe( 50 );
expect( ensureValidStep( 49, 0, '10' ) ).toBe( 50 );
} );

it( 'should clamp number or string values', () => {
expect( roundClamp( '50', 1, 10 ) ).toBe( 10 );
expect( roundClamp( '50', -10, 10 ) ).toBe( 10 );
expect( roundClamp( -50, -10, '10' ) ).toBe( -10 );
} );

it( 'should clamp with step', () => {
expect( roundClamp( 40, 0, 100, 10 ) ).toBe( 40 );
expect( roundClamp( 42, 0, 100, 10 ) ).toBe( 40 );
expect( roundClamp( 45, 0, 100, 10 ) ).toBe( 50 );
expect( roundClamp( 49, 0, 100, 10 ) ).toBe( 50 );
expect( roundClamp( 50, 0, 100, 10 ) ).toBe( 50 );
it( 'should round to step', () => {
expect( ensureValidStep( 40, 0, 10 ) ).toBe( 40 );
expect( ensureValidStep( 42, 0, 10 ) ).toBe( 40 );
expect( ensureValidStep( 45, 0, 10 ) ).toBe( 50 );
expect( ensureValidStep( 49, 0, 10 ) ).toBe( 50 );

expect( roundClamp( 50, 0, 100, 15 ) ).toBe( 45 );
expect( roundClamp( 50, 0, 100, '15' ) ).toBe( 45 );
expect( roundClamp( 50, 0, 100, 11 ) ).toBe( 55 );
expect( ensureValidStep( 50, 0, 15 ) ).toBe( 45 );
expect( ensureValidStep( 50, 0, 11 ) ).toBe( 55 );
} );

it( 'should clamp with float in step', () => {
expect( roundClamp( 40, 1, 100, 1 ) ).toBe( 40 );
expect( roundClamp( 40.5, 1, 100, 0.1 ) ).toBe( 40.5 );
expect( roundClamp( 40.05, 1, 100, 0.01 ) ).toBe( 40.05 );
expect( roundClamp( 40.06, 1, 100, 0.1 ) ).toBe( 40.1 );
expect( roundClamp( 40.123005, 1, 100, 0.001 ) ).toBe( 40.123 );
it( 'should round with float in step', () => {
expect( ensureValidStep( 40.5, 1, 0.1 ) ).toBe( 40.5 );
expect( ensureValidStep( 40.05, 1, 0.01 ) ).toBe( 40.05 );
expect( ensureValidStep( 40.06, 1, 0.1 ) ).toBe( 40.1 );
expect( ensureValidStep( 40.123005, 1, 0.001 ) ).toBe( 40.123 );
} );

it( 'should round to steps starting from min', () => {
expect( roundClamp( 10, 0.25, 100, 1 ) ).toBe( 10.25 );
expect( roundClamp( 10, -20.25, 100, 1 ) ).toBe( 9.75 );
expect( roundClamp( 10.5, 0.05, 100, 0.1 ) ).toBe( 10.45 );
expect( roundClamp( 10.51, 0.05, 100, 0.1 ) ).toBe( 10.55 );
} );

it( 'should restrain max to a value that is a multiple of step starting from min', () => {
expect( roundClamp( 10, 1, 10, 2 ) ).toBe( 9 );
expect( roundClamp( 10.125, 0.1, 10, 0.125 ) ).toBe( 9.975 );
expect( ensureValidStep( 10, 0.25, 1 ) ).toBe( 10.25 );
expect( ensureValidStep( 10, -20.25, 1 ) ).toBe( 9.75 );
expect( ensureValidStep( 10.5, 0.05, 0.1 ) ).toBe( 10.45 );
expect( ensureValidStep( 10.51, 0.05, 0.1 ) ).toBe( 10.55 );
} );

it( 'should round with a precision that’s the greater of min and step', () => {
expect( roundClamp( 10.061, 0.01, 20, 0.1 ) ).toBe( 10.11 );
expect( roundClamp( 10.105, 0.1, 20, 0.01 ) ).toBe( 10.11 );
expect( ensureValidStep( 10.061, 0.01, 0.1 ) ).toBe( 10.11 );
expect( ensureValidStep( 10.105, 0.1, 0.01 ) ).toBe( 10.11 );
} );
} );

0 comments on commit 701b64d

Please sign in to comment.