@@ -7,32 +7,76 @@ import TestRenderer from 'react-test-renderer';
77 * Internal dependencies
88 */
99import { usePriceConstraint } from '../use-price-constraints' ;
10+ import { ROUND_UP , ROUND_DOWN } from '../constants' ;
1011
1112describe ( 'usePriceConstraints' , ( ) => {
1213 const TestComponent = ( { price } ) => {
13- const priceConstraint = usePriceConstraint ( price ) ;
14- return < div priceConstraint = { priceConstraint } /> ;
14+ const maxPriceConstraint = usePriceConstraint ( price , 2 , ROUND_UP ) ;
15+ const minPriceConstraint = usePriceConstraint ( price , 2 , ROUND_DOWN ) ;
16+ return (
17+ < div
18+ minPriceConstraint = { minPriceConstraint }
19+ maxPriceConstraint = { maxPriceConstraint }
20+ />
21+ ) ;
1522 } ;
1623
17- it ( 'price constraint should be updated when new price is set' , ( ) => {
18- const renderer = TestRenderer . create ( < TestComponent price = { 10 } /> ) ;
24+ it ( 'max price constraint should be updated when new price is set' , ( ) => {
25+ const renderer = TestRenderer . create ( < TestComponent price = { 1000 } /> ) ;
1926 const container = renderer . root . findByType ( 'div' ) ;
2027
21- expect ( container . props . priceConstraint ) . toBe ( 10 ) ;
28+ expect ( container . props . maxPriceConstraint ) . toBe ( 1000 ) ;
2229
23- renderer . update ( < TestComponent price = { 20 } /> ) ;
30+ renderer . update ( < TestComponent price = { 2000 } /> ) ;
2431
25- expect ( container . props . priceConstraint ) . toBe ( 20 ) ;
32+ expect ( container . props . maxPriceConstraint ) . toBe ( 2000 ) ;
33+ } ) ;
34+
35+ it ( 'min price constraint should be updated when new price is set' , ( ) => {
36+ const renderer = TestRenderer . create ( < TestComponent price = { 1000 } /> ) ;
37+ const container = renderer . root . findByType ( 'div' ) ;
38+
39+ expect ( container . props . minPriceConstraint ) . toBe ( 1000 ) ;
40+
41+ renderer . update ( < TestComponent price = { 2000 } /> ) ;
42+
43+ expect ( container . props . minPriceConstraint ) . toBe ( 2000 ) ;
2644 } ) ;
2745
2846 it ( 'previous price constraint should be preserved when new price is not a infinite number' , ( ) => {
29- const renderer = TestRenderer . create ( < TestComponent price = { 10 } /> ) ;
47+ const renderer = TestRenderer . create ( < TestComponent price = { 1000 } /> ) ;
3048 const container = renderer . root . findByType ( 'div' ) ;
3149
32- expect ( container . props . priceConstraint ) . toBe ( 10 ) ;
50+ expect ( container . props . maxPriceConstraint ) . toBe ( 1000 ) ;
3351
3452 renderer . update ( < TestComponent price = { Infinity } /> ) ;
3553
36- expect ( container . props . priceConstraint ) . toBe ( 10 ) ;
54+ expect ( container . props . maxPriceConstraint ) . toBe ( 1000 ) ;
55+ } ) ;
56+
57+ it ( 'max price constraint should be higher if the price is decimal' , ( ) => {
58+ const renderer = TestRenderer . create (
59+ < TestComponent price = { 1099 } />
60+ ) ;
61+ const container = renderer . root . findByType ( 'div' ) ;
62+
63+ expect ( container . props . maxPriceConstraint ) . toBe ( 2000 ) ;
64+
65+ renderer . update ( < TestComponent price = { 1999 } /> ) ;
66+
67+ expect ( container . props . maxPriceConstraint ) . toBe ( 2000 ) ;
68+ } ) ;
69+
70+ it ( 'min price constraint should be lower if the price is decimal' , ( ) => {
71+ const renderer = TestRenderer . create (
72+ < TestComponent price = { 999 } />
73+ ) ;
74+ const container = renderer . root . findByType ( 'div' ) ;
75+
76+ expect ( container . props . minPriceConstraint ) . toBe ( 0 ) ;
77+
78+ renderer . update ( < TestComponent price = { 1999 } /> ) ;
79+
80+ expect ( container . props . minPriceConstraint ) . toBe ( 1000 ) ;
3781 } ) ;
3882} ) ;
0 commit comments