From 4fb30f294265a7fcaf5c08258d3d18cd10013995 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 12 Sep 2014 12:50:04 +1000 Subject: [PATCH] When removing outgoing exchanges, do not removing variants from other outgoing exchanges --- .../admin/order_cycle.js.erb.coffee | 13 ++--- .../unit/order_cycle_spec.js.coffee | 48 ++++++++++++------- 2 files changed, 38 insertions(+), 23 deletions(-) diff --git a/app/assets/javascripts/admin/order_cycle.js.erb.coffee b/app/assets/javascripts/admin/order_cycle.js.erb.coffee index b8068681ca..64f2550466 100644 --- a/app/assets/javascripts/admin/order_cycle.js.erb.coffee +++ b/app/assets/javascripts/admin/order_cycle.js.erb.coffee @@ -197,12 +197,13 @@ angular.module('order_cycle', ['ngResource']) this.order_cycle.outgoing_exchanges.push({enterprise_id: new_distributor_id, incoming: false, active: true, variants: {}, enterprise_fees: []}) removeExchange: (exchange) -> - incoming_index = this.order_cycle.incoming_exchanges.indexOf exchange - this.order_cycle.incoming_exchanges.splice(incoming_index, 1) if incoming_index > -1 - outgoing_index = this.order_cycle.outgoing_exchanges.indexOf exchange - this.order_cycle.outgoing_exchanges.splice(outgoing_index, 1) if outgoing_index > -1 - - this.removeDistributionOfVariant(variant_id) for variant_id, active of exchange.variants when active + if exchange.incoming + incoming_index = this.order_cycle.incoming_exchanges.indexOf exchange + this.order_cycle.incoming_exchanges.splice(incoming_index, 1) + this.removeDistributionOfVariant(variant_id) for variant_id, active of exchange.variants when active + else + outgoing_index = this.order_cycle.outgoing_exchanges.indexOf exchange + this.order_cycle.outgoing_exchanges.splice(outgoing_index, 1) if outgoing_index > -1 addCoordinatorFee: -> this.order_cycle.coordinator_fees.push({}) diff --git a/spec/javascripts/unit/order_cycle_spec.js.coffee b/spec/javascripts/unit/order_cycle_spec.js.coffee index 0d5127314a..2a8d001804 100644 --- a/spec/javascripts/unit/order_cycle_spec.js.coffee +++ b/spec/javascripts/unit/order_cycle_spec.js.coffee @@ -516,30 +516,44 @@ describe 'OrderCycle services', -> ] describe 'removing exchanges', -> - it 'removes incoming exchanges', -> - exchange = {enterprise_id: '123', active: true, variants: {}, enterprise_fees: []} - OrderCycle.order_cycle.incoming_exchanges = [exchange] - OrderCycle.removeExchange(exchange) - expect(OrderCycle.order_cycle.incoming_exchanges).toEqual [] + exchange = null - it 'removes outgoing exchanges', -> - exchange = {enterprise_id: '123', active: true, variants: {}, enterprise_fees: []} - OrderCycle.order_cycle.outgoing_exchanges = [exchange] - OrderCycle.removeExchange(exchange) - expect(OrderCycle.order_cycle.outgoing_exchanges).toEqual [] - - it 'removes distribution of all exchange variants', -> + beforeEach -> spyOn(OrderCycle, 'removeDistributionOfVariant') exchange = enterprise_id: '123' active: true + incoming: false variants: {1: true, 2: false, 3: true} enterprise_fees: [] - OrderCycle.order_cycle.incoming_exchanges = [exchange] - OrderCycle.removeExchange(exchange) - expect(OrderCycle.removeDistributionOfVariant).toHaveBeenCalledWith('1') - expect(OrderCycle.removeDistributionOfVariant).not.toHaveBeenCalledWith('2') - expect(OrderCycle.removeDistributionOfVariant).toHaveBeenCalledWith('3') + + describe "removing incoming exchanges", -> + beforeEach -> + exchange.incoming = true + OrderCycle.order_cycle.incoming_exchanges = [exchange] + + it 'removes the exchange', -> + OrderCycle.removeExchange(exchange) + expect(OrderCycle.order_cycle.incoming_exchanges).toEqual [] + + it 'removes distribution of all exchange variants', -> + OrderCycle.removeExchange(exchange) + expect(OrderCycle.removeDistributionOfVariant).toHaveBeenCalledWith('1') + expect(OrderCycle.removeDistributionOfVariant).not.toHaveBeenCalledWith('2') + expect(OrderCycle.removeDistributionOfVariant).toHaveBeenCalledWith('3') + + describe "removing outgoing exchanges", -> + beforeEach -> + exchange.incoming = false + OrderCycle.order_cycle.outgoing_exchanges = [exchange] + + it 'removes the exchange', -> + OrderCycle.removeExchange(exchange) + expect(OrderCycle.order_cycle.outgoing_exchanges).toEqual [] + + it "does not remove distribution of any variants", -> + OrderCycle.removeExchange(exchange) + expect(OrderCycle.removeDistributionOfVariant).not.toHaveBeenCalled() it 'adds coordinator fees', -> OrderCycle.addCoordinatorFee()