Skip to content

Commit

Permalink
Merge pull request #487 from recurly/fix-addon-removal
Browse files Browse the repository at this point in the history
Fixes SubscriptionPricing addon removal
  • Loading branch information
bhelx authored Oct 26, 2018
2 parents c4ab27e + aadb9c9 commit 3d45b06
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 19 deletions.
4 changes: 2 additions & 2 deletions lib/recurly/pricing/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class Pricing extends Emitter {
* example
*
* .remove({ plan: 'plan_code' });
* .remove({ addon: 'addon_code' });
* .remove({ addons: 'addon_code' });
* .remove({ coupon: 'coupon_code' });
* .remove({ address: true }); // to remove without specifying a code
*
Expand All @@ -113,7 +113,7 @@ export class Pricing extends Emitter {
let id = opts[prop];
if (!~Object.keys(this.items).indexOf(prop)) return this.error(errors('invalid-item'), reject);
if (Array.isArray(this.items[prop])) {
let pos = this.items[prop].indexOf(findByCode(this.items[prop], { code: id }));
let pos = this.items[prop].indexOf(findByCode(this.items[prop], id));
if (~pos) {
item = this.items[prop].splice(pos);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/recurly/pricing/subscription/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export default class SubscriptionPricing extends Pricing {
let addon = findByCode(this.items.addons, addonCode);

if (quantity === 0) {
this.remove({ addon: addonCode });
this.remove({ addons: addonCode });
}

if (addon) {
Expand Down
91 changes: 75 additions & 16 deletions test/pricing/subscription/subscription.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,23 +180,82 @@ describe('Recurly.Pricing.Subscription', function () {
});
});

describe('with usage addons', function () {
it('should not apply the usage cost to the price', function (done) {
this.pricing
.plan('basic', { quantity: 1})
.addon('snarf')
.addon('with_usage')
.address({
country: 'US',
postal_code: 'NoTax'
})
.done(function (price) {
assert.equal(price.now.addons,"1.00")
assert.equal(price.next.addons,"1.00")
assert.equal(price.now.total,"22.99")
assert.equal(price.next.total,"20.99")
done();
describe('with addons', () => {
describe('with fixed addons', () => {
it('updates the price accordingly', function (done) {
this.pricing
.plan('basic', { quantity: 1 })
.addon('snarf')
.done(price => {
assert.equal(price.now.addons, '1.00');
assert.equal(price.next.addons, '1.00');
assert.equal(price.now.total, '22.99');
assert.equal(price.next.total, '20.99');
done();
});
});

describe('when an addon quantity is updated', () => {
it('updates the quantity and price accordingly', function (done) {
this.pricing
.plan('basic', { quantity: 1 })
.addon('snarf', { quantity: 1 })
.then(() => {
assert.equal(this.pricing.items.addons.length, 1);
assert.equal(this.pricing.items.addons[0].code, 'snarf');
assert.equal(this.pricing.items.addons[0].quantity, 1);
})
.addon('snarf', { quantity: 2 })
.done(price => {
assert.equal(this.pricing.items.addons.length, 1);
assert.equal(this.pricing.items.addons[0].code, 'snarf');
assert.equal(this.pricing.items.addons[0].quantity, 2);
assert.equal(price.now.addons, '2.00');
assert.equal(price.next.addons, '2.00');
assert.equal(price.now.total, '23.99');
assert.equal(price.next.total, '21.99');
done();
});
});
});

describe('when an addon quantity is updated to zero', () => {
it('removes the addon from the pricing instance', function (done) {
this.pricing
.plan('basic', { quantity: 1 })
.addon('snarf', { quantity: 2 })
.then(() => {
assert.equal(this.pricing.items.addons.length, 1);
assert.equal(this.pricing.items.addons[0].code, 'snarf');
assert.equal(this.pricing.items.addons[0].quantity, 2);
})
.addon('snarf', { quantity: 0 })
.done(price => {
assert.equal(this.pricing.items.addons.length, 0);
assert.equal(price.now.addons, '0.00');
assert.equal(price.next.addons, '0.00');
assert.equal(price.now.total, '21.99');
assert.equal(price.next.total, '19.99');
done();
});
});
});
});

describe('with usage addons', () => {
it('should not apply the usage cost to the price', function (done) {
this.pricing
.plan('basic', { quantity: 1 })
.addon('snarf')
.addon('with_usage')
.done(function (price) {
assert.equal(price.now.addons, '1.00');
assert.equal(price.next.addons, '1.00');
assert.equal(price.now.total, '22.99');
assert.equal(price.next.total, '20.99');
done();
});
});
});
});

Expand Down

0 comments on commit 3d45b06

Please sign in to comment.