Skip to content

Commit

Permalink
Merge branch 'better-error-for-bad-amount' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
wrandall22 committed Oct 25, 2023
2 parents c95ab94 + 3ba6453 commit 7d503d5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,12 @@ class ProductConfigFormController {
if (includes(error.data, 'already in the cart')) {
this.errorAlreadyInCart = true
this.onStateChange({ state: 'errorAlreadyInCart' })
} else if (error.data && error.data.messages && error.data.messages[0] && error.data.messages[0].id === 'field.invalid.decimal.format') {
this.amountFormatError = error.data.messages[0]['debug-message']
this.onStateChange({ state: 'errorSubmitting' })
} else if (includes(error.data, 'decimal number')) {
this.amountFormatError = error.data
this.onStateChange({ state: 'errorSubmitting' })
} else {
this.errorSavingGeneric = true
this.$log.error('Error adding or updating item in cart', error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,41 @@ describe('product config form component', function () {
expect($ctrl.errorSavingGeneric).toEqual(false)
})

it('should handle an error when saving a bad decimal amount - old error style', () => {
const error = { data: 'Amount must be a valid decimal number without dollar signs or commas.' }
$ctrl.cartService[operation].mockReturnValue(Observable.throw(error))
$ctrl.itemConfigForm.$dirty = true
$ctrl.saveGiftToCart()

expect($ctrl.submittingGift).toEqual(false)
expect($ctrl.cartService[operation]).toHaveBeenCalledWith(...operationArgs)
expect($ctrl.onStateChange).toHaveBeenCalledWith({ state: 'errorSubmitting' })
expect($ctrl.errorAlreadyInCart).toEqual(false)
expect($ctrl.errorSavingGeneric).toEqual(false)
expect($ctrl.amountFormatError).toEqual = error.data
})

it('should handle an error when saving a bad decimal amount - new error style', () => {
const error = {
data: {
messages: [{
id: 'field.invalid.decimal.format',
'debug-message': 'Amount must be a valid decimal number without dollar signs or commas.'
}]
}
}
$ctrl.cartService[operation].mockReturnValue(Observable.throw(error))
$ctrl.itemConfigForm.$dirty = true
$ctrl.saveGiftToCart()

expect($ctrl.submittingGift).toEqual(false)
expect($ctrl.cartService[operation]).toHaveBeenCalledWith(...operationArgs)
expect($ctrl.onStateChange).toHaveBeenCalledWith({ state: 'errorSubmitting' })
expect($ctrl.errorAlreadyInCart).toEqual(false)
expect($ctrl.errorSavingGeneric).toEqual(false)
expect($ctrl.amountFormatError).toEqual = error.data.messages[0]['debug-message']
})

it('should clear the cover fee decision when editing the amount of an item in the cart', () => {
if (isEdit) {
jest.spyOn($ctrl.orderService, 'clearCoverFees')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ <h4 class="panel-title border-bottom-small" translate>
<p translate>{{'ADDING_CART_ERROR'}}</p>
</div>

<div ng-if="$ctrl.amountFormatError" role="alert" class="alert alert-danger">
<p translate>{{$ctrl.amountFormatError}}</p>
</div>

<div ng-if="$ctrl.errorAlreadyInCart" class="alert alert-warning" role="alert">
<p translate>{{'GIFT_IN_CART_ERROR'}}</p>
</div>
Expand Down

0 comments on commit 7d503d5

Please sign in to comment.