Skip to content

Commit

Permalink
Remove extraneous $ and , characters in the amount
Browse files Browse the repository at this point in the history
  • Loading branch information
wrandall22 committed Oct 25, 2023
1 parent a2a4fe4 commit 5c27beb
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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 ''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
})

Expand Down Expand Up @@ -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')
})
})
})

0 comments on commit 5c27beb

Please sign in to comment.