Merge branch 'api_fixes' into bom

This commit is contained in:
Rob H
2014-04-23 12:03:01 +10:00
62 changed files with 775 additions and 2282 deletions

View File

@@ -0,0 +1,6 @@
//= require angular
//= require angular-resource
//= require angular-animate
//= require angular-mocks
//= require angular-cookies
//= require angular-flash.min.js

View File

@@ -23,20 +23,19 @@ describe "AdminOrderMgmtCtrl", ->
httpBackend.expectGET("/api/order_cycles/accessible").respond returnedOrderCycles
spyOn(scope, "initialiseVariables").andCallThrough()
spyOn(scope, "fetchOrders").andReturn "nothing"
spyOn(returnedSuppliers, "unshift")
spyOn(returnedDistributors, "unshift")
spyOn(returnedOrderCycles, "unshift")
#spyOn(returnedSuppliers, "unshift")
#spyOn(returnedDistributors, "unshift")
#spyOn(returnedOrderCycles, "unshift")
scope.initialise "api_key"
httpBackend.flush()
expect(scope.suppliers).toEqual ["list of suppliers"]
expect(scope.distributors).toEqual ["list of distributors"]
expect(scope.orderCycles).toEqual [ "oc1", "oc2", "oc3" ]
expect(scope.initialiseVariables.calls.length).toEqual 1
expect(scope.fetchOrders.calls.length).toEqual 1
expect(returnedSuppliers.unshift.calls.length).toEqual 1
expect(returnedDistributors.unshift.calls.length).toEqual 1
expect(returnedOrderCycles.unshift.calls.length).toEqual 1
expect(scope.spree_api_key_ok).toEqual true
expect(scope.suppliers).toEqual [{ id : '', name : 'All' }, 'list of suppliers']
expect(scope.distributors).toEqual [ { id : '', name : 'All' }, 'list of distributors' ]
expect(scope.orderCycles).toEqual [ { id : '', name : 'All' }, 'oc1', 'oc2', 'oc3' ]
expect(scope.initialiseVariables.calls.length).toBe 1
expect(scope.fetchOrders.calls.length).toBe 1
expect(scope.spree_api_key_ok).toBe true
describe "fetching orders", ->
beforeEach ->
@@ -625,4 +624,4 @@ describe "Auxiliary functions", ->
expect(formatDate(date)).toEqual "2010-06-15"
it "returns a time formatted as hh-MM:ss", ->
expect(formatTime(date)).toEqual "05:10:30"
expect(formatTime(date)).toEqual "05:10:30"

View File

@@ -0,0 +1,35 @@
describe "DetailsCtrl", ->
ctrl = null
scope = null
order = null
beforeEach ->
module("Darkswarm")
inject ($controller, $rootScope) ->
scope = $rootScope.$new()
ctrl = $controller 'DetailsCtrl', {$scope: scope}
it "finds a field by path", ->
scope.details =
path: "test"
expect(scope.field('path')).toEqual "test"
it "tests validity", ->
scope.details =
path:
$dirty: true
$invalid: true
expect(scope.fieldValid('path')).toEqual false
it "returns errors by path", ->
scope.Order =
errors: ->
scope.details =
path:
$error:
email: true
required: true
expect(scope.fieldErrors('path')).toEqual ["must be email address", "can't be blank"].join ", "

View File

@@ -1,34 +1,27 @@
describe "CheckoutCtrl", ->
ctrl = null
scope = null
order = null
Order = null
beforeEach ->
module("Darkswarm")
order =
id: 3102
shipping_method_id: "7"
ship_address_same_as_billing: true
payment_method_id: null
shipping_methods:
7:
require_ship_address: true
price: 0.0
25:
require_ship_address: false
price: 13
inject ($controller) ->
scope = {}
ctrl = $controller 'CheckoutCtrl', {$scope: scope, order: order}
it 'Gets the ship address automatically', ->
expect(scope.require_ship_address).toEqual true
it 'Gets the current shipping price', ->
expect(scope.shippingPrice()).toEqual 0.0
scope.order.shipping_method_id = 25
expect(scope.shippingPrice()).toEqual 13
Order = {
submit: ->
navigate: ->
order:
id: 1
}
inject ($controller, $rootScope) ->
scope = $rootScope.$new()
ctrl = $controller 'CheckoutCtrl', {$scope: scope, Order: Order}
it "defaults the user accordion to visible", ->
expect(scope.accordion.user).toEqual true
it "delegates to the service on submit", ->
event = {
preventDefault: ->
}
spyOn(Order, "submit")
scope.purchase(event)
expect(Order.submit).toHaveBeenCalled()

