Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit a52690f

Browse files
authored
remove minorUnit and revert tests (#1450)
1 parent 4c1c584 commit a52690f

File tree

2 files changed

+26
-28
lines changed

2 files changed

+26
-28
lines changed

assets/js/blocks/price-filter/test/use-price-constraints.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import { ROUND_UP, ROUND_DOWN } from '../constants';
1111

1212
describe( 'usePriceConstraints', () => {
1313
const TestComponent = ( { price } ) => {
14-
const maxPriceConstraint = usePriceConstraint( price, 2, ROUND_UP );
15-
const minPriceConstraint = usePriceConstraint( price, 2, ROUND_DOWN );
14+
const maxPriceConstraint = usePriceConstraint( price, ROUND_UP );
15+
const minPriceConstraint = usePriceConstraint( price, ROUND_DOWN );
1616
return (
1717
<div
1818
minPriceConstraint={ minPriceConstraint }
@@ -22,61 +22,61 @@ describe( 'usePriceConstraints', () => {
2222
};
2323

2424
it( 'max price constraint should be updated when new price is set', () => {
25-
const renderer = TestRenderer.create( <TestComponent price={ 1000 } /> );
25+
const renderer = TestRenderer.create( <TestComponent price={ 10 } /> );
2626
const container = renderer.root.findByType( 'div' );
2727

28-
expect( container.props.maxPriceConstraint ).toBe( 1000 );
28+
expect( container.props.maxPriceConstraint ).toBe( 10 );
2929

30-
renderer.update( <TestComponent price={ 2000 } /> );
30+
renderer.update( <TestComponent price={ 20 } /> );
3131

32-
expect( container.props.maxPriceConstraint ).toBe( 2000 );
32+
expect( container.props.maxPriceConstraint ).toBe( 20 );
3333
} );
3434

3535
it( 'min price constraint should be updated when new price is set', () => {
36-
const renderer = TestRenderer.create( <TestComponent price={ 1000 } /> );
36+
const renderer = TestRenderer.create( <TestComponent price={ 10 } /> );
3737
const container = renderer.root.findByType( 'div' );
3838

39-
expect( container.props.minPriceConstraint ).toBe( 1000 );
39+
expect( container.props.minPriceConstraint ).toBe( 10 );
4040

41-
renderer.update( <TestComponent price={ 2000 } /> );
41+
renderer.update( <TestComponent price={ 20 } /> );
4242

43-
expect( container.props.minPriceConstraint ).toBe( 2000 );
43+
expect( container.props.minPriceConstraint ).toBe( 20 );
4444
} );
4545

4646
it( 'previous price constraint should be preserved when new price is not a infinite number', () => {
47-
const renderer = TestRenderer.create( <TestComponent price={ 1000 } /> );
47+
const renderer = TestRenderer.create( <TestComponent price={ 10 } /> );
4848
const container = renderer.root.findByType( 'div' );
4949

50-
expect( container.props.maxPriceConstraint ).toBe( 1000 );
50+
expect( container.props.maxPriceConstraint ).toBe( 10 );
5151

5252
renderer.update( <TestComponent price={ Infinity } /> );
5353

54-
expect( container.props.maxPriceConstraint ).toBe( 1000 );
54+
expect( container.props.maxPriceConstraint ).toBe( 10 );
5555
} );
5656

5757
it( 'max price constraint should be higher if the price is decimal', () => {
5858
const renderer = TestRenderer.create(
59-
<TestComponent price={ 1099 } />
59+
<TestComponent price={ 10.99 } />
6060
);
6161
const container = renderer.root.findByType( 'div' );
6262

63-
expect( container.props.maxPriceConstraint ).toBe( 2000 );
63+
expect( container.props.maxPriceConstraint ).toBe( 20 );
6464

65-
renderer.update( <TestComponent price={ 1999 } /> );
65+
renderer.update( <TestComponent price={ 19.99 } /> );
6666

67-
expect( container.props.maxPriceConstraint ).toBe( 2000 );
67+
expect( container.props.maxPriceConstraint ).toBe( 20 );
6868
} );
6969

7070
it( 'min price constraint should be lower if the price is decimal', () => {
7171
const renderer = TestRenderer.create(
72-
<TestComponent price={ 999 } />
72+
<TestComponent price={ 9.99 } />
7373
);
7474
const container = renderer.root.findByType( 'div' );
7575

7676
expect( container.props.minPriceConstraint ).toBe( 0 );
7777

78-
renderer.update( <TestComponent price={ 1999 } /> );
78+
renderer.update( <TestComponent price={ 19.99 } /> );
7979

80-
expect( container.props.minPriceConstraint ).toBe( 1000 );
80+
expect( container.props.minPriceConstraint ).toBe( 10 );
8181
} );
8282
} );

assets/js/blocks/price-filter/use-price-constraints.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,18 @@ import { ROUND_UP, ROUND_DOWN } from './constants';
1212
* Return the price constraint.
1313
*
1414
* @param {number} price Price in minor unit, e.g. cents.
15-
* @param {number} minorUnit Price minor unit (number of digits after the decimal separator).
1615
* @param {ROUND_UP|ROUND_DOWN} direction Rounding flag whether we round up or down.
1716
*/
18-
export const usePriceConstraint = ( price, minorUnit, direction ) => {
19-
const step = 10 * 10 ** minorUnit;
17+
export const usePriceConstraint = ( price, direction ) => {
2018
let currentConstraint;
2119
if ( direction === ROUND_UP ) {
2220
currentConstraint = isNaN( price )
2321
? null
24-
: Math.ceil( parseFloat( price, 10 ) / step ) * step;
22+
: Math.ceil( parseFloat( price, 10 ) / 10 ) * 10;
2523
} else if ( direction === ROUND_DOWN ) {
2624
currentConstraint = isNaN( price )
2725
? null
28-
: Math.floor( parseFloat( price, 10 ) / step ) * step;
26+
: Math.floor( parseFloat( price, 10 ) / 10 ) * 10;
2927
}
3028

3129
const previousConstraint = usePrevious( currentConstraint, ( val ) =>
@@ -36,9 +34,9 @@ export const usePriceConstraint = ( price, minorUnit, direction ) => {
3634
: previousConstraint;
3735
};
3836

39-
export default ( { minPrice, maxPrice, minorUnit } ) => {
37+
export default ( { minPrice, maxPrice } ) => {
4038
return {
41-
minConstraint: usePriceConstraint( minPrice, minorUnit, ROUND_DOWN ),
42-
maxConstraint: usePriceConstraint( maxPrice, minorUnit, ROUND_UP ),
39+
minConstraint: usePriceConstraint( minPrice, ROUND_DOWN ),
40+
maxConstraint: usePriceConstraint( maxPrice, ROUND_UP ),
4341
};
4442
};

0 commit comments

Comments
 (0)