Skip to content

Commit

Permalink
Merge pull request #59 from recurly/use-rjs-test-server
Browse files Browse the repository at this point in the history
Rewrites tests for useCheckoutPricing to use test server
  • Loading branch information
chrissrogers committed Apr 30, 2020
2 parents 0a6a7b6 + 09b8a23 commit c126874
Show file tree
Hide file tree
Showing 10 changed files with 605 additions and 460 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ PKG = lib node_modules
test: $(PKG)
@npm test
test-debug: $(PKG)
@node --inspect-brk node_modules/.bin/jest --runInBand
@node --inspect-brk node_modules/.bin/jest --runInBand --forceExit
test-watch: $(PKG)
@npm test -- --watchAll
test-types: $(PKG)
Expand Down
6 changes: 6 additions & 0 deletions globalSetup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Need to import 'regenerator-runtime/runtime' here instead of in
// jest.setup.js, otherwise, import 'recurly.js/test/server' fails because
// it uses async/await.
require('regenerator-runtime/runtime');
require('recurly.js/test/server');
export default async function () {}
3 changes: 2 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ module.exports = {
transform: {
'^.+\\.js$': 'babel-jest',
'.+\\.css$': 'jest-transform-css'
}
},
globalSetup: './globalSetup.js'
}
1 change: 0 additions & 1 deletion jest.setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import Adapter from 'enzyme-adapter-react-16';

// TODO: Lock to CDN distribution
import recurly from 'recurly.js';
import 'regenerator-runtime/runtime';

global.recurly = recurly;

Expand Down
31 changes: 13 additions & 18 deletions lib/use-checkout-pricing.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,9 @@ export default function useCheckoutPricing (initialInputs, handleError = throwEr

addSubscriptions(subscriptions, checkoutPricing)
.then(() => {
if (adjustments.length) {
checkoutPricing = addAdjustments(adjustments, checkoutPricing).reprice();
};
checkoutPricing = addAdjustments(adjustments, checkoutPricing);

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

checkoutPricing = checkoutPricing.reprice().done(() => {
setPricing(checkoutPricing);
Expand All @@ -70,21 +66,24 @@ export default function useCheckoutPricing (initialInputs, handleError = throwEr
});

function addAdjustments (adjustments, checkoutPricing) {
if (!adjustments.length) return checkoutPricing.reprice();

return adjustments
.reduce((checkoutPricing, adjustment) => {
if (adjustment.itemCode) {
return checkoutPricing.adjustment(adjustment).catch(handleError);
}
return checkoutPricing;
return checkoutPricing.adjustment(adjustment).catch(handleError);
}, checkoutPricing)
};

function addRestInputs(restInputs, checkoutPricing) {
const restInputsEntries = Object.entries(restInputs);

if (!restInputsEntries.length) return checkoutPricing.reprice();

const { PRICING_METHODS } = checkoutPricing.pricing;
const exclude = ['reset', 'remove', 'reprice', 'subscription', 'adjustment', 'addon', 'plan'];
const permittedInputs = PRICING_METHODS.filter(method => !exclude.includes(method));

return Object.entries(restInputs).reduce((acc, input) => {
return restInputsEntries.reduce((acc, input) => {
const [method, value] = input;
const shouldCallPricingMethod = value && permittedInputs.includes(method);
return shouldCallPricingMethod ? acc[method](value).catch(handleError) : acc;
Expand All @@ -94,10 +93,6 @@ export default function useCheckoutPricing (initialInputs, handleError = throwEr
function addSubscriptions(subscriptions, checkoutPricing) {
const { subscriptionPricings } = subscriptions.reduce(
({ checkoutPricing, subscriptionPricings }, { plan, tax, addons = [], quantity }) => {
if (!plan) {
return { checkoutPricing, subscriptionPricings }
}

let subscriptionPricing;
if (restInputs.currency) {
subscriptionPricing = recurly.Pricing.Subscription().currency(restInputs.currency).plan(plan, { quantity });
Expand All @@ -120,7 +115,7 @@ export default function useCheckoutPricing (initialInputs, handleError = throwEr
subscriptionPricings: [...subscriptionPricings, subscriptionPricing]
};
},
{ checkoutPricing, subscriptionPricings: [] },
{ checkoutPricing, subscriptionPricings: [] }
);

return Promise.all(subscriptionPricings);
Expand All @@ -137,12 +132,12 @@ export default function useCheckoutPricing (initialInputs, handleError = throwEr

const pricingState = {
price: (pricing && cloneDeep(pricing.price)) || {},
loading,
loading
};

return [pricingState, setInput];
};

function throwError(err) {
export function throwError(err) {
throw err;
};
Loading

0 comments on commit c126874

Please sign in to comment.