diff --git a/app/assets/javascripts/darkswarm/services/cart.js.coffee b/app/assets/javascripts/darkswarm/services/cart.js.coffee index 74f978bf3a..c519d22fdd 100644 --- a/app/assets/javascripts/darkswarm/services/cart.js.coffee +++ b/app/assets/javascripts/darkswarm/services/cart.js.coffee @@ -43,6 +43,11 @@ Darkswarm.factory 'Cart', (CurrentOrder, Variants, $timeout, $http)-> @line_items.filter (li)-> li.quantity > 0 + total_item_count: => + @line_items_present().reduce (sum,li) -> + sum = sum + li.quantity + , 0 + empty: => @line_items_present().length == 0 diff --git a/app/views/shared/menu/_cart.html.haml b/app/views/shared/menu/_cart.html.haml index d9c990db5f..646bd33425 100644 --- a/app/views/shared/menu/_cart.html.haml +++ b/app/views/shared/menu/_cart.html.haml @@ -3,7 +3,7 @@ %span.nav-branded %i.ofn-i_027-shopping-cart %span - {{ Cart.line_items_present().length }} + {{ Cart.total_item_count() }} items .joyride-tip-guide{"ng-class" => "{ in: open }", "ng-show" => "open"} diff --git a/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee b/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee index 4079d70bcb..4d13ccc8b5 100644 --- a/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee @@ -1,15 +1,17 @@ describe 'Cart service', -> Cart = null Variants = null - variant = {id: 1} - order = { - line_items: [ - variant: variant - ] - } + variant = null + order = null beforeEach -> module 'Darkswarm' + variant = {id: 1} + order = { + line_items: [ + variant: variant + ] + } angular.module('Darkswarm').value('currentOrder', order) inject ($injector)-> Variants = $injector.get("Variants") @@ -23,10 +25,15 @@ describe 'Cart service', -> it "creates and backreferences new line items if necessary", -> Cart.register_variant(v2 = {id: 2}) - expect(Cart.line_items[1].variant).toBe v2 - expect(Cart.line_items[1].variant.line_item).toBe Cart.line_items[1] + expect(Cart.line_items[1].variant).toBe v2 + expect(Cart.line_items[1].variant.line_item).toBe Cart.line_items[1] it "returns a list of items actually in the cart", -> expect(Cart.line_items_present()).toEqual [] order.line_items[0].quantity = 1 - expect(Cart.line_items_present().length).toEqual 1 + expect(Cart.line_items_present().length).toEqual + + it "sums the quantity of each line item for cart total", -> + expect(Cart.line_items_present()).toEqual [] + order.line_items[0].quantity = 2 + expect(Cart.total_item_count()).toEqual 2