couponable
Advanced tools
Comparing version 6.3.0 to 6.4.0
68
index.js
@@ -17,3 +17,3 @@ // if the comma or decimal portions of the price need internationalization | ||
const formattedAmount = unitAmount / (10 ** currencyDecimalPlaces); | ||
const formattedAmount = unitAmount / 10 ** currencyDecimalPlaces; | ||
@@ -27,3 +27,3 @@ return intlFormat.format(formattedAmount); | ||
if (percentOff) { | ||
discount = amountInCents * percentOff / 100; | ||
discount = (amountInCents * percentOff) / 100; | ||
} else if (amountOffInCents) { | ||
@@ -51,3 +51,3 @@ discount = amountOffInCents; | ||
var quantity = orderItem.quantity || 0, | ||
total = orderItem.priceInCents; | ||
total = orderItem.priceInCents; | ||
let totalAmountOffInCents; | ||
@@ -64,3 +64,3 @@ | ||
if (orderItem.isBulkPurchase && orderItem.coupon.amountOffInCents) { | ||
totalAmountOffInCents = orderItem.coupon.amountOffInCents * quantity | ||
totalAmountOffInCents = orderItem.coupon.amountOffInCents * quantity; | ||
} | ||
@@ -70,3 +70,9 @@ | ||
} | ||
total = Math.round(discountable(total, orderItem.coupon.percentOff, totalAmountOffInCents || orderItem.coupon.amountOffInCents)); | ||
total = Math.round( | ||
discountable( | ||
total, | ||
orderItem.coupon.percentOff, | ||
totalAmountOffInCents || orderItem.coupon.amountOffInCents | ||
) | ||
); | ||
} | ||
@@ -82,8 +88,20 @@ | ||
} else { | ||
let quantity = orderItem.quantity || 0, | ||
total = orderItem.price.unitAmount; | ||
let quantity = orderItem.quantity || 0; | ||
let total; | ||
if (orderItem.purchasableType === 'pickableGroup') { | ||
total = | ||
orderItem.price.unitsAmount[orderItem.learningPaths.length + orderItem.courses.length - 1]; | ||
} else if (orderItem.purchasableType === 'bundle') { | ||
total = | ||
orderItem.interval === 'year' | ||
? orderItem.price.annualUnitAmount | ||
: orderItem.price.unitAmount; | ||
} else { | ||
total = orderItem.price.unitAmount; | ||
} | ||
let totalUnitAmountOff; | ||
if (orderItem.variation) { | ||
total += (orderItem.variation.unitAmount || orderItem.variation.priceInCents || 0); | ||
total += orderItem.variation.unitAmount || orderItem.variation.priceInCents || 0; | ||
} | ||
@@ -96,3 +114,3 @@ | ||
if (orderItem.isBulkPurchase && orderItem.coupon.amountOffInCents) { | ||
totalUnitAmountOff = orderItem.coupon.amountOffInCents * quantity | ||
totalUnitAmountOff = orderItem.coupon.amountOffInCents * quantity; | ||
} | ||
@@ -102,3 +120,9 @@ | ||
} | ||
total = Math.round(discountable(total, orderItem.coupon.percentOff, totalUnitAmountOff || orderItem.coupon.amountOffInCents)); | ||
total = Math.round( | ||
discountable( | ||
total, | ||
orderItem.coupon.percentOff, | ||
totalUnitAmountOff || orderItem.coupon.amountOffInCents | ||
) | ||
); | ||
} | ||
@@ -155,3 +179,8 @@ return total * quantity; | ||
function totalLineTwo(orderItem, currencySymbol) { | ||
if (orderItem.purchasableType === 'bundle' && !orderItem.gift && orderItem.coupon && orderItem.coupon.duration !== 'forever') { | ||
if ( | ||
orderItem.purchasableType === 'bundle' && | ||
!orderItem.gift && | ||
orderItem.coupon && | ||
orderItem.coupon.duration !== 'forever' | ||
) { | ||
return priceFormat(totalRecurring(orderItem), currencySymbol) + ' / ' + orderItem.interval; | ||
@@ -164,4 +193,13 @@ } else { | ||
function totalLineTwoMulticurrency(orderItem, currencyCode) { | ||
if (orderItem.purchasableType === 'bundle' && !orderItem.gift && orderItem.coupon && orderItem.coupon.duration !== 'forever') { | ||
return priceFormatMultiCurrency(totalRecurringMulticurrency(orderItem), currencyCode) + ' / ' + orderItem.interval; | ||
if ( | ||
orderItem.purchasableType === 'bundle' && | ||
!orderItem.gift && | ||
orderItem.coupon && | ||
orderItem.coupon.duration !== 'forever' | ||
) { | ||
return ( | ||
priceFormatMultiCurrency(totalRecurringMulticurrency(orderItem), currencyCode) + | ||
' / ' + | ||
orderItem.interval | ||
); | ||
} else { | ||
@@ -225,3 +263,5 @@ return null; | ||
} else { | ||
define('couponable', ['exports'], function (__exports__) { __exports__['default'] = couponable; }); | ||
define('couponable', ['exports'], function (__exports__) { | ||
__exports__['default'] = couponable; | ||
}); | ||
} |
{ | ||
"name": "couponable", | ||
"description": "Helper functions for dealing with coupons.", | ||
"version": "6.3.0", | ||
"version": "6.4.0", | ||
"author": "Chris McC", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
876
test.js
@@ -15,213 +15,339 @@ var couponable = require('.'); | ||
describe('totalDueNow', function() { | ||
it('calculates correctly', function() { | ||
assert.equal(totalDueNow({ | ||
quantity: 2, | ||
variation: {priceInCents: 2}, | ||
priceInCents: 2 | ||
}), 8); | ||
describe('totalDueNow', function () { | ||
it('calculates correctly', function () { | ||
assert.equal( | ||
totalDueNow({ | ||
quantity: 2, | ||
variation: { priceInCents: 2 }, | ||
priceInCents: 2 | ||
}), | ||
8 | ||
); | ||
}); | ||
it('calculates correctly when a total is provided', function() { | ||
assert.equal(totalDueNow({ | ||
total: 8 | ||
}), 8); | ||
it('calculates correctly when a total is provided', function () { | ||
assert.equal( | ||
totalDueNow({ | ||
total: 8 | ||
}), | ||
8 | ||
); | ||
}); | ||
it('handles coupons correctly', function() { | ||
assert.equal(totalDueNow({ | ||
quantity: 1, | ||
coupon: {percentOff: 50}, | ||
priceInCents: 2 | ||
}), 1); | ||
it('handles coupons correctly', function () { | ||
assert.equal( | ||
totalDueNow({ | ||
quantity: 1, | ||
coupon: { percentOff: 50 }, | ||
priceInCents: 2 | ||
}), | ||
1 | ||
); | ||
}); | ||
it('handles coupons for bundles & bulk purchases correctly', function() { | ||
assert.equal(totalDueNow({ | ||
quantity: 10, | ||
coupon: {amountOffInCents: 5}, | ||
priceInCents: 10, | ||
purchasableType: 'bundle' | ||
}), 95); | ||
it('handles coupons for bundles & bulk purchases correctly', function () { | ||
assert.equal( | ||
totalDueNow({ | ||
quantity: 10, | ||
coupon: { amountOffInCents: 5 }, | ||
priceInCents: 10, | ||
purchasableType: 'bundle' | ||
}), | ||
95 | ||
); | ||
assert.equal(totalDueNow({ | ||
quantity: 10, | ||
coupon: {amountOffInCents: 5}, | ||
priceInCents: 10, | ||
purchasableType: 'course', | ||
isBulkPurchase: true | ||
}), 50); | ||
assert.equal( | ||
totalDueNow({ | ||
quantity: 10, | ||
coupon: { amountOffInCents: 5 }, | ||
priceInCents: 10, | ||
purchasableType: 'course', | ||
isBulkPurchase: true | ||
}), | ||
50 | ||
); | ||
}); | ||
}); | ||
describe('totalDueNowMulticurrency', function() { | ||
it('calculates correctly mc', function() { | ||
assert.equal(totalDueNowMulticurrency({ | ||
quantity: 2, | ||
variation: { priceInCents: 2 }, | ||
price: { | ||
unitAmount: 2 | ||
} | ||
}), 8); | ||
describe('totalDueNowMulticurrency', function () { | ||
it('calculates correctly mc', function () { | ||
assert.equal( | ||
totalDueNowMulticurrency({ | ||
quantity: 2, | ||
variation: { priceInCents: 2 }, | ||
price: { | ||
unitAmount: 2 | ||
} | ||
}), | ||
8 | ||
); | ||
}); | ||
it('calculates correctly when a total is provided', function() { | ||
assert.equal(totalDueNowMulticurrency({ | ||
total: 8 | ||
}), 8); | ||
it('calculates correctly when a total is provided', function () { | ||
assert.equal( | ||
totalDueNowMulticurrency({ | ||
total: 8 | ||
}), | ||
8 | ||
); | ||
}); | ||
it('handles coupons correctly', function() { | ||
assert.equal(totalDueNowMulticurrency({ | ||
quantity: 1, | ||
coupon: { percentOff: 50 }, | ||
price: { | ||
unitAmount: 2 | ||
} | ||
}), 1); | ||
it('handles coupons correctly', function () { | ||
assert.equal( | ||
totalDueNowMulticurrency({ | ||
quantity: 1, | ||
coupon: { percentOff: 50 }, | ||
price: { | ||
unitAmount: 2 | ||
} | ||
}), | ||
1 | ||
); | ||
}); | ||
it('handles coupons for bundles & bulk purchases correctly', function() { | ||
assert.equal(totalDueNowMulticurrency({ | ||
quantity: 10, | ||
coupon: { amountOffInCents: 5 }, | ||
price: { | ||
unitAmount: 10 | ||
}, | ||
purchasableType: 'bundle' | ||
}), 95); | ||
it('handles coupons for bundles & bulk purchases correctly', function () { | ||
assert.equal( | ||
totalDueNowMulticurrency({ | ||
quantity: 10, | ||
coupon: { amountOffInCents: 5 }, | ||
price: { | ||
unitAmount: 10 | ||
}, | ||
purchasableType: 'bundle' | ||
}), | ||
95 | ||
); | ||
assert.equal(totalDueNowMulticurrency({ | ||
quantity: 10, | ||
coupon: { amountOffInCents: 5 }, | ||
price: { | ||
unitAmount: 10 | ||
}, | ||
purchasableType: 'course', | ||
isBulkPurchase: true | ||
}), 50); | ||
assert.equal( | ||
totalDueNowMulticurrency({ | ||
quantity: 10, | ||
coupon: { amountOffInCents: 5 }, | ||
price: { | ||
unitAmount: 10 | ||
}, | ||
purchasableType: 'course', | ||
isBulkPurchase: true | ||
}), | ||
50 | ||
); | ||
}); | ||
it('handles pickable groups', function () { | ||
assert.equal( | ||
totalDueNowMulticurrency({ | ||
quantity: 1, | ||
coupon: { percentOff: 50 }, | ||
learningPaths: ['lp-1'], | ||
courses: ['course-1'], | ||
price: { | ||
unitsAmount: [500, 600] | ||
}, | ||
purchasableType: 'pickableGroup' | ||
}), | ||
300 | ||
); | ||
assert.equal( | ||
totalDueNowMulticurrency({ | ||
quantity: 1, | ||
learningPaths: ['lp-1'], | ||
courses: [], | ||
price: { | ||
unitsAmount: [500, 600] | ||
}, | ||
purchasableType: 'pickableGroup' | ||
}), | ||
500 | ||
); | ||
}); | ||
it('handles bundles', function () { | ||
assert.equal( | ||
totalDueNowMulticurrency({ | ||
quantity: 1, | ||
interval: 'year', | ||
price: { | ||
annualUnitAmount: 10000, | ||
unitAmount: 400 | ||
}, | ||
purchasableType: 'bundle' | ||
}), | ||
10000 | ||
); | ||
}); | ||
}); | ||
describe('totalRecurring', function() { | ||
it('returns null for non-bundles', function() { | ||
assert.equal(totalRecurring({purchasableType: 'course'}), null); | ||
describe('totalRecurring', function () { | ||
it('returns null for non-bundles', function () { | ||
assert.equal(totalRecurring({ purchasableType: 'course' }), null); | ||
}); | ||
it('returns priceInCents for bundles without coupons', function() { | ||
assert.equal(totalRecurring({purchasableType: 'bundle', priceInCents: 2}), 2); | ||
it('returns priceInCents for bundles without coupons', function () { | ||
assert.equal(totalRecurring({ purchasableType: 'bundle', priceInCents: 2 }), 2); | ||
}); | ||
it('returns totalDueNow for bundles with forever coupons', function() { | ||
assert.equal(totalRecurring({purchasableType: 'bundle', priceInCents: 2, quantity: 1, coupon: {duration: 'forever', percentOff: 50}}), 1); | ||
it('returns totalDueNow for bundles with forever coupons', function () { | ||
assert.equal( | ||
totalRecurring({ | ||
purchasableType: 'bundle', | ||
priceInCents: 2, | ||
quantity: 1, | ||
coupon: { duration: 'forever', percentOff: 50 } | ||
}), | ||
1 | ||
); | ||
}); | ||
}); | ||
describe('totalRecurringMulticurrency', function() { | ||
it('returns null for non-bundles', function() { | ||
describe('totalRecurringMulticurrency', function () { | ||
it('returns null for non-bundles', function () { | ||
assert.equal(totalRecurringMulticurrency({ purchasableType: 'course' }), null); | ||
}); | ||
it('returns priceInCents for bundles without coupons', function() { | ||
assert.equal(totalRecurringMulticurrency({ purchasableType: 'bundle', price: { unitAmount: 2 } }), 2); | ||
it('returns priceInCents for bundles without coupons', function () { | ||
assert.equal( | ||
totalRecurringMulticurrency({ purchasableType: 'bundle', price: { unitAmount: 2 } }), | ||
2 | ||
); | ||
}); | ||
it('returns totalDueNow for bundles with forever coupons', function() { | ||
assert.equal(totalRecurringMulticurrency({ purchasableType: 'bundle', price: { unitAmount: 2 }, quantity: 1, coupon: {duration: 'forever', percentOff: 50} }), 1); | ||
it('returns totalDueNow for bundles with forever coupons', function () { | ||
assert.equal( | ||
totalRecurringMulticurrency({ | ||
purchasableType: 'bundle', | ||
price: { unitAmount: 2 }, | ||
quantity: 1, | ||
coupon: { duration: 'forever', percentOff: 50 } | ||
}), | ||
1 | ||
); | ||
}); | ||
}); | ||
describe('totalLineOne', function() { | ||
describe('with a bundle', function() { | ||
it('returns the price with the interval if there is no coupon', function() { | ||
assert.equal(totalLineOne({ | ||
quantity: 1, | ||
purchasableType: 'bundle', | ||
interval: 'month', | ||
priceInCents: 200 | ||
}), '$2.00 / month'); | ||
describe('totalLineOne', function () { | ||
describe('with a bundle', function () { | ||
it('returns the price with the interval if there is no coupon', function () { | ||
assert.equal( | ||
totalLineOne({ | ||
quantity: 1, | ||
purchasableType: 'bundle', | ||
interval: 'month', | ||
priceInCents: 200 | ||
}), | ||
'$2.00 / month' | ||
); | ||
}); | ||
it('returns an alternate currency symbol', function() { | ||
assert.equal(totalLineOne({ | ||
quantity: 1, | ||
purchasableType: 'bundle', | ||
interval: 'month', | ||
priceInCents: 200 | ||
}, '£'), '£2.00 / month'); | ||
it('returns an alternate currency symbol', function () { | ||
assert.equal( | ||
totalLineOne( | ||
{ | ||
quantity: 1, | ||
purchasableType: 'bundle', | ||
interval: 'month', | ||
priceInCents: 200 | ||
}, | ||
'£' | ||
), | ||
'£2.00 / month' | ||
); | ||
}); | ||
it('returns the price without the interval if it is a gift', function() { | ||
assert.equal(totalLineOne({ | ||
quantity: 1, | ||
gift: true, | ||
purchasableType: 'bundle', | ||
interval: 'month', | ||
priceInCents: 200 | ||
}), '$2.00'); | ||
it('returns the price without the interval if it is a gift', function () { | ||
assert.equal( | ||
totalLineOne({ | ||
quantity: 1, | ||
gift: true, | ||
purchasableType: 'bundle', | ||
interval: 'month', | ||
priceInCents: 200 | ||
}), | ||
'$2.00' | ||
); | ||
}); | ||
it('returns the price with the interval if there is no coupon', function() { | ||
assert.equal(totalLineOne({ | ||
quantity: 1, | ||
purchasableType: 'bundle', | ||
interval: 'month', | ||
priceInCents: 200 | ||
}), '$2.00 / month'); | ||
it('returns the price with the interval if there is no coupon', function () { | ||
assert.equal( | ||
totalLineOne({ | ||
quantity: 1, | ||
purchasableType: 'bundle', | ||
interval: 'month', | ||
priceInCents: 200 | ||
}), | ||
'$2.00 / month' | ||
); | ||
}); | ||
it('returns the price with the extended interval if there is a repeating coupon', function() { | ||
assert.equal(totalLineOne({ | ||
quantity: 1, | ||
purchasableType: 'bundle', | ||
interval: 'month', | ||
coupon: {amountOffInCents: 199, duration: 'repeating', durationInMonths: 4}, | ||
priceInCents: 200 | ||
}), '$0.01 / month for the first 4 months'); | ||
it('returns the price with the extended interval if there is a repeating coupon', function () { | ||
assert.equal( | ||
totalLineOne({ | ||
quantity: 1, | ||
purchasableType: 'bundle', | ||
interval: 'month', | ||
coupon: { amountOffInCents: 199, duration: 'repeating', durationInMonths: 4 }, | ||
priceInCents: 200 | ||
}), | ||
'$0.01 / month for the first 4 months' | ||
); | ||
}); | ||
it('returns the price with the extended interval if there is a once coupon', function() { | ||
assert.equal(totalLineOne({ | ||
quantity: 1, | ||
purchasableType: 'bundle', | ||
interval: 'month', | ||
coupon: {amountOffInCents: 199, duration: 'once'}, | ||
priceInCents: 200 | ||
}), '$0.01 for the first month'); | ||
it('returns the price with the extended interval if there is a once coupon', function () { | ||
assert.equal( | ||
totalLineOne({ | ||
quantity: 1, | ||
purchasableType: 'bundle', | ||
interval: 'month', | ||
coupon: { amountOffInCents: 199, duration: 'once' }, | ||
priceInCents: 200 | ||
}), | ||
'$0.01 for the first month' | ||
); | ||
}); | ||
it('returns the price with interval if the coupon is forever', function() { | ||
assert.equal(totalLineOne({ | ||
quantity: 1, | ||
purchasableType: 'bundle', | ||
interval: 'month', | ||
coupon: {amountOffInCents: 1, duration: 'forever'}, | ||
priceInCents: 2 | ||
}), '$0.01 / month'); | ||
it('returns the price with interval if the coupon is forever', function () { | ||
assert.equal( | ||
totalLineOne({ | ||
quantity: 1, | ||
purchasableType: 'bundle', | ||
interval: 'month', | ||
coupon: { amountOffInCents: 1, duration: 'forever' }, | ||
priceInCents: 2 | ||
}), | ||
'$0.01 / month' | ||
); | ||
}); | ||
it('returns Free if the due now is free and the coupon is forever', function() { | ||
assert.equal(totalLineOne({ | ||
quantity: 1, | ||
purchasableType: 'bundle', | ||
coupon: {amountOffInCents: 2, duration: 'forever'}, | ||
priceInCents: 2 | ||
}), 'Free'); | ||
it('returns Free if the due now is free and the coupon is forever', function () { | ||
assert.equal( | ||
totalLineOne({ | ||
quantity: 1, | ||
purchasableType: 'bundle', | ||
coupon: { amountOffInCents: 2, duration: 'forever' }, | ||
priceInCents: 2 | ||
}), | ||
'Free' | ||
); | ||
}); | ||
}); | ||
describe('with a non-bundle', function() { | ||
it('returns the formatted price', function() { | ||
assert.equal(totalLineOne({ | ||
quantity: 1, | ||
priceInCents: 200000 | ||
}), '$2,000.00'); | ||
describe('with a non-bundle', function () { | ||
it('returns the formatted price', function () { | ||
assert.equal( | ||
totalLineOne({ | ||
quantity: 1, | ||
priceInCents: 200000 | ||
}), | ||
'$2,000.00' | ||
); | ||
}); | ||
it('returns Free if the due now is free', function() { | ||
assert.equal(totalLineOne({ | ||
quantity: 1, | ||
coupon: {amountOffInCents: 2}, | ||
priceInCents: 2 | ||
}), 'Free'); | ||
it('returns Free if the due now is free', function () { | ||
assert.equal( | ||
totalLineOne({ | ||
quantity: 1, | ||
coupon: { amountOffInCents: 2 }, | ||
priceInCents: 2 | ||
}), | ||
'Free' | ||
); | ||
}); | ||
@@ -231,51 +357,63 @@ }); | ||
describe('totalLineOneMulticurrency', function() { | ||
describe('with a non-bundle', function() { | ||
it('returns the formatted price in usd', function() { | ||
assert.equal(totalLineOneMulticurrency( | ||
{ | ||
quantity: 1, | ||
price: { | ||
unitAmount: 200000 | ||
} | ||
}, | ||
'usd' | ||
), '$2,000.00'); | ||
describe('totalLineOneMulticurrency', function () { | ||
describe('with a non-bundle', function () { | ||
it('returns the formatted price in usd', function () { | ||
assert.equal( | ||
totalLineOneMulticurrency( | ||
{ | ||
quantity: 1, | ||
price: { | ||
unitAmount: 200000 | ||
} | ||
}, | ||
'usd' | ||
), | ||
'$2,000.00' | ||
); | ||
}); | ||
it('returns the formatted price in yen', function() { | ||
assert.equal(totalLineOneMulticurrency( | ||
{ | ||
quantity: 1, | ||
price: { | ||
unitAmount: 4505 | ||
} | ||
}, | ||
'jpy' | ||
), '¥4,505'); | ||
it('returns the formatted price in yen', function () { | ||
assert.equal( | ||
totalLineOneMulticurrency( | ||
{ | ||
quantity: 1, | ||
price: { | ||
unitAmount: 4505 | ||
} | ||
}, | ||
'jpy' | ||
), | ||
'¥4,505' | ||
); | ||
}); | ||
it('returns the formatted price in euro', function() { | ||
assert.equal(totalLineOneMulticurrency( | ||
{ | ||
quantity: 1, | ||
price: { | ||
unitAmount: 350002 | ||
} | ||
}, | ||
'eur' | ||
), '€3,500.02'); | ||
it('returns the formatted price in euro', function () { | ||
assert.equal( | ||
totalLineOneMulticurrency( | ||
{ | ||
quantity: 1, | ||
price: { | ||
unitAmount: 350002 | ||
} | ||
}, | ||
'eur' | ||
), | ||
'€3,500.02' | ||
); | ||
}); | ||
it('returns Free if the due now is free', function() { | ||
assert.equal(totalLineOneMulticurrency( | ||
{ | ||
quantity: 1, | ||
coupon: { amountOffInCents: 2 }, | ||
price: { | ||
unitAmount: 2 | ||
} | ||
}, | ||
'usd' | ||
), 'Free'); | ||
it('returns Free if the due now is free', function () { | ||
assert.equal( | ||
totalLineOneMulticurrency( | ||
{ | ||
quantity: 1, | ||
coupon: { amountOffInCents: 2 }, | ||
price: { | ||
unitAmount: 2 | ||
} | ||
}, | ||
'usd' | ||
), | ||
'Free' | ||
); | ||
}); | ||
@@ -285,60 +423,80 @@ }); | ||
describe('totalLineTwo', function() { | ||
describe('with a bundle', function() { | ||
it('returns null if there is no coupon', function() { | ||
assert.equal(totalLineTwo({ | ||
quantity: 1, | ||
purchasableType: 'bundle', | ||
priceInCents: 2 | ||
}), null); | ||
describe('totalLineTwo', function () { | ||
describe('with a bundle', function () { | ||
it('returns null if there is no coupon', function () { | ||
assert.equal( | ||
totalLineTwo({ | ||
quantity: 1, | ||
purchasableType: 'bundle', | ||
priceInCents: 2 | ||
}), | ||
null | ||
); | ||
}); | ||
it('returns the total recurring with the interval if there is a non-forever coupon', function() { | ||
assert.equal(totalLineTwo({ | ||
quantity: 1, | ||
purchasableType: 'bundle', | ||
interval: 'month', | ||
coupon: {amountOffInCents: 4, duration: 'once'}, | ||
priceInCents: 6 | ||
}), '$0.06 / month'); | ||
it('returns the total recurring with the interval if there is a non-forever coupon', function () { | ||
assert.equal( | ||
totalLineTwo({ | ||
quantity: 1, | ||
purchasableType: 'bundle', | ||
interval: 'month', | ||
coupon: { amountOffInCents: 4, duration: 'once' }, | ||
priceInCents: 6 | ||
}), | ||
'$0.06 / month' | ||
); | ||
}); | ||
it('returns the total recurring while taking quantity into account', function() { | ||
assert.equal(totalLineTwo({ | ||
quantity: 10, | ||
purchasableType: 'bundle', | ||
interval: 'month', | ||
coupon: {amountOffInCents: 4, duration: 'once'}, | ||
priceInCents: 6 | ||
}), '$0.60 / month'); | ||
it('returns the total recurring while taking quantity into account', function () { | ||
assert.equal( | ||
totalLineTwo({ | ||
quantity: 10, | ||
purchasableType: 'bundle', | ||
interval: 'month', | ||
coupon: { amountOffInCents: 4, duration: 'once' }, | ||
priceInCents: 6 | ||
}), | ||
'$0.60 / month' | ||
); | ||
}); | ||
it('returns an alternate currency symbol', function() { | ||
assert.equal(totalLineTwo({ | ||
quantity: 1, | ||
purchasableType: 'bundle', | ||
interval: 'month', | ||
coupon: {amountOffInCents: 4, duration: 'once'}, | ||
priceInCents: 6 | ||
}, '£'), '£0.06 / month'); | ||
it('returns an alternate currency symbol', function () { | ||
assert.equal( | ||
totalLineTwo( | ||
{ | ||
quantity: 1, | ||
purchasableType: 'bundle', | ||
interval: 'month', | ||
coupon: { amountOffInCents: 4, duration: 'once' }, | ||
priceInCents: 6 | ||
}, | ||
'£' | ||
), | ||
'£0.06 / month' | ||
); | ||
}); | ||
it('returns null if there is a forever coupon', function() { | ||
assert.equal(totalLineTwo({ | ||
quantity: 1, | ||
purchasableType: 'bundle', | ||
interval: 'month', | ||
coupon: {amountOffInCents: 4, duration: 'forever'}, | ||
priceInCents: 6 | ||
}), null); | ||
it('returns null if there is a forever coupon', function () { | ||
assert.equal( | ||
totalLineTwo({ | ||
quantity: 1, | ||
purchasableType: 'bundle', | ||
interval: 'month', | ||
coupon: { amountOffInCents: 4, duration: 'forever' }, | ||
priceInCents: 6 | ||
}), | ||
null | ||
); | ||
}); | ||
}); | ||
describe('with a non-bundle', function() { | ||
it('returns null', function() { | ||
assert.equal(totalLineTwo({ | ||
quantity: 1, | ||
priceInCents: 2 | ||
}), null); | ||
describe('with a non-bundle', function () { | ||
it('returns null', function () { | ||
assert.equal( | ||
totalLineTwo({ | ||
quantity: 1, | ||
priceInCents: 2 | ||
}), | ||
null | ||
); | ||
}); | ||
@@ -348,74 +506,92 @@ }); | ||
describe('totalLineTwoMulticurrency', function() { | ||
describe('with a bundle', function() { | ||
it('returns null if there is no coupon', function() { | ||
assert.equal(totalLineTwoMulticurrency({ | ||
quantity: 1, | ||
purchasableType: 'bundle', | ||
price: { | ||
unitAmount: 2 | ||
} | ||
}), null); | ||
describe('totalLineTwoMulticurrency', function () { | ||
describe('with a bundle', function () { | ||
it('returns null if there is no coupon', function () { | ||
assert.equal( | ||
totalLineTwoMulticurrency({ | ||
quantity: 1, | ||
purchasableType: 'bundle', | ||
price: { | ||
unitAmount: 2 | ||
} | ||
}), | ||
null | ||
); | ||
}); | ||
it('returns the total recurring with the interval if there is a non-forever coupon', function() { | ||
assert.equal(totalLineTwoMulticurrency({ | ||
quantity: 1, | ||
purchasableType: 'bundle', | ||
interval: 'month', | ||
coupon: { amountOffInCents: 4, duration: 'once' }, | ||
price: { | ||
unitAmount: 6 | ||
} | ||
}), '$0.06 / month'); | ||
it('returns the total recurring with the interval if there is a non-forever coupon', function () { | ||
assert.equal( | ||
totalLineTwoMulticurrency({ | ||
quantity: 1, | ||
purchasableType: 'bundle', | ||
interval: 'month', | ||
coupon: { amountOffInCents: 4, duration: 'once' }, | ||
price: { | ||
unitAmount: 6 | ||
} | ||
}), | ||
'$0.06 / month' | ||
); | ||
}); | ||
it('returns the total recurring while taking quantity into account', function() { | ||
assert.equal(totalLineTwoMulticurrency({ | ||
quantity: 10, | ||
purchasableType: 'bundle', | ||
interval: 'month', | ||
coupon: {amountOffInCents: 4, duration: 'once'}, | ||
price: { | ||
unitAmount: 6 | ||
} | ||
}), '$0.60 / month'); | ||
it('returns the total recurring while taking quantity into account', function () { | ||
assert.equal( | ||
totalLineTwoMulticurrency({ | ||
quantity: 10, | ||
purchasableType: 'bundle', | ||
interval: 'month', | ||
coupon: { amountOffInCents: 4, duration: 'once' }, | ||
price: { | ||
unitAmount: 6 | ||
} | ||
}), | ||
'$0.60 / month' | ||
); | ||
}); | ||
it('returns an alternate currency symbol', function() { | ||
assert.equal(totalLineTwoMulticurrency( | ||
{ | ||
it('returns an alternate currency symbol', function () { | ||
assert.equal( | ||
totalLineTwoMulticurrency( | ||
{ | ||
quantity: 1, | ||
purchasableType: 'bundle', | ||
interval: 'month', | ||
coupon: { amountOffInCents: 4, duration: 'once' }, | ||
price: { | ||
unitAmount: 6 | ||
} | ||
}, | ||
'gbp' | ||
), | ||
'£0.06 / month' | ||
); | ||
}); | ||
it('returns null if there is a forever coupon', function () { | ||
assert.equal( | ||
totalLineTwoMulticurrency({ | ||
quantity: 1, | ||
purchasableType: 'bundle', | ||
interval: 'month', | ||
coupon: {amountOffInCents: 4, duration: 'once'}, | ||
coupon: { amountOffInCents: 4, duration: 'forever' }, | ||
price: { | ||
unitAmount: 6 | ||
} | ||
}, | ||
'gbp'), '£0.06 / month'); | ||
}), | ||
null | ||
); | ||
}); | ||
it('returns null if there is a forever coupon', function() { | ||
assert.equal(totalLineTwoMulticurrency({ | ||
quantity: 1, | ||
purchasableType: 'bundle', | ||
interval: 'month', | ||
coupon: {amountOffInCents: 4, duration: 'forever'}, | ||
price: { | ||
unitAmount: 6 | ||
} | ||
}), null); | ||
}); | ||
}); | ||
describe('with a non-bundle', function() { | ||
it('returns null', function() { | ||
assert.equal(totalLineTwoMulticurrency({ | ||
quantity: 1, | ||
price: { | ||
unitAmount: 2 | ||
} | ||
}), null); | ||
describe('with a non-bundle', function () { | ||
it('returns null', function () { | ||
assert.equal( | ||
totalLineTwoMulticurrency({ | ||
quantity: 1, | ||
price: { | ||
unitAmount: 2 | ||
} | ||
}), | ||
null | ||
); | ||
}); | ||
@@ -425,57 +601,69 @@ }); | ||
describe('totalDescription', function() { | ||
it('returns line one and two if they are both set', function() { | ||
assert.equal(totalDescription({ | ||
quantity: 1, | ||
purchasableType: 'bundle', | ||
interval: 'month', | ||
coupon: {amountOffInCents: 4, duration: 'once'}, | ||
priceInCents: 6 | ||
}), '$0.02 for the first month, then $0.06 / month'); | ||
describe('totalDescription', function () { | ||
it('returns line one and two if they are both set', function () { | ||
assert.equal( | ||
totalDescription({ | ||
quantity: 1, | ||
purchasableType: 'bundle', | ||
interval: 'month', | ||
coupon: { amountOffInCents: 4, duration: 'once' }, | ||
priceInCents: 6 | ||
}), | ||
'$0.02 for the first month, then $0.06 / month' | ||
); | ||
}); | ||
it('returns an alternate currency symbol', function() { | ||
assert.equal(totalDescription({ | ||
quantity: 1, | ||
purchasableType: 'bundle', | ||
interval: 'month', | ||
coupon: {amountOffInCents: 4, duration: 'once'}, | ||
priceInCents: 6 | ||
}, '£'), '£0.02 for the first month, then £0.06 / month'); | ||
it('returns an alternate currency symbol', function () { | ||
assert.equal( | ||
totalDescription( | ||
{ | ||
quantity: 1, | ||
purchasableType: 'bundle', | ||
interval: 'month', | ||
coupon: { amountOffInCents: 4, duration: 'once' }, | ||
priceInCents: 6 | ||
}, | ||
'£' | ||
), | ||
'£0.02 for the first month, then £0.06 / month' | ||
); | ||
}); | ||
it('returns just line one if line two is not set', function() { | ||
assert.equal(totalDescription({ | ||
quantity: 1, | ||
purchasableType: 'bundle', | ||
interval: 'month', | ||
coupon: {amountOffInCents: 4, duration: 'forever'}, | ||
priceInCents: 6 | ||
}), '$0.02 / month'); | ||
it('returns just line one if line two is not set', function () { | ||
assert.equal( | ||
totalDescription({ | ||
quantity: 1, | ||
purchasableType: 'bundle', | ||
interval: 'month', | ||
coupon: { amountOffInCents: 4, duration: 'forever' }, | ||
priceInCents: 6 | ||
}), | ||
'$0.02 / month' | ||
); | ||
}); | ||
}); | ||
describe('discountable', function() { | ||
it('prefers percentOff to amountOffInCents when both are given', function() { | ||
describe('discountable', function () { | ||
it('prefers percentOff to amountOffInCents when both are given', function () { | ||
assert.equal(discountable(10, 50, 7), 5); | ||
}); | ||
it('defaults to zero if not provided an amountOffInCents or percentOff', function() { | ||
it('defaults to zero if not provided an amountOffInCents or percentOff', function () { | ||
assert.equal(discountable(10), 10); | ||
}); | ||
describe('amountOffInCents', function() { | ||
it('calculates correctly', function() { | ||
describe('amountOffInCents', function () { | ||
it('calculates correctly', function () { | ||
assert.equal(discountable(10, null, 7), 3); | ||
}); | ||
it('handles floats', function() { | ||
it('handles floats', function () { | ||
assert.equal(discountable(10, null, 5.5), 4.5); | ||
}); | ||
it('will not go below zero', function() { | ||
it('will not go below zero', function () { | ||
assert.equal(discountable(10, null, 11), 0); | ||
}); | ||
it('ignores negatives', function() { | ||
it('ignores negatives', function () { | ||
assert.equal(discountable(10, null, -3), 7); | ||
@@ -485,20 +673,20 @@ }); | ||
describe('percentOff', function() { | ||
it('calculates correctly', function() { | ||
describe('percentOff', function () { | ||
it('calculates correctly', function () { | ||
assert.equal(discountable(10, 10), 9); | ||
}); | ||
it('handles floats', function() { | ||
it('handles floats', function () { | ||
assert.equal(discountable(10, 10.5), 8.95); | ||
}); | ||
it('calculates 100% off correctly', function() { | ||
it('calculates 100% off correctly', function () { | ||
assert.equal(discountable(10, 100), 0); | ||
}); | ||
it('calculates > 100% off correctly', function() { | ||
it('calculates > 100% off correctly', function () { | ||
assert.equal(discountable(10, 200), 0); | ||
}); | ||
it('ignores negatives', function() { | ||
it('ignores negatives', function () { | ||
assert.equal(discountable(10, -50), 5); | ||
@@ -505,0 +693,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24685
852