Skip to content

Commit

Permalink
Remove parseFloat calls
Browse files Browse the repository at this point in the history
  • Loading branch information
mirka committed Sep 6, 2022
1 parent cb27d44 commit 08a71e9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
9 changes: 3 additions & 6 deletions packages/components/src/focal-point-picker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,8 @@ export function FocalPointPicker( {
Math.round( n * 1e2 ) / 1e2;

return {
// @ts-expect-error: TODO: Is this parseFloat necessary?
x: roundToTwoDecimalPlaces( parseFloat( resolvedValue.x ) ),
// @ts-expect-error: TODO: Is this parseFloat necessary?
y: roundToTwoDecimalPlaces( parseFloat( resolvedValue.y ) ),
x: roundToTwoDecimalPlaces( resolvedValue.x ),
y: roundToTwoDecimalPlaces( resolvedValue.y ),
};
};

Expand All @@ -212,8 +210,7 @@ export function FocalPointPicker( {
const delta =
code === 'ArrowUp' || code === 'ArrowLeft' ? -1 * step : step;
const axis = code === 'ArrowUp' || code === 'ArrowDown' ? 'y' : 'x';
// @ts-expect-error: TODO: Is this parseFloat necessary?
value[ axis ] = parseFloat( value[ axis ] ) + delta;
value[ axis ] = value[ axis ] + delta;
onChange?.( getFinalValue( value ) );
};

Expand Down
6 changes: 2 additions & 4 deletions packages/components/src/focal-point-picker/stories/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,8 @@ Snapping.args = {

const threshold = 0.05;

// @ts-expect-error: TODO: Is this parseFloat necessary?
let x = parseFloat( value.x );
// @ts-expect-error: TODO: Is this parseFloat necessary?
let y = parseFloat( value.y );
let x = value.x;
let y = value.y;

snapValues.x.forEach( ( snapValue ) => {
if ( snapValue - threshold < x && x < snapValue + threshold ) {
Expand Down
32 changes: 26 additions & 6 deletions packages/components/src/focal-point-picker/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ describe( 'FocalPointPicker', () => {
await user.keyboard( '[ArrowUp]' );

expect( spy ).toHaveBeenCalled();
expect( spyChange ).toHaveBeenCalledWith( {
x: '0.91',
y: '0.42',
expect( spyChange ).toHaveBeenLastCalledWith( {
x: 0.91,
y: 0.42,
} );
} );
} );
Expand Down Expand Up @@ -143,10 +143,30 @@ describe( 'FocalPointPicker', () => {
await user.click( dragArea );
await user.keyboard( '[ArrowDown]' );

expect( spyChange ).toHaveBeenCalledWith( {
x: '0.14',
y: '0.63',
expect( spyChange ).toHaveBeenLastCalledWith( {
x: 0.14,
y: 0.63,
} );
} );
} );

describe( 'value handling', () => {
it( 'should handle legacy string values', () => {
const onChangeSpy = jest.fn();
render(
<Picker
value={ { x: '0.1', y: '0.2' } }
onChange={ onChangeSpy }
/>
);

expect(
screen.getByRole( 'spinbutton', { name: 'Left' } ).value
).toBe( '10' );
expect(
screen.getByRole( 'spinbutton', { name: 'Top' } ).value
).toBe( '20' );
expect( onChangeSpy ).not.toHaveBeenCalled();
} );
} );
} );

0 comments on commit 08a71e9

Please sign in to comment.