describe 'Cart service', -> Cart = null Variants = null variant = null order = null beforeEach -> module 'Darkswarm' variant = id: 1 name_to_display: 'name' product_name: 'name' order = { line_items: [ variant: variant ] } angular.module('Darkswarm').value('currentOrder', order) inject ($injector)-> Variants = $injector.get("Variants") Cart = $injector.get("Cart") it "backreferences line items", -> expect(Cart.line_items[0].variant.line_item).toBe Cart.line_items[0] it "registers variants with the Variants service", -> expect(Variants.variants[1]).toBe variant it "generates extended variant names", -> expect(Cart.line_items[0].variant.extended_name).toEqual "name" 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] 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 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 describe "generating an extended variant name", -> it "returns the product name when it is the same as the variant name", -> variant = {product_name: 'product_name', name_to_display: 'product_name'} expect(Cart.extendedVariantName(variant)).toEqual "product_name" describe "when the product name and the variant name differ", -> it "returns a combined name when there is no options text", -> variant = product_name: 'product_name' name_to_display: 'name_to_display' expect(Cart.extendedVariantName(variant)).toEqual "product_name - name_to_display" it "returns a combined name when there is some options text", -> variant = product_name: 'product_name' name_to_display: 'name_to_display' options_text: 'options_text' expect(Cart.extendedVariantName(variant)).toEqual "product_name - name_to_display (options_text)"