mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-01 02:03:22 +00:00
Add JS spec and basic initialisation code for BOM
This commit is contained in:
33
app/assets/javascripts/admin/bulk_order_management.js.coffee
Normal file
33
app/assets/javascripts/admin/bulk_order_management.js.coffee
Normal file
@@ -0,0 +1,33 @@
|
||||
ordersApp = angular.module("bulk_order_management", ["bulk_product_update"])
|
||||
|
||||
ordersApp.config [
|
||||
"$httpProvider"
|
||||
(provider) ->
|
||||
provider.defaults.headers.common["X-CSRF-Token"] = $("meta[name=csrf-token]").attr("content")
|
||||
]
|
||||
|
||||
ordersApp.controller "AdminBulkOrdersCtrl", [
|
||||
"$scope", "$http", "dataFetcher"
|
||||
($scope, $http, dataFetcher) ->
|
||||
$scope.updateStatusMessage =
|
||||
text: ""
|
||||
style: {}
|
||||
|
||||
$scope.initialise = (spree_api_key) ->
|
||||
authorise_api_reponse = ""
|
||||
dataFetcher("/api/users/authorise_api?token=" + spree_api_key).then (data) ->
|
||||
authorise_api_reponse = data
|
||||
$scope.spree_api_key_ok = data.hasOwnProperty("success") and data["success"] == "Use of API Authorised"
|
||||
if $scope.spree_api_key_ok
|
||||
$http.defaults.headers.common["X-Spree-Token"] = spree_api_key
|
||||
dataFetcher("/api/enterprises/managed?template=bulk_index&q[is_primary_producer_eq]=true").then (data) ->
|
||||
$scope.suppliers = data
|
||||
# Need to have suppliers before we get products so we can match suppliers to product.supplier
|
||||
$scope.fetchOrders()
|
||||
else if authorise_api_reponse.hasOwnProperty("error")
|
||||
$scope.api_error_msg = authorise_api_reponse("error")
|
||||
else
|
||||
api_error_msg = "You don't have an API key yet. An attempt was made to generate one, but you are currently not authorised, please contact your site administrator for access."
|
||||
|
||||
$scope.fetchOrders = ->
|
||||
]
|
||||
@@ -10,6 +10,7 @@ module.exports = function(config) {
|
||||
'app/assets/javascripts/shared/angular-*.js',
|
||||
|
||||
'app/assets/javascripts/admin/order_cycle.js.erb.coffee',
|
||||
'app/assets/javascripts/admin/bulk_order_management.js.coffee',
|
||||
'app/assets/javascripts/admin/bulk_product_update.js.coffee',
|
||||
'app/assets/javascripts/darkswarm/*.js*',
|
||||
'app/assets/javascripts/darkswarm/**/*.js*',
|
||||
|
||||
23
spec/javascripts/unit/bulk_order_management_spec.js.coffee
Normal file
23
spec/javascripts/unit/bulk_order_management_spec.js.coffee
Normal file
@@ -0,0 +1,23 @@
|
||||
describe "AdminBulkOrdersCtrl", ->
|
||||
ctrl = scope = timeout = httpBackend = null
|
||||
|
||||
beforeEach ->
|
||||
module "bulk_order_management"
|
||||
beforeEach inject(($controller, $rootScope, $httpBackend) ->
|
||||
scope = $rootScope.$new()
|
||||
ctrl = $controller
|
||||
httpBackend = $httpBackend
|
||||
|
||||
ctrl "AdminBulkOrdersCtrl", {$scope: scope, $timeout: timeout}
|
||||
)
|
||||
|
||||
describe "loading data upon initialisation", ->
|
||||
it "gets a list of suppliers and then calls fetchOrders", ->
|
||||
httpBackend.expectGET("/api/users/authorise_api?token=api_key").respond success: "Use of API Authorised"
|
||||
httpBackend.expectGET("/api/enterprises/managed?template=bulk_index&q[is_primary_producer_eq]=true").respond "list of suppliers"
|
||||
spyOn(scope, "fetchOrders").andReturn "nothing"
|
||||
scope.initialise "api_key"
|
||||
httpBackend.flush()
|
||||
expect(scope.suppliers).toEqual "list of suppliers"
|
||||
expect(scope.fetchOrders.calls.length).toEqual 1
|
||||
expect(scope.spree_api_key_ok).toEqual true
|
||||
Reference in New Issue
Block a user