Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement @woocommerce/price-format dependency #29

Open
wants to merge 25 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
6fc3130
Implement @woocommerce/price-format dependency
nielslange Nov 27, 2021
b6204e9
Update dependency @wordpress/components to v19.1.5
renovate-bot Dec 14, 2021
e0ab51f
Remove obsolete checks
nielslange Dec 16, 2021
8d3ba4c
Add default value showing insufficient cart totals progress bar
nielslange Dec 16, 2021
e85f38c
Replace getCurrencyFormat() with getCurrencyFromPriceResponse()
nielslange Dec 16, 2021
fce48d6
Merge branch 'trunk' into add/28-implement-@woocommerce-price-format-…
nielslange Dec 20, 2021
aadbd61
Merge branch 'trunk' into renovate/wordpress-monorepo
nielslange Dec 20, 2021
f7518da
Merge pull request #36 from woocommerce/renovate/wordpress-monorepo
nielslange Dec 20, 2021
2ce76b9
Update wordpress monorepo
renovate-bot Jan 17, 2022
9974eaf
Merge pull request #38 from woocommerce/renovate/wordpress-monorepo
nielslange Jan 24, 2022
63aac05
Remove obsolete icon
nielslange Jan 24, 2022
c48fcc2
Merge pull request #46 from woocommerce/fix/45-remove-obsolete-icon
nielslange Jan 24, 2022
bff7992
Adjust getMinorUnit() function
nielslange Jan 25, 2022
e1c27dc
Use default current totals value from block.json
nielslange Jan 25, 2022
d272664
Remove obsolete attributes from tests
nielslange Jan 25, 2022
fcefbeb
Implement @woocommerce/price-format dependency
nielslange Jan 25, 2022
30f5a83
Remove obsolete checks
nielslange Dec 16, 2021
5c7f6ab
Add default value showing insufficient cart totals progress bar
nielslange Dec 16, 2021
2cf0023
Replace getCurrencyFormat() with getCurrencyFromPriceResponse()
nielslange Dec 16, 2021
1d9e48d
Adjust getMinorUnit() function
nielslange Jan 25, 2022
706a248
Use default current totals value from block.json
nielslange Jan 25, 2022
96d7c07
Remove obsolete attributes from tests
nielslange Jan 25, 2022
0aa53b1
Interim Jest mock commit
nielslange Jan 28, 2022
e6572fb
Merge branch 'add/28-implement-@woocommerce-price-format-dependency' …
nielslange Jan 28, 2022
995f5a0
Fix failing integration test
nielslange Jan 28, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20,686 changes: 7,382 additions & 13,304 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
"prettier": "npm:[email protected]"
},
"devDependencies": {
"@wordpress/components": "19.2.0",
"@testing-library/jest-dom": "5.16.1",
"@testing-library/react": "12.1.2",
"@wordpress/components": "19.1.0",
"@wordpress/dependency-extraction-webpack-plugin": "3.2.1",
"@wordpress/i18n": "4.2.4",
"@wordpress/prettier-config": "1.1.1",
"@wordpress/scripts": "19.2.2"
"@wordpress/scripts": "19.2.3"
}
}
1 change: 1 addition & 0 deletions src/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import ProgressLabel from './components/progress-label';
import ProgressBar from './components/progress-bar';
import './style.scss';

