Building out and testing our Product and OrderCycle Angular stuff

This commit is contained in:
Will Marshall
2013-12-11 12:42:33 +11:00
parent 4db8f755bf
commit 44fe304efb
11 changed files with 80 additions and 60 deletions

View File

@@ -1,5 +1,4 @@
#Shop.controller "OrderCycleCtrl", ($scope, OrderCycle) ->
#$scope.setOrderCycle = ()->
#console.log "foo"
##OrderCycle.
Shop.controller "OrderCycleCtrl", ($scope, $rootScope, OrderCycle) ->
$scope.order_cycle = OrderCycle.order_cycle
$scope.changeOrderCycle = ->
OrderCycle.set_order_cycle()

View File

@@ -1,4 +1,12 @@
angular.module("Shop").controller "ProductsCtrl", ($scope, Product) ->
angular.module("Shop").controller "ProductsCtrl", ($scope, $rootScope, Product) ->
$scope.products = Product.all()
#$scope.order_cycle = OrderCycle.order_cycle
#$scope.updateProducts = ->
#$scope.products = Product.all()
#$scope.$watch "order_cycle.order_cycle_id", $scope.updateProducts
#$scope.updateProducts()

View File

@@ -1,6 +1,10 @@
Shop.factory 'OrderCycle', ($resource) ->
class OrderCycle
@set_order_cycle: (id)->
new $resource("/shop/order_cycle").$save () ->
console.log "pushed"
# Push id to endpoint
Shop.factory 'OrderCycle', ($resource, Product) ->
new class OrderCycle
@order_cycle = {
order_cycle_id: null
}
set_order_cycle: (id = null)->
new $resource("/shop/order_cycle").save {order_cycle_id: id}, ->
Product.update()

View File

@@ -1,5 +1,7 @@
Shop.factory 'Product', ($resource) ->
class Product
@all: ->
response = $resource("/shop/products").query()
new class Product
@products: null
update: ->
@products = $resource("/shop/products").query()
all: ->
@products || @update()

View File

@@ -1,4 +1,4 @@
%ordercycle
%ordercycle{"ng-controller" => "OrderCycleCtrl"}
- if @order_cycles.empty?
Orders are currently closed for this hub
%p Please contact your hub directly to see if they accept late orders, or wait until the next cycle opens.
@@ -8,7 +8,7 @@
- else
Ready for:
= select_tag :order_cycle_id,
options_for_select(order_cycles_name_and_pickup_times(@order_cycles),
current_order_cycle.andand.id), :prompt => "Select an Order Cycle!"
%select{"ng-model" => "order_cycle.order_cycle_id",
"ng-change" => "changeOrderCycle()",
"ng-options" => "c for c in #{@order_cycles.map {|oc| oc.id}.to_json}"}

View File

@@ -7,8 +7,9 @@
= @distributor.long_description.andand.html_safe
%products{"ng-controller" => "ProductsCtrl"}
{{ products }}
%pre {{ products | json }}
= render partial: "enterprises/contact_us"
= render partial: "enterprises/about_us"

View File

@@ -1,30 +1,46 @@
describe 'Shop controllers', ->
describe 'All controllers', ->
describe 'ProductsCtrl', ->
ctrl = null
scope = null
event = null
rootScope = null
Product = null
beforeEach ->
module('Shop')
scope = {}
Product =
all: ->
'testy mctest'
inject ($controller) ->
inject ($controller, $rootScope) ->
rootScope = $rootScope
scope = $rootScope.$new()
ctrl = $controller 'ProductsCtrl', {$scope: scope, Product : Product}
it 'Fetches products from Product', ->
expect(scope.products).toEqual 'testy mctest'
#it "updates products when the changeOrderCycle event is seen", ->
#spyOn(scope, "updateProducts")
#rootScope.$emit "changeOrderCycle"
#expect(scope.updateProducts).toHaveBeenCalled()
describe 'OrderCycleCtrl', ->
ctrl = null
scope = null
event = null
rootScope = null
product_ctrl = null
OrderCycle = null
beforeEach ->
module 'Shop'
scope = {}
inject ($controller) ->
ctrl = $controller 'OrderCycleCtrl'
inject ($controller, $rootScope) ->
rootScope = $rootScope
scope = $rootScope.$new()
ctrl = $controller 'OrderCycleCtrl', {$scope: scope}
#it "triggers an event when the order cycle changes", ->
#spyOn(rootScope, "$emit")
#scope.changeOrderCycle()
#expect(scope.$emit).toHaveBeenCalledWith "changeOrderCycle"

View File

@@ -0,0 +1,23 @@
describe 'OrderCycle service', ->
$httpBackend = null
OrderCycle = null
mockProduct = {
update: ->
}
beforeEach ->
module 'Shop', ($provide)->
$provide.value "Product", mockProduct
null # IMPORTANT
# You must return null because module() is a bit dumb
inject (_OrderCycle_, _$httpBackend_)->
$httpBackend = _$httpBackend_
OrderCycle = _OrderCycle_
it "posts the order_cycle ID and tells product to update", ->
$httpBackend.expectPOST("/shop/order_cycle", {"order_cycle_id" : 10}).respond(200)
spyOn(mockProduct, "update")
OrderCycle.set_order_cycle(10)
$httpBackend.flush()
expect(mockProduct.update).toHaveBeenCalled()

View File

@@ -8,7 +8,7 @@ describe 'Product service', ->
Product = $injector.get("Product")
$httpBackend = _$httpBackend_
it "Fetches products from the backend", ->
it "Fetches products from the backend on init", ->
$httpBackend.expectGET("/shop/products").respond([{test : "cats"}])
products = Product.all()
$httpBackend.flush()

View File

@@ -1,15 +0,0 @@
describe 'Shop services', ->
$httpBackend = null
Product = null
beforeEach ->
module 'Shop'
inject ($injector, _$httpBackend_)->
Product = $injector.get("Product")
$httpBackend = _$httpBackend_
it "Fetches products from the backend", ->
$httpBackend.expectGET("/shop/products").respond([{test : "cats"}])
products = Product.all()
$httpBackend.flush()
expect(products[0].test).toEqual "cats"

View File

@@ -1,18 +0,0 @@
describe 'Shop controllers', ->
describe 'ProductsCtrl', ->
ctrl = null
scope = null
event = null
Product = null
beforeEach ->
module('Shop')
scope = {}
Product =
all: ->
'testy mctest'
inject ($controller) ->
ctrl = $controller 'ProductsCtrl', {$scope: scope, Product : Product}
it 'Fetches products from Product', ->
expect(scope.products).toEqual 'testy mctest'