From f997026796896aea652967cc249e86e56ccecd41 Mon Sep 17 00:00:00 2001 From: Will Marshall Date: Wed, 21 May 2014 16:54:48 +1000 Subject: [PATCH] Reworking the JS specs --- .../product_node_controller.js.coffee | 1 + .../controllers/products_controller.js.coffee | 2 - .../darkswarm/services/product.js.coffee | 3 + .../hub_node_controller_spec.js.coffee | 11 ---- .../product_node_controller_spec.js.coffee | 24 +++++++ .../products_controller_spec.js.coffee | 64 ++++++------------- .../darkswarm/services/order_spec.js.coffee | 2 + .../{ => services}/product_spec.js.coffee | 2 +- 8 files changed, 50 insertions(+), 59 deletions(-) create mode 100644 spec/javascripts/unit/darkswarm/controllers/products/product_node_controller_spec.js.coffee rename spec/javascripts/unit/darkswarm/{ => services}/product_spec.js.coffee (86%) diff --git a/app/assets/javascripts/darkswarm/controllers/products/product_node_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/products/product_node_controller.js.coffee index 3e24f006ae..959423e3ca 100644 --- a/app/assets/javascripts/darkswarm/controllers/products/product_node_controller.js.coffee +++ b/app/assets/javascripts/darkswarm/controllers/products/product_node_controller.js.coffee @@ -1,4 +1,5 @@ Darkswarm.controller "ProductNodeCtrl", ($scope) -> + $scope.price = -> if $scope.product.variants.length > 0 prices = (v.price for v in $scope.product.variants) diff --git a/app/assets/javascripts/darkswarm/controllers/products_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/products_controller.js.coffee index 09df18ca38..323df2fc4e 100644 --- a/app/assets/javascripts/darkswarm/controllers/products_controller.js.coffee +++ b/app/assets/javascripts/darkswarm/controllers/products_controller.js.coffee @@ -12,5 +12,3 @@ Darkswarm.controller "ProductsCtrl", ($scope, $rootScope, Product, OrderCycle) - code = e.keyCode || e.which if code == 13 e.preventDefault() - - $scope.productPrice = (product) -> diff --git a/app/assets/javascripts/darkswarm/services/product.js.coffee b/app/assets/javascripts/darkswarm/services/product.js.coffee index ca31d5e9c0..e58b83bacc 100644 --- a/app/assets/javascripts/darkswarm/services/product.js.coffee +++ b/app/assets/javascripts/darkswarm/services/product.js.coffee @@ -2,6 +2,9 @@ Darkswarm.factory 'Product', ($resource) -> new class Product constructor: -> @update() + + # TODO: don't need to scope this into object + # Already on object as far as controller scope is concerned data: products: null loading: true diff --git a/spec/javascripts/unit/darkswarm/controllers/hub_node_controller_spec.js.coffee b/spec/javascripts/unit/darkswarm/controllers/hub_node_controller_spec.js.coffee index a2d14f5811..cb77cb1703 100644 --- a/spec/javascripts/unit/darkswarm/controllers/hub_node_controller_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/controllers/hub_node_controller_spec.js.coffee @@ -19,14 +19,3 @@ describe "HubNodeCtrl", -> expect(scope.current()).toEqual false scope.hub = {id: 99} expect(scope.current()).toEqual true - - it "knows whether selecting this hub will empty the cart", -> - CurrentHub.id = undefined - expect(scope.emptiesCart()).toEqual false - - CurrentHub.id = 99 - scope.hub.id = 99 - expect(scope.emptiesCart()).toEqual false - - scope.hub.id = 1 - expect(scope.emptiesCart()).toEqual true diff --git a/spec/javascripts/unit/darkswarm/controllers/products/product_node_controller_spec.js.coffee b/spec/javascripts/unit/darkswarm/controllers/products/product_node_controller_spec.js.coffee new file mode 100644 index 0000000000..4b9e404b9e --- /dev/null +++ b/spec/javascripts/unit/darkswarm/controllers/products/product_node_controller_spec.js.coffee @@ -0,0 +1,24 @@ +describe "ProductNodeCtrl", -> + ctrl = null + scope = null + product = + id: 99 + price: 10.00 + variants: [] + + beforeEach -> + module('Darkswarm') + inject ($controller) -> + scope = + product: product + ctrl = $controller 'ProductNodeCtrl', {$scope: scope} + + describe "determining the price to display for a product", -> + it "displays the product price when the product does not have variants", -> + expect(scope.price()).toEqual 10.00 + + it "displays the minimum variant price when the product has variants", -> + scope.product = + price: 11 + variants: [{price: 22}, {price: 33}] + expect(scope.price()).toEqual 22 diff --git a/spec/javascripts/unit/darkswarm/controllers/products_controller_spec.js.coffee b/spec/javascripts/unit/darkswarm/controllers/products_controller_spec.js.coffee index 269c8385b8..685d495039 100644 --- a/spec/javascripts/unit/darkswarm/controllers/products_controller_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/controllers/products_controller_spec.js.coffee @@ -1,47 +1,21 @@ -describe 'All controllers', -> - describe 'ProductsCtrl', -> - ctrl = null - scope = null - event = null - Product = null +describe 'ProductsCtrl', -> + ctrl = null + scope = null + event = null + Product = null - beforeEach -> - module('Darkswarm') - Product = - all: -> - update: -> - data: "testy mctest" - OrderCycle = - order_cycle: {} - - inject ($controller) -> - scope = {} - ctrl = $controller 'ProductsCtrl', {$scope: scope, Product: Product, OrderCycle: OrderCycle} - - it 'fetches products from Product', -> - expect(scope.data).toEqual 'testy mctest' - - describe "determining the price to display for a product", -> - it "displays the product price when the product does not have variants", -> - product = {variants: [], price: 12.34} - expect(scope.productPrice(product)).toEqual 12.34 - - it "displays the minimum variant price when the product has variants", -> - product = - price: 11 - variants: [{price: 22}, {price: 33}] - expect(scope.productPrice(product)).toEqual 22 - - describe 'OrderCycleCtrl', -> - ctrl = null - scope = null - event = null - product_ctrl = null - OrderCycle = null - - beforeEach -> - module 'Darkswarm' + beforeEach -> + module('Darkswarm') + Product = + all: -> + update: -> + data: "testy mctest" + OrderCycle = + order_cycle: {} + + inject ($controller) -> scope = {} - inject ($controller) -> - scope = {} - ctrl = $controller 'OrderCycleCtrl', {$scope: scope} + ctrl = $controller 'ProductsCtrl', {$scope: scope, Product: Product, OrderCycle: OrderCycle} + + it 'fetches products from Product', -> + expect(scope.data).toEqual 'testy mctest' diff --git a/spec/javascripts/unit/darkswarm/services/order_spec.js.coffee b/spec/javascripts/unit/darkswarm/services/order_spec.js.coffee index 0462e92952..fedcf23629 100644 --- a/spec/javascripts/unit/darkswarm/services/order_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/services/order_spec.js.coffee @@ -26,8 +26,10 @@ describe 'Order service', -> } angular.module('Darkswarm').value('order', orderData) module 'Darkswarm' + inject ($injector, _$httpBackend_)-> $httpBackend = _$httpBackend_ + $httpBackend.expectGET("/shop/products").respond 200, [] Order = $injector.get("Order") Navigation = $injector.get("Navigation") flash = $injector.get("flash") diff --git a/spec/javascripts/unit/darkswarm/product_spec.js.coffee b/spec/javascripts/unit/darkswarm/services/product_spec.js.coffee similarity index 86% rename from spec/javascripts/unit/darkswarm/product_spec.js.coffee rename to spec/javascripts/unit/darkswarm/services/product_spec.js.coffee index 7108d819af..260074a981 100644 --- a/spec/javascripts/unit/darkswarm/product_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/services/product_spec.js.coffee @@ -10,5 +10,5 @@ describe 'Product service', -> it "Fetches products from the backend on init", -> $httpBackend.expectGET("/shop/products").respond([{test : "cats"}]) - products = Product.all() $httpBackend.flush() + expect(Product.data.products[0].test).toEqual "cats"