diff --git a/src/app/productConfig/productConfigForm/productConfigForm.component.js b/src/app/productConfig/productConfigForm/productConfigForm.component.js index fc4aab92b..36df92db2 100644 --- a/src/app/productConfig/productConfigForm/productConfigForm.component.js +++ b/src/app/productConfig/productConfigForm/productConfigForm.component.js @@ -315,6 +315,14 @@ class ProductConfigFormController { const data = this.omitIrrelevantData(this.itemConfig) const comment = data.DONATION_SERVICES_COMMENTS const isTestingTransaction = comment ? comment.toLowerCase().includes('test') : false + const transformedAmount = this.transformAmountIfNecessary(data.AMOUNT) + if (transformedAmount === 'error') { + this.submittingGift = false + this.errorSavingGeneric = true + this.onStateChange({ state: 'errorSubmitting' }) + return + } + data.AMOUNT = transformedAmount this.brandedAnalyticsFactory.saveTestingTransaction(isTestingTransaction) this.analyticsFactory.saveTestingTransaction(this.productData, isTestingTransaction) @@ -358,6 +366,19 @@ class ProductConfigFormController { }) } + transformAmountIfNecessary (amount) { + let transformedAmount = amount + if (!angular.isNumber(amount)) { + transformedAmount = amount.replace('$', '') + transformedAmount = transformedAmount.replace(',', '') + transformedAmount = parseFloat(transformedAmount) + if (isNaN(transformedAmount)) { + return 'error' + } + } + return transformedAmount + } + displayId () { if (!this.productData) { return '' diff --git a/src/app/productConfig/productConfigForm/productConfigForm.component.spec.js b/src/app/productConfig/productConfigForm/productConfigForm.component.spec.js index b920d2555..2819578a0 100644 --- a/src/app/productConfig/productConfigForm/productConfigForm.component.spec.js +++ b/src/app/productConfig/productConfigForm/productConfigForm.component.spec.js @@ -715,6 +715,30 @@ describe('product config form component', function () { $ctrl.saveGiftToCart() } }) + + it('should transform the amount', () => { + $ctrl.itemConfig.AMOUNT = '$85' + $ctrl.itemConfigForm.$dirty = true + $ctrl.saveGiftToCart() + + expect($ctrl.submittingGift).toEqual(false) + expect($ctrl.cartService[operation]).toHaveBeenCalledWith(...operationArgs) + expect($ctrl.$scope.$emit).toHaveBeenCalledWith(cartEvent) + expect($ctrl.onStateChange).toHaveBeenCalledWith({ state: 'submitted' }) + expect($ctrl.errorAlreadyInCart).toEqual(false) + expect($ctrl.errorSavingGeneric).toEqual(false) + }) + + it('should fail if the amount is invalid', () => { + $ctrl.itemConfig.AMOUNT = 'test' + $ctrl.itemConfigForm.$dirty = true + $ctrl.saveGiftToCart() + + expect($ctrl.submittingGift).toEqual(false) + expect($ctrl.onStateChange).toHaveBeenCalledWith({ state: 'errorSubmitting' }) + expect($ctrl.errorAlreadyInCart).toEqual(false) + expect($ctrl.errorSavingGeneric).toEqual(true) + }) } }) @@ -762,4 +786,31 @@ describe('product config form component', function () { expect($ctrl.$window.location).toEqual('https://example.com') }) }) + + describe('transformAmountIfNecessary', () => { + it('should not change the int amount', () => { + const amount = '5' + expect($ctrl.transformAmountIfNecessary(amount)).toEqual(5) + }) + + it('should not change the float amount', () => { + const amount = '5.12' + expect($ctrl.transformAmountIfNecessary(amount)).toEqual(5.12) + }) + + it('should remove the $', () => { + const amount = '$50' + expect($ctrl.transformAmountIfNecessary(amount)).toEqual(50) + }) + + it('should remove the comma', () => { + const amount = '1,000' + expect($ctrl.transformAmountIfNecessary(amount)).toEqual(1000) + }) + + it('should return an error', () => { + const amount = 'test' + expect($ctrl.transformAmountIfNecessary(amount)).toEqual('error') + }) + }) })