Fix bug in subscriptions address controller where the country id lookup was not exact and states returned were incorrect. Add unit tests to cover different cases

This commit is contained in:
luisramos0
2018-10-16 17:30:57 +01:00
parent 590091c42a
commit feaf16d878
3 changed files with 54 additions and 7 deletions

View File

@@ -1,11 +1,21 @@
angular.module("admin.subscriptions").controller "AddressController", ($scope, $filter, StatusMessage, availableCountries) ->
$scope.countries = availableCountries
$scope.statesFor = (country_id) ->
return [] unless country_id
$filter('filter')(availableCountries, {id: country_id})[0].states
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.$watch 'subscription.bill_address.country_id', (newValue, oldValue) ->
$scope.billStates = $scope.statesFor(newValue) if newValue?
$scope.$watch 'subscription.ship_address.country_id', (newValue, oldValue) ->
$scope.shipStates = $scope.statesFor(newValue) if newValue?
$scope.registerNextCallback 'address', ->
$scope.subscription_form.$submitted = true
if $scope.subscription_address_form.$valid
@@ -18,9 +28,3 @@ angular.module("admin.subscriptions").controller "AddressController", ($scope, $
$scope.registerBackCallback 'address', ->
StatusMessage.clear()
$scope.setView('details')
$scope.$watch 'subscription.bill_address.country_id', (newValue, oldValue) ->
$scope.billStates = $scope.statesFor(newValue) if newValue?
$scope.$watch 'subscription.ship_address.country_id', (newValue, oldValue) ->
$scope.shipStates = $scope.statesFor(newValue) if newValue?