Skip to content

Commit

Permalink
Add tests (which found a bug to fix)
Browse files Browse the repository at this point in the history
  • Loading branch information
wrandall22 committed Oct 25, 2023
1 parent e17d9c2 commit 3ba6453
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ class ProductConfigFormController {
if (includes(error.data, 'already in the cart')) {
this.errorAlreadyInCart = true
this.onStateChange({ state: 'errorAlreadyInCart' })
} else if (error.data.messages[0] && error.data.messages[0].id === 'field.invalid.decimal.format') {
} 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')) {
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

0 comments on commit 3ba6453

Please sign in to comment.