From dbf15c03d32e724a6e24b4def6b35c2ca16e32d9 Mon Sep 17 00:00:00 2001 From: Will Marshall Date: Fri, 25 Apr 2014 12:13:50 +1000 Subject: [PATCH] Refactoring how we change window.location --- app/assets/javascripts/darkswarm/services/order.js.coffee | 7 ++----- .../unit/darkswarm/services/order_spec.js.coffee | 4 +++- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/app/assets/javascripts/darkswarm/services/order.js.coffee b/app/assets/javascripts/darkswarm/services/order.js.coffee index c3bebdf143..3c86afac9f 100644 --- a/app/assets/javascripts/darkswarm/services/order.js.coffee +++ b/app/assets/javascripts/darkswarm/services/order.js.coffee @@ -1,4 +1,4 @@ -Darkswarm.factory 'Order', ($resource, Product, order, $http, CheckoutFormState, flash)-> +Darkswarm.factory 'Order', ($resource, Product, order, $http, CheckoutFormState, flash, Navigation)-> new class Order errors: {} @@ -7,12 +7,9 @@ Darkswarm.factory 'Order', ($resource, Product, order, $http, CheckoutFormState, # Default to first shipping method if none selected @order.shipping_method_id ||= parseInt(Object.keys(@order.shipping_methods)[0]) - navigate: (path)-> - window.location.pathname = path - submit: -> $http.put('/shop/checkout', {order: @preprocess()}).success (data, status)=> - @navigate(data.path) + Navigation.go data.path .error (response, status)=> @errors = response.errors flash.error = response.flash?.error diff --git a/spec/javascripts/unit/darkswarm/services/order_spec.js.coffee b/spec/javascripts/unit/darkswarm/services/order_spec.js.coffee index f78cec582b..a6e3ab9642 100644 --- a/spec/javascripts/unit/darkswarm/services/order_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/services/order_spec.js.coffee @@ -3,6 +3,7 @@ describe 'Order service', -> orderData = null $httpBackend = null CheckoutFormState = null + Navigation = null flash = null beforeEach -> @@ -28,9 +29,10 @@ describe 'Order service', -> inject ($injector, _$httpBackend_)-> $httpBackend = _$httpBackend_ Order = $injector.get("Order") + Navigation = $injector.get("Navigation") flash = $injector.get("flash") CheckoutFormState = $injector.get("CheckoutFormState") - spyOn(Order, "navigate") # Stubbing out writes to window.location + spyOn(Navigation, "go") # Stubbing out writes to window.location it "defaults the shipping method to the first", -> expect(Order.order.shipping_method_id).toEqual 7