Skip to content

Commit

Permalink
Merge pull request #49 from recurly/fix-currency
Browse files Browse the repository at this point in the history
Fixes issue with useCheckoutPricing currency
  • Loading branch information
chrissrogers authored Mar 30, 2020
2 parents 454994d + a60d287 commit 6ae4129
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 10 deletions.
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
collectCoverage: true,
coverageDirectory: './build/reports/coverage/',
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
setupFilesAfterEnv: ['<rootDir>/jest.setup.js', 'jest-extended'],
transformIgnorePatterns: ['/node_modules/(?!recurly.js).+\\.js$'],
transform: {
'^.+\\.js$': 'babel-jest',
Expand Down
16 changes: 8 additions & 8 deletions lib/use-checkout-pricing.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,6 @@ export default function useCheckoutPricing (initialInputs, handleError = throwEr
const { subscriptions = [], adjustments = [], ...restInputs } = input;
let checkoutPricing = recurly.Pricing.Checkout();

if (Object.keys(restInputs).length) {
checkoutPricing = addRestInputs(restInputs, checkoutPricing);
};

if (adjustments.length) {
checkoutPricing = addAdjustments(adjustments, checkoutPricing);
};

addSubscriptions(subscriptions, checkoutPricing)
.then(() => {
checkoutPricing = checkoutPricing.reprice().done(() => {
Expand All @@ -73,6 +65,14 @@ export default function useCheckoutPricing (initialInputs, handleError = throwEr
setLoading(false);
});

if (adjustments.length) {
checkoutPricing = addAdjustments(adjustments, checkoutPricing);
};

if (Object.keys(restInputs).length) {
checkoutPricing = addRestInputs(restInputs, checkoutPricing);
};

function addAdjustments (adjustments, checkoutPricing) {
return adjustments
.reduce((checkoutPricing, adjustment) => {
Expand Down
46 changes: 46 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"enzyme": "^3.11.0",
"enzyme-adapter-react-16": "^1.15.2",
"jest": "^24.9.0",
"jest-extended": "^0.11.5",
"jest-transform-css": "^2.0.0",
"react": "^16.12.0",
"react-dom": "^16.12.0",
Expand Down
40 changes: 39 additions & 1 deletion test/use-checkout-pricing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,44 @@ describe('useCheckoutPricing', function() {
});
});

describe('Call order', () => {
it('should call checkoutPricing.subscriptions before checkoutPricing.adjustments', async () => {
const initialInput = {
subscriptions: [{ plan: 'basic' }],
adjustments: { itemCode: 'item-1', quantity: 2 },
};

renderUseCheckoutPricing(initialInput);

await act(async () => {
setTimeout(() => {
expect(checkoutPricingReturn.subscription).toHaveBeenCalled();
expect(checkoutPricingReturn.adjustment).toHaveBeenCalled();
expect(checkoutPricingReturn.subscription)
.toHaveBeenCalledBefore(checkoutPricingReturn.adjustment);
}, 10);
});
});

it('should call checkoutPricing.adjustments before checkoutPricing rest inputs', async () => {
const initialInput = {
currency: "USD",
adjustments: { itemCode: 'item-1', quantity: 2 },
};

renderUseCheckoutPricing(initialInput);

await act(async () => {
setTimeout(() => {
expect(checkoutPricingReturn.currency).toHaveBeenCalled();
expect(checkoutPricingReturn.adjustment).toHaveBeenCalled();
expect(checkoutPricingReturn.adjustment)
.toHaveBeenCalledBefore(checkoutPricingReturn.currency);
}, 10);
});
});
});

describe('Error handler', () => {
it('should be passed to .catch', async () => {
const handleError = jest.fn();
Expand All @@ -218,7 +256,7 @@ describe('useCheckoutPricing', function() {
expect(handleError).toHaveBeenCalled();
expect(checkoutPricingReturn.catch).toHaveBeenCalled();
expect(subscriptionPricingReturn.catch).toHaveBeenCalled();
}, 5);
}, 10);
});
});
});
Expand Down

0 comments on commit 6ae4129

Please sign in to comment.