View File

@@ -39,6 +39,6 @@ describe 'OrderCycle service', ->
it "tells us when no order cycle is selected", ->
OrderCycle.order_cycle = null
expect(OrderCycle.selected()).toEqual false
OrderCycle.order_cycle = {test: "blah"}
OrderCycle.order_cycle = {orders_close_at: "10 days ago"}
expect(OrderCycle.selected()).toEqual true

View File

@@ -0,0 +1,86 @@
describe 'Order service', ->
Order = null
orderData = null
$httpBackend = null
CheckoutFormState = null
flash = null
beforeEach ->
orderData = {
id: 3102
payment_method_id: null
bill_address: {test: "foo"}
ship_address: {test: "bar"}
shipping_methods:
7:
require_ship_address: true
price: 0.0
25:
require_ship_address: false
price: 13
payment_methods:
99:
test: "foo"
}
angular.module('Darkswarm').value('order', orderData)
module 'Darkswarm'
inject ($injector, _$httpBackend_)->
$httpBackend = _$httpBackend_
Order = $injector.get("Order")
flash = $injector.get("flash")
CheckoutFormState = $injector.get("CheckoutFormState")
spyOn(Order, "navigate") # Stubbing out writes to window.location
it "defaults the shipping method to the first", ->
expect(Order.order.shipping_method_id).toEqual 7
expect(Order.shippingMethod()).toEqual { require_ship_address : true, price : 0 }
# This is now handled via localStorage defaults
xit "defaults to 'same as billing' for address", ->
expect(Order.order.ship_address_same_as_billing).toEqual true
it 'Tracks whether a ship address is required', ->
expect(Order.requireShipAddress()).toEqual true
Order.order.shipping_method_id = 25
expect(Order.requireShipAddress()).toEqual false
it 'Gets the current shipping price', ->
expect(Order.shippingPrice()).toEqual 0.0
Order.order.shipping_method_id = 25
expect(Order.shippingPrice()).toEqual 13
it 'Gets the current payment method', ->
expect(Order.paymentMethod()).toEqual null
Order.order.payment_method_id = 99
expect(Order.paymentMethod()).toEqual {test: "foo"}
it "Posts the Order to the server", ->
$httpBackend.expectPUT("/shop/checkout", {order: Order.preprocess()}).respond 200, {path: "test"}
Order.submit()
$httpBackend.flush()
it "sends flash messages to the flash service", ->
$httpBackend.expectPUT("/shop/checkout").respond 400, {flash: {error: "frogs"}}
Order.submit()
$httpBackend.flush()
expect(flash.error).toEqual "frogs"
it "puts errors into the scope", ->
$httpBackend.expectPUT("/shop/checkout").respond 400, {errors: {error: "frogs"}}
Order.submit()
$httpBackend.flush()
expect(Order.errors).toEqual {error: "frogs"}
it "Munges the order attributes to add _attributes as Rails needs", ->
expect(Order.preprocess().bill_address_attributes).not.toBe(undefined)
expect(Order.preprocess().bill_address).toBe(undefined)
expect(Order.preprocess().ship_address_attributes).not.toBe(undefined)
expect(Order.preprocess().ship_address).toBe(undefined)
it "Munges the order attributes to clone ship address from bill address", ->
CheckoutFormState.ship_address_same_as_billing = false
expect(Order.preprocess().ship_address_attributes).toEqual(orderData.ship_address)
CheckoutFormState.ship_address_same_as_billing = true
expect(Order.preprocess().ship_address_attributes).toEqual(orderData.bill_address)

View File

@@ -401,10 +401,12 @@ describe 'OrderCycle services', ->
it 'loads enterprise fees', ->
enterprise_fees = EnterpriseFee.index()
$httpBackend.flush()
expect(enterprise_fees).toEqual [
expected_fees = [
new EnterpriseFee.EnterpriseFee({id: 1, name: "Yayfee", enterprise_id: 1})
new EnterpriseFee.EnterpriseFee({id: 2, name: "FeeTwo", enterprise_id: 2})
]
for fee, i in enterprise_fees
expect(fee.id).toEqual(expected_fees[i].id)
it 'reports its loadedness', ->
expect(EnterpriseFee.loaded).toBe(false)
@@ -828,4 +830,4 @@ describe 'OrderCycle services', ->
expect(order_cycle.outgoing_exchanges[0].enterprise_fees).toEqual [{id: 3}, {id: 4}]
expect(order_cycle.incoming_exchanges[0].enterprise_fee_ids).toBeUndefined()
expect(order_cycle.outgoing_exchanges[0].enterprise_fee_ids).toBeUndefined()