mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-01 02:03:22 +00:00
Extract country states logic out of subscriptions address controller into new service CountryStates
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
angular.module("ofn.admin").factory "CountryStates", ($filter) ->
|
||||
new class CountryStates
|
||||
|
||||
statesFor: (countries, country_id) ->
|
||||
return [] unless country_id
|
||||
country = $filter('filter')(countries, {id: country_id}, true)[0]
|
||||
return [] unless country
|
||||
country.states
|
||||
@@ -1,20 +1,14 @@
|
||||
angular.module("admin.subscriptions").controller "AddressController", ($scope, $filter, StatusMessage, availableCountries) ->
|
||||
angular.module("admin.subscriptions").controller "AddressController", ($scope, StatusMessage, availableCountries, CountryStates) ->
|
||||
$scope.countries = availableCountries
|
||||
|
||||
$scope.statesFor = (country_id) ->
|
||||
return [] unless country_id
|
||||
country = $filter('filter')(availableCountries, {id: country_id}, true)[0]
|
||||
return [] unless country
|
||||
country.states
|
||||
|
||||
$scope.billStates = $scope.statesFor($scope.subscription.bill_address.country_id)
|
||||
$scope.shipStates = $scope.statesFor($scope.subscription.ship_address.country_id)
|
||||
$scope.billStates = CountryStates.statesFor(availableCountries, $scope.subscription.bill_address.country_id)
|
||||
$scope.shipStates = CountryStates.statesFor(availableCountries, $scope.subscription.ship_address.country_id)
|
||||
|
||||
$scope.$watch 'subscription.bill_address.country_id', (newValue, oldValue) ->
|
||||
$scope.billStates = $scope.statesFor(newValue) if newValue?
|
||||
$scope.billStates = CountryStates.statesFor(availableCountries, newValue) if newValue?
|
||||
|
||||
$scope.$watch 'subscription.ship_address.country_id', (newValue, oldValue) ->
|
||||
$scope.shipStates = $scope.statesFor(newValue) if newValue?
|
||||
$scope.shipStates = CountryStates.statesFor(availableCountries, newValue) if newValue?
|
||||
|
||||
$scope.registerNextCallback 'address', ->
|
||||
$scope.subscription_form.$submitted = true
|
||||
|
||||
@@ -1 +1 @@
|
||||
angular.module("admin.subscriptions", ['ngResource','admin.indexUtils','admin.dropdown'])
|
||||
angular.module("admin.subscriptions", ['ngResource','admin.indexUtils','admin.dropdown', 'ofn.admin'])
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
describe "AddressController", ->
|
||||
scope = null
|
||||
subscription = { id: 1 }
|
||||
describe "CountryStates service", ->
|
||||
countryStates = null
|
||||
|
||||
states_in_spain = [{id: 55, name: "CAT", abbr: "CAT"}]
|
||||
states_in_portugal = [{id: 55, name: "ACT", abbr: "ACT"}, {id: 5, name: "BFT", abbr: "BFT"}]
|
||||
@@ -11,33 +10,25 @@ describe "AddressController", ->
|
||||
]
|
||||
|
||||
beforeEach ->
|
||||
module('admin.subscriptions')
|
||||
|
||||
inject ($controller, $rootScope) ->
|
||||
scope = $rootScope
|
||||
|
||||
scope.registerNextCallback = () ->
|
||||
scope.registerBackCallback = () ->
|
||||
scope.subscription = subscription
|
||||
subscription.bill_address = {country_id: 1}
|
||||
subscription.ship_address = {country_id: 2}
|
||||
$controller 'AddressController', {$scope: scope, availableCountries: availableCountries}
|
||||
module('ofn.admin')
|
||||
inject (CountryStates) ->
|
||||
countryStates = CountryStates
|
||||
|
||||
describe "statesFor", ->
|
||||
it "returns empty array for nil country id", ->
|
||||
expect(scope.statesFor(null)).toEqual []
|
||||
expect(countryStates.statesFor(availableCountries, null)).toEqual []
|
||||
|
||||
it "returns empty array for country id not in availableCountries", ->
|
||||
expect(scope.statesFor(10)).toEqual []
|
||||
expect(countryStates.statesFor(availableCountries, 10)).toEqual []
|
||||
|
||||
it "returns empty array for country id in availableCountries but without states", ->
|
||||
expect(scope.statesFor(9)).toEqual []
|
||||
expect(countryStates.statesFor(availableCountries, 9)).toEqual []
|
||||
|
||||
it "returns states for country id in availableCountries with states", ->
|
||||
expect(scope.statesFor(119)).toEqual states_in_spain
|
||||
expect(countryStates.statesFor(availableCountries, 119)).toEqual states_in_spain
|
||||
|
||||
it "returns empty array for country id (11) in availableCountries but only as part of other country id (119)", ->
|
||||
expect(scope.statesFor(11)).toEqual []
|
||||
expect(countryStates.statesFor(availableCountries, 11)).toEqual []
|
||||
|
||||
it "returns states for country id (19) in availableCountries with states even if other country ids contain the requested id (119)", ->
|
||||
expect(scope.statesFor(19)).toEqual states_in_portugal
|
||||
expect(countryStates.statesFor(availableCountries, 19)).toEqual states_in_portugal
|
||||
Reference in New Issue
Block a user