Skip to content

Commit

Permalink
Merge pull request #1068 from CruGlobal/fix-amount-handling-again
Browse files Browse the repository at this point in the history
Fix amount handling again
  • Loading branch information
wrandall22 authored Oct 27, 2023
2 parents 224ccc5 + 7c55782 commit 0881f8b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,23 +197,23 @@ class ProductConfigFormController {
}

waitForFormInitialization () {
const unregister = this.$scope.$watch('$ctrl.itemConfigForm.AMOUNT', () => {
if (this.itemConfigForm && this.itemConfigForm.AMOUNT) {
const unregister = this.$scope.$watch('$ctrl.itemConfigForm.amount', () => {
if (this.itemConfigForm && this.itemConfigForm.amount) {
unregister()
this.addCustomValidators()
}
})
}

addCustomValidators () {
this.itemConfigForm.AMOUNT.$parsers.push(value => value.replace('$', '').replace(',', '')) // Ignore a dollar sign and comma if included by the user
this.itemConfigForm.AMOUNT.$validators.minimum = value => {
this.itemConfigForm.amount.$parsers.push(value => value.replace('$', '').replace(',', '')) // Ignore a dollar sign and comma if included by the user
this.itemConfigForm.amount.$validators.minimum = value => {
return !this.customInputActive || value * 1.0 >= 1
}
this.itemConfigForm.AMOUNT.$validators.maximum = value => {
this.itemConfigForm.amount.$validators.maximum = value => {
return !this.customInputActive || value * 1.0 < 10000000
}
this.itemConfigForm.AMOUNT.$validators.pattern = value => {
this.itemConfigForm.amount.$validators.pattern = value => {
const regex = /^([0-9]*)(\.[0-9]{1,2})?$/
return !this.customInputActive || regex.test(value)
}
Expand Down Expand Up @@ -315,7 +315,6 @@ class ProductConfigFormController {
const data = this.omitIrrelevantData(this.itemConfig)
const comment = data.DONATION_SERVICES_COMMENTS
const isTestingTransaction = comment ? comment.toLowerCase().includes('test') : false
data.AMOUNT = this.transformAmountIfNecessary(data.AMOUNT)
this.brandedAnalyticsFactory.saveTestingTransaction(isTestingTransaction)
this.analyticsFactory.saveTestingTransaction(this.productData, isTestingTransaction)

Expand Down Expand Up @@ -359,13 +358,6 @@ class ProductConfigFormController {
})
}

transformAmountIfNecessary (amount) {
if (!angular.isNumber(amount)) {
return amount.replace('$', '')
}
return amount
}

displayId () {
if (!this.productData) {
return ''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ describe('product config form component', function () {
$ctrl.itemConfigForm = {
$valid: true,
$dirty: false,
AMOUNT: {}
amount: {}
}
$ctrl.$scope.$digest()

Expand All @@ -332,7 +332,7 @@ describe('product config form component', function () {

describe('addCustomValidators()', () => {
beforeEach(() => {
$ctrl.itemConfigForm.AMOUNT = {
$ctrl.itemConfigForm.amount = {
$validators: {},
$parsers: []
}
Expand All @@ -342,28 +342,28 @@ describe('product config form component', function () {
$ctrl.customInputActive = true
$ctrl.addCustomValidators()

expect($ctrl.itemConfigForm.AMOUNT.$parsers[0]('$10')).toBe('10')
expect($ctrl.itemConfigForm.AMOUNT.$parsers[0]('$10,000')).toBe('10000')
expect($ctrl.itemConfigForm.amount.$parsers[0]('$10')).toBe('10')
expect($ctrl.itemConfigForm.amount.$parsers[0]('$10,000')).toBe('10000')

expect($ctrl.itemConfigForm.AMOUNT.$validators.minimum('1')).toBe(true)
expect($ctrl.itemConfigForm.AMOUNT.$validators.minimum('0.9')).toBe(false)
expect($ctrl.itemConfigForm.amount.$validators.minimum('1')).toBe(true)
expect($ctrl.itemConfigForm.amount.$validators.minimum('0.9')).toBe(false)

expect($ctrl.itemConfigForm.AMOUNT.$validators.maximum('9999999.99')).toBe(true)
expect($ctrl.itemConfigForm.AMOUNT.$validators.maximum('10000000')).toBe(false)
expect($ctrl.itemConfigForm.amount.$validators.maximum('9999999.99')).toBe(true)
expect($ctrl.itemConfigForm.amount.$validators.maximum('10000000')).toBe(false)

expect($ctrl.itemConfigForm.AMOUNT.$validators.pattern('4.4')).toBe(true)
expect($ctrl.itemConfigForm.AMOUNT.$validators.pattern('4.')).toBe(false)
expect($ctrl.itemConfigForm.AMOUNT.$validators.pattern('4.235')).toBe(false)
expect($ctrl.itemConfigForm.amount.$validators.pattern('4.4')).toBe(true)
expect($ctrl.itemConfigForm.amount.$validators.pattern('4.')).toBe(false)
expect($ctrl.itemConfigForm.amount.$validators.pattern('4.235')).toBe(false)
})

it('should pass validation in any \'bad\' case', () => {
$ctrl.customInputActive = false
$ctrl.addCustomValidators()

expect($ctrl.itemConfigForm.AMOUNT.$validators.minimum('0.3')).toBe(true)
expect($ctrl.itemConfigForm.AMOUNT.$validators.minimum('dlksfjs')).toBe(true)
expect($ctrl.itemConfigForm.AMOUNT.$validators.maximum('4542452454524.99')).toBe(true)
expect($ctrl.itemConfigForm.AMOUNT.$validators.pattern('1.214')).toBe(true)
expect($ctrl.itemConfigForm.amount.$validators.minimum('0.3')).toBe(true)
expect($ctrl.itemConfigForm.amount.$validators.minimum('dlksfjs')).toBe(true)
expect($ctrl.itemConfigForm.amount.$validators.maximum('4542452454524.99')).toBe(true)
expect($ctrl.itemConfigForm.amount.$validators.pattern('1.214')).toBe(true)
})
})

Expand Down Expand Up @@ -715,22 +715,6 @@ describe('product config form component', function () {
$ctrl.saveGiftToCart()
}
})

it('should transform the amount', () => {
$ctrl.itemConfig.AMOUNT = '$85'
$ctrl.itemConfigForm.$dirty = true
const overrideArgs = isEdit
? ['uri', 'items/crugive/<some id>', { AMOUNT: '85' }]
: ['items/crugive/<some id>', { AMOUNT: '85' }, undefined]
$ctrl.saveGiftToCart()

expect($ctrl.submittingGift).toEqual(false)
expect($ctrl.cartService[operation]).toHaveBeenCalledWith(...overrideArgs)
expect($ctrl.$scope.$emit).toHaveBeenCalledWith(cartEvent)
expect($ctrl.onStateChange).toHaveBeenCalledWith({ state: 'submitted' })
expect($ctrl.errorAlreadyInCart).toEqual(false)
expect($ctrl.errorSavingGeneric).toEqual(false)
})
}
})

Expand Down Expand Up @@ -778,31 +762,4 @@ 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(amount)
})

it('should not change the float amount', () => {
const amount = '5.12'
expect($ctrl.transformAmountIfNecessary(amount)).toEqual(amount)
})

it('should remove the $', () => {
const amount = '$50'
expect($ctrl.transformAmountIfNecessary(amount)).toEqual('50')
})

it('should not strip other characters', () => {
const amount = '23,00'
expect($ctrl.transformAmountIfNecessary(amount)).toEqual(amount)
})

it('should pass through the original amount', () => {
const amount = 'test'
expect($ctrl.transformAmountIfNecessary(amount)).toEqual('test')
})
})
})

0 comments on commit 0881f8b

Please sign in to comment.