Getting the test framework set up for Angular, setting up a products fetch stub

This commit is contained in:
Will Marshall
2013-12-06 15:24:42 +11:00
parent b806947e7b
commit 63dfa0b696
14 changed files with 123 additions and 43 deletions

View File

@@ -8,7 +8,7 @@
#= require ./shop
#= require_tree .
$ ->
$(document).foundation()
#$ ->
#$(document).foundation()

View File

@@ -0,0 +1,5 @@
#Shop.controller "OrderCycleCtrl", ($scope, OrderCycle) ->
#$scope.setOrderCycle = ()->
#console.log "foo"
##OrderCycle.

View File

@@ -0,0 +1,6 @@
Shop.factory 'OrderCycle', ($resource) ->
class OrderCycle
@set_order_cycle: (id)->
new $resource("/shop/order_cycle").$save () ->
console.log "pushed"
# Push id to endpoint

View File

@@ -1,8 +1,5 @@
Shop.factory 'Product', ($resource) ->
#return $resource("/shop/products")
class Product
@all: ->
$resource("/shop/products").query()
#new Product
response = $resource("/shop/products").query()

View File

@@ -4,7 +4,15 @@ class ShopController < BaseController
before_filter :set_distributor
before_filter :set_order_cycles
def index
def show
end
def products
if current_order_cycle
render json: Spree::Product.all.to_json
else
render json: "", status: 404
end
end
private

View File

@@ -1,10 +1,14 @@
- 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.
%ordercycle
- 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.
= render partial: "shop/next_order_cycle"
= render partial: "shop/last_order_cycle"
= render partial: "shop/next_order_cycle"
= render partial: "shop/last_order_cycle"
- 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!"
- 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!"

View File

@@ -1,13 +0,0 @@
= @distributor.name
= render partial: "shop/order_cycles"
%description
= @distributor.long_description.andand.html_safe
%products{"ng-app" => "Shop", "ng-controller" => "ProductsCtrl"}
{{ products | json }}
= render partial: "enterprises/contact_us"
= render partial: "enterprises/about_us"

View File

@@ -0,0 +1,14 @@
%shop{"ng-app" => "Shop"}
= @distributor.name
= render partial: "shop/order_cycles"
%description
= @distributor.long_description.andand.html_safe
%products{"ng-controller" => "ProductsCtrl"}
{{ products }}
= render partial: "enterprises/contact_us"
= render partial: "enterprises/about_us"

View File

@@ -11,6 +11,8 @@ module.exports = function(config) {
'app/assets/javascripts/admin/order_cycle.js.erb.coffee',
'app/assets/javascripts/admin/bulk_product_update.js',
'app/assets/javascripts/darkswarm/*.js*',
'app/assets/javascripts/darkswarm/**/*.js*',
'spec/javascripts/unit/**/*.js*'
],

View File

@@ -2,7 +2,9 @@ Openfoodnetwork::Application.routes.draw do
root :to => 'home#temp_landing_page'
resources :shop
resource :shop, controller: :shop do
get :products
end
resources :enterprises do
collection do

View File

@@ -1,22 +1,44 @@
require 'spec_helper'
describe ShopController do
it "should select an order cycle when only one order cycle is open" do
d = create(:distributor_enterprise)
oc1 = create(:order_cycle, distributors: [d])
let(:d) { create(:distributor_enterprise) }
before do
controller.stub(:current_distributor).and_return d
spree_get :index
controller.current_order_cycle.should == oc1
end
it "should not set an order cycle when multiple order cycles are open" do
d = create(:distributor_enterprise)
oc1 = create(:order_cycle, distributors: [d])
oc2 = create(:order_cycle, distributors: [d])
controller.stub(:current_distributor).and_return d
spree_get :index
controller.current_order_cycle.should == nil
describe "Selecting order cycles" do
it "should select an order cycle when only one order cycle is open" do
oc1 = create(:order_cycle, distributors: [d])
spree_get :show
controller.current_order_cycle.should == oc1
end
it "should not set an order cycle when multiple order cycles are open" do
oc1 = create(:order_cycle, distributors: [d])
oc2 = create(:order_cycle, distributors: [d])
spree_get :show
controller.current_order_cycle.should == nil
end
end
it "should create/load an order when loading show"
describe "returning products" do
let(:product) { create(:product) }
let(:order_cycle) { create(:order_cycle, distributors: [d]) }
before do
Spree::Product.stub(:all).and_return([product])
end
it "returns products via json" do
controller.stub(:current_order_cycle).and_return order_cycle
xhr :get, :products
response.should be_success
response.body.should_not be_empty
end
it "does not return products if no order_cycle is selected" do
xhr :get, :products
response.status.should == 404
response.body.should be_empty
end
end
end

View File

@@ -754,4 +754,4 @@ describe 'OrderCycle services', ->
expect(data.incoming_exchanges[0].enterprise_fees).toBeUndefined()
expect(data.outgoing_exchanges[0].enterprise_fees).toBeUndefined()
expect(data.incoming_exchanges[0].enterprise_fee_ids).toEqual [1, 2]
expect(data.outgoing_exchanges[0].enterprise_fee_ids).toEqual [3, 4]
expect(data.outgoing_exchanges[0].enterprise_fee_ids).toEqual [3, 4]

View File

@@ -0,0 +1,15 @@
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

@@ -0,0 +1,18 @@
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'