From 7ab05cf857727c521ddd59c4198a79791d575f15 Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Thu, 6 Oct 2016 13:38:09 +1100 Subject: [PATCH] Basic AngularJS controller and service for loading StandingOrders --- .../standing_orders_controller.js.coffee | 2 + .../services/standing_orders.js.coffee | 13 +++++ .../standing_orders_controller_spec.js.coffee | 51 +++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 app/assets/javascripts/admin/standing_orders/controllers/standing_orders_controller.js.coffee create mode 100644 app/assets/javascripts/admin/standing_orders/services/standing_orders.js.coffee create mode 100644 spec/javascripts/unit/admin/standing_orders/controllers/standing_orders_controller_spec.js.coffee diff --git a/app/assets/javascripts/admin/standing_orders/controllers/standing_orders_controller.js.coffee b/app/assets/javascripts/admin/standing_orders/controllers/standing_orders_controller.js.coffee new file mode 100644 index 0000000000..e83b463310 --- /dev/null +++ b/app/assets/javascripts/admin/standing_orders/controllers/standing_orders_controller.js.coffee @@ -0,0 +1,2 @@ +angular.module("admin.standingOrders").controller "StandingOrdersController", ($scope, StandingOrders) -> + $scope.standingOrders = StandingOrders.index {} # {enterprise_id: $scope.shop_id} diff --git a/app/assets/javascripts/admin/standing_orders/services/standing_orders.js.coffee b/app/assets/javascripts/admin/standing_orders/services/standing_orders.js.coffee new file mode 100644 index 0000000000..3294b951be --- /dev/null +++ b/app/assets/javascripts/admin/standing_orders/services/standing_orders.js.coffee @@ -0,0 +1,13 @@ +angular.module("admin.standingOrders").factory 'StandingOrders', ($q, StandingOrderResource) -> + new class StandingOrders + byID: {} + pristineByID: {} + + index: (params={}, callback=null) -> + StandingOrderResource.index params, (data) => + @load(data) + + load: (standingOrders) -> + for standingOrder in standingOrders + @byID[standingOrder.id] = standingOrder + @pristineByID[standingOrder.id] = angular.copy(standingOrder) diff --git a/spec/javascripts/unit/admin/standing_orders/controllers/standing_orders_controller_spec.js.coffee b/spec/javascripts/unit/admin/standing_orders/controllers/standing_orders_controller_spec.js.coffee new file mode 100644 index 0000000000..1005d86879 --- /dev/null +++ b/spec/javascripts/unit/admin/standing_orders/controllers/standing_orders_controller_spec.js.coffee @@ -0,0 +1,51 @@ +describe "StandingOrdersCtrl", -> + scope = null + http = null + shops = null + + beforeEach -> + module('admin.standingOrders') + # module ($provide) -> + # $provide.value 'columns', [] + # null + + # shops = [ + # { name: "Shop 1", id: 1 } + # { name: "Shop 2", id: 2 } + # { name: "Shop 3", id: 3 } + # ] + + inject ($controller, $rootScope, _StandingOrderResource_, $httpBackend) -> + scope = $rootScope + http = $httpBackend + $controller 'StandingOrdersController', {$scope: scope, StandingOrderResource: _StandingOrderResource_} + jasmine.addMatchers + toDeepEqual: (util, customEqualityTesters) -> + compare: (actual, expected) -> + { pass: angular.equals(actual, expected) } + + # it "has no shop pre-selected", inject (CurrentShop) -> + # expect(CurrentShop.shop).toEqual {} + + describe "initialization", -> # setting shop_id on scope + standingOrder = { id: 5, customer_id: 3, schedule_id: 1} + standingOrders = [standingOrder] + + beforeEach inject -> + scope.standing_orders_form = jasmine.createSpyObj('standing_orders_form', ['$setPristine']) + http.expectGET('/admin/standing_orders.json').respond 200, standingOrders + # scope.$apply -> + # scope.shop_id = 3 + http.flush() + + # it "sets the CurrentShop", inject (CurrentShop) -> + # expect(CurrentShop.shop).toEqual shops[2] + # + # it "sets the form state to pristine", -> + # expect(scope.standingOrders_form.$setPristine).toHaveBeenCalled() + # + # it "clears all changes", inject (pendingChanges) -> + # expect(pendingChanges.removeAll).toHaveBeenCalled() + + it "retrieves the list of standingOrders", -> + expect(scope.standingOrders).toDeepEqual standingOrders