export default function Block( attributes ) {
return (
Expand Down
8 changes: 7 additions & 1 deletion src/components/progress-bar/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
const ProgressBar = ( { currentTotal, freeShippingFrom } ) => {
/**
* Internal dependencies
*/
import { getCurrentTotal } from '../../util';

const ProgressBar = ( { freeShippingFrom, cart } ) => {
const currentTotal = getCurrentTotal( cart );
const progress = ( currentTotal / freeShippingFrom ) * 100;
const divWidth = ( progress > 100 ? 100 : progress ) + '%';
const divStyle = { width: divWidth };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import '@testing-library/jest-dom';
/**
* Internal dependencies
*/
import ProgressBar from './../index';
import ProgressBar from './index';

const outer = '.wc-free-shipping-progress-bar__outer';
const inner = '.wc-free-shipping-progress-bar__inner';
Expand All @@ -24,22 +24,34 @@ describe( 'The ProgressBar component', () => {
} );

it( 'shows the style for a full bar correctly', () => {
render( <ProgressBar freeShippingFrom="4" currentTotal="4" /> );
const cart = {
cartTotals: { total_price: 100, currency_minor_unit: 0 },
};
render( <ProgressBar freeShippingFrom="100" cart={ cart } /> );
expect( document.querySelector( inner ) ).toHaveStyle( 'width: 100%' );
} );

it( 'shows the style for a threequarter bar correctly', () => {
render( <ProgressBar freeShippingFrom="4" currentTotal="3" /> );
const cart = {
cartTotals: { total_price: 75, currency_minor_unit: 0 },
};
render( <ProgressBar freeShippingFrom="100" cart={ cart } /> );
expect( document.querySelector( inner ) ).toHaveStyle( 'width: 75%' );
} );

it( 'shows the style for a half bar correctly', () => {
render( <ProgressBar freeShippingFrom="4" currentTotal="2" /> );
const cart = {
cartTotals: { total_price: 50, currency_minor_unit: 0 },
};
render( <ProgressBar freeShippingFrom="100" cart={ cart } /> );
expect( document.querySelector( inner ) ).toHaveStyle( 'width: 50%' );
} );

it( 'shows the style for an empty bar correctly', () => {
render( <ProgressBar freeShippingFrom="4" currentTotal="0" /> );
const cart = {
cartTotals: { total_price: 0, currency_minor_unit: 0 },
};
render( <ProgressBar freeShippingFrom="1" cart={ cart } /> );
expect( document.querySelector( inner ) ).toHaveStyle( 'width: 0%' );
} );
} );
41 changes: 0 additions & 41 deletions src/components/progress-label/__test__/index.test.js

This file was deleted.

23 changes: 20 additions & 3 deletions src/components/progress-label/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
/**
* External dependencies
*/
import {
formatPrice,
getCurrencyFromPriceResponse,
} from '@woocommerce/price-format';

/**
* Internal dependencies
*/
import { getCurrentTotal, getMinorUnit, getRemaining } from '../../util';

const ProgressLabel = ( {
currentTotal,
freeShippingFrom,
labelInsufficientTotals,
labelSufficientTotals,
cart,
} ) => {
const remaining = Number( freeShippingFrom - currentTotal ).toFixed( 2 );
const currentTotal = getCurrentTotal( cart );
const minorUnit = getMinorUnit( cart );
const remaining = getRemaining( freeShippingFrom, currentTotal, minorUnit );
const currency = getCurrencyFromPriceResponse( cart );
const price = formatPrice( remaining, currency );
const message =
remaining > 0
? labelInsufficientTotals.replace( '{amount}', remaining )
? labelInsufficientTotals.replace( '{amount}', price )
: labelSufficientTotals;

return (
Expand Down
71 changes: 71 additions & 0 deletions src/components/progress-label/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* External dependencies
*/
import { render, screen } from '@testing-library/react';
import '@testing-library/jest-dom';

/**
* Internal dependencies
*/
import ProgressLabel from './index';

jest.mock(
'@woocommerce/price-format',
() => ( {
formatPrice: jest.fn().mockImplementation( () => '$100.00' ),
getCurrencyFromPriceResponse: jest.fn().mockImplementation( () => ( {
code: 'USD',
prefix: '$',
} ) ),
} ),
{ virtual: true }
);

const label = '.wc-free-shipping-progress-bar__label';

describe( 'The ProgressLabel component', () => {
/**
* Test is the <ProgressLabel /> component renderes correctly without props.
*/
it( 'shows the classnames of the div correctly', () => {
render( <ProgressLabel /> );
expect( document.querySelector( label ) ).toBeInTheDocument();
} );

/**
* Test is the <ProgressLabel /> component shows the correct message for the
* insufficient totals when shipping is free from $200.00, but the total
* cart value is $100.00.
*/
it( 'shows the message for insufficient totals correctly', () => {
const labelBefore = 'Spend only {amount} more to get free shipping!';
const labelAfter = 'Spend only $100.00 more to get free shipping!';
const args = {
freeShippingFrom: 200,
labelInsufficientTotals: labelBefore,
cart: {
cartTotals: { total_price: 100, currency_minor_unit: 0 },
},
};

render( <ProgressLabel { ...args } /> );
expect( screen.getByText( labelAfter ) ).toBeInTheDocument();
} );

/**
* Test is the <ProgressLabel /> component shows the correct message for the
* sufficient totals when shipping is free from $100.00, and the total cart
* value equals $100.00.
*/
it( 'shows the message for sufficient totals correctly', () => {
const label = 'You have qualified for free shipping. Great job!';
const args = {
freeShippingFrom: 100,
labelSufficientTotals: label,
cart: { cartTotals: { total_price: 100, currency_minor_unit: 0 } },
};

const progressbar = render( <ProgressLabel { ...args } /> );
expect( screen.getByText( label ) ).toBeInTheDocument();
} );
} );
1 change: 0 additions & 1 deletion src/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import ProgressBar from './components/progress-bar';
import useViewSwitcher from './components/use-view-switcher';
import { notice } from './constants';
import { Icon, progressBarHalf, progressBarFull } from './icons';
import './style.scss';

const BlockSettings = ( { attributes, setAttributes } ) => {
const { freeShippingFrom } = attributes;
Expand Down
86 changes: 0 additions & 86 deletions src/progress-bar-icon.js

This file was deleted.

47 changes: 47 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Internal dependencies
*/
import metadata from '../block.json';

/**
* Get the current totals.
*
* @param {Object} cart The cart object.
* @returns {Number} The totals of the cart object, if available, else the totals of the cart progress bar preview.
*/
export function getCurrentTotal( cart ) {
if ( undefined !== cart ) {
const total_price = cart.cartTotals.total_price;
const minor_unit = cart.cartTotals.currency_minor_unit;

return total_price / Math.pow( 10, minor_unit );
mikejolley marked this conversation as resolved.
Show resolved Hide resolved
}

return metadata.attributes.currentTotal.default;
}

/**
* Get the minor unit.
*
* @param {Object} cart The cart object.
* @returns {Number} The totals of the cart object, if available, else 0.
*/
export function getMinorUnit( cart ) {
return cart?.cartTotals?.currency_minor_unit || 0;
}

/**
* Get the remaining value.
*
* @param {Number} freeShippingFrom The free shipping from value.
* @param {Number} currentTotal The current total value.
* @param {Number} minorUnit The minor unit value.
* @returns {Number} The difference between freeShippingFrom and currentTotal.
*/
export function getRemaining( freeShippingFrom, currentTotal, minorUnit ) {
const remaining = freeShippingFrom - currentTotal;
const power = Math.pow( 10, minorUnit );
const result = remaining.toFixed( minorUnit ) * power;

return result;
mikejolley marked this conversation as resolved.
Show resolved Hide resolved
}
Loading