Skip to content

Commit

Permalink
Fix console error, refactor tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
wjames111 committed Nov 25, 2024
1 parent 08b686e commit 8128308
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ class ExistingPaymentMethodsController {
$onInit () {
this.enableContinue({ $event: false })
this.loadPaymentMethods()
this.addCvvValidators()
console.log(this.sessionStorage)
this.waitForFormInitialization()
}

$onChanges (changes) {
Expand All @@ -57,6 +56,15 @@ class ExistingPaymentMethodsController {
}
}

waitForFormInitialization () {
const unregister = this.$scope.$watch('$ctrl.creditCardPaymentForm.securityCode', () => {
if (this.creditCardPaymentForm && this.creditCardPaymentForm.securityCode) {
unregister()
this.addCvvValidators()
}
})
}

addCvvValidators () {
this.$scope.$watch('$ctrl.creditCardPaymentForm.securityCode.$viewValue', (number) => {
this.creditCardPaymentForm.securityCode.$validators.minLength = cruPayments.creditCard.cvv.validate.minLength
Expand Down
2 changes: 1 addition & 1 deletion src/app/checkout/step-2/step-2.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class Step2Controller {
}
}

getContinueDisabled () {
isContinueDisabled () {
if (this.selectedPaymentMethod?.['card-type'] && typeof this.isCvvValid !== 'undefined' && !this.isCvvValid) {
return true
}
Expand Down
123 changes: 66 additions & 57 deletions src/app/checkout/step-2/step-2.component.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,14 @@ describe('checkout', () => {
})
})

describe('getContinueDisabled', () => {
describe('isContinueDisabled', () => {
it('should return true when there are existing payment methods but none are valid', () => {
self.controller.handleExistingPaymentLoading(true, true)
self.controller.handlePaymentChange(undefined)

expect(self.controller.existingPaymentMethods).toBe(true)
expect(self.controller.selectedPaymentMethod).toBeUndefined()
expect(self.controller.getContinueDisabled()).toBe(true)
expect(self.controller.isContinueDisabled()).toBe(true)
})

it('should return false when there are existing payment methods and at least one is valid', () => {
Expand All @@ -265,27 +265,27 @@ describe('checkout', () => {

expect(self.controller.existingPaymentMethods).toBe(true)
expect(self.controller.selectedPaymentMethod).not.toBeUndefined()
expect(self.controller.getContinueDisabled()).toBe(false)
expect(self.controller.isContinueDisabled()).toBe(false)
})

it('should return false when there are not existing payment methods', () => {
self.controller.handleExistingPaymentLoading(true, false)

expect(self.controller.existingPaymentMethods).toBe(false)
expect(self.controller.selectedPaymentMethod).toBeUndefined()
expect(self.controller.getContinueDisabled()).toBe(false)
expect(self.controller.isContinueDisabled()).toBe(false)
})

it('should return true while the payment methods are loading', () => {
self.controller.$onInit()

expect(self.controller.loadingPaymentMethods).toBe(true)
expect(self.controller.getContinueDisabled()).toBe(true)
expect(self.controller.isContinueDisabled()).toBe(true)

self.controller.handleExistingPaymentLoading(true, false)

expect(self.controller.loadingPaymentMethods).toBe(false)
expect(self.controller.getContinueDisabled()).toBe(false)
expect(self.controller.isContinueDisabled()).toBe(false)
})

it('should return true while the payment form is encrypting or loading', () => {
Expand All @@ -295,80 +295,89 @@ describe('checkout', () => {
self.controller.onPaymentFormStateChange({ state: 'encrypting' })

expect(self.controller.paymentFormState).toBe('encrypting')
expect(self.controller.getContinueDisabled()).toBe(true)
expect(self.controller.isContinueDisabled()).toBe(true)

self.controller.onPaymentFormStateChange({ state: 'loading', payload: {}, update: false })

expect(self.controller.paymentFormState).toBe('loading')
expect(self.controller.getContinueDisabled()).toBe(true)
expect(self.controller.isContinueDisabled()).toBe(true)

deferred.resolve()
self.$flushPendingTasks()

expect(self.controller.paymentFormState).toBe('success')
expect(self.controller.getContinueDisabled()).toBe(false)
expect(self.controller.isContinueDisabled()).toBe(false)
})

it('should return true when cvv is invalid and credit card is used', () => {
self.controller.handleExistingPaymentLoading(true, true)
self.controller.isCvvValid = false
self.controller.handlePaymentChange({'card-type': 'visa'})

expect(self.controller.getContinueDisabled()).toBe(true)
})
describe('existing credit card used', () => {
it('should return true when cvv is invalid', () => {
self.controller.handleExistingPaymentLoading(true, true)
self.controller.isCvvValid = false
self.controller.handlePaymentChange({'card-type': 'visa'})

expect(self.controller.isContinueDisabled()).toBe(true)
})

it('should return true when cvv is valid and credit card is used', () => {
self.controller.handleExistingPaymentLoading(true, true)
self.controller.isCvvValid = true
self.controller.handlePaymentChange({'card-type': 'visa'})

expect(self.controller.getContinueDisabled()).toBe(false)
})
it('should return true when cvv is valid', () => {
self.controller.handleExistingPaymentLoading(true, true)
self.controller.isCvvValid = true
self.controller.handlePaymentChange({'card-type': 'visa'})
expect(self.controller.isContinueDisabled()).toBe(false)
})

it('should return true when cvv validity is undefined and credit card is used', () => {
self.controller.handleExistingPaymentLoading(true, true)
self.controller.isCvvValid = undefined
self.controller.handlePaymentChange({'card-type': 'visa'})

expect(self.controller.getContinueDisabled()).toBe(false)
})
it('should return true when cvv validity is undefined', () => {
self.controller.handleExistingPaymentLoading(true, true)
self.controller.isCvvValid = undefined
self.controller.handlePaymentChange({'card-type': 'visa'})
expect(self.controller.isContinueDisabled()).toBe(false)
})

it('should return false when cvv is invalid and credit card is used', () => {
self.controller.handleExistingPaymentLoading(true, true)
self.controller.isCvvValid = false
self.controller.handlePaymentChange({'account-type': 'checking'})

expect(self.controller.getContinueDisabled()).toBe(false)
it('should return false when cvv is invalid', () => {
self.controller.handleExistingPaymentLoading(true, true)
self.controller.isCvvValid = false
self.controller.handlePaymentChange({'account-type': 'checking'})

expect(self.controller.isContinueDisabled()).toBe(false)
})
})

it('should return false when cvv validity is undefined and EFT is used', () => {
self.controller.handleExistingPaymentLoading(true, true)
self.controller.isCvvValid = undefined
self.controller.handlePaymentChange({'account-type': 'checking'})

expect(self.controller.getContinueDisabled()).toBe(false)
})
describe('existing EFT used', () => {
it('should return false when cvv validity is undefined', () => {
self.controller.handleExistingPaymentLoading(true, true)
self.controller.isCvvValid = undefined
self.controller.handlePaymentChange({'account-type': 'checking'})

expect(self.controller.isContinueDisabled()).toBe(false)
})

it('should return false when cvv is valid and EFT is used', () => {
self.controller.handleExistingPaymentLoading(true, true)
self.controller.isCvvValid = true
self.controller.handlePaymentChange({'account-type': 'checking'})

expect(self.controller.getContinueDisabled()).toBe(false)
it('should return false when cvv is valid', () => {
self.controller.handleExistingPaymentLoading(true, true)
self.controller.isCvvValid = true
self.controller.handlePaymentChange({'account-type': 'checking'})

expect(self.controller.isContinueDisabled()).toBe(false)
})
})

it('should return false when cvv is invalid and new credit card payment is added', () => {
self.controller.handlePaymentChange({'card-type': 'visa'})
self.controller.isCvvValid = false
expect(self.controller.getContinueDisabled()).toBe(true)
describe('new credit card used', () => {
it('should return false when cvv is invalid and new credit card payment is added', () => {
self.controller.handlePaymentChange({'card-type': 'visa'})
self.controller.isCvvValid = false
expect(self.controller.isContinueDisabled()).toBe(true)
})
})

it('should return false when cvv is invalid and new EFT is added', () => {
self.controller.handlePaymentChange({'account-type': 'checking'})
self.controller.isCvvValid = false
expect(self.controller.getContinueDisabled()).toBe(false)
describe('new EFT used', () => {
it('should return false when cvv is invalid and new EFT is added', () => {
self.controller.handlePaymentChange({'account-type': 'checking'})
self.controller.isCvvValid = false
expect(self.controller.isContinueDisabled()).toBe(false)
})
})
})

describe('enableContinue', () => {
it('should set isCvvValid to false', () => {
self.controller.enableContinue(false)
Expand Down
2 changes: 1 addition & 1 deletion src/app/checkout/step-2/step-2.tpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
<button id="previousStepButton1" class="btn btn-default" ng-click="$ctrl.changeStep({newStep: 'contact'})">Previous Step</button>
</div>
<div class="col-sm-5 col-sm-offset-2">
<button id="continueCheckoutButton" class="btn btn-primary pull-right btn-block-mobile" ng-click="$ctrl.submit()" ng-disabled="$ctrl.getContinueDisabled()">
<button id="continueCheckoutButton" class="btn btn-primary pull-right btn-block-mobile" ng-click="$ctrl.submit()" ng-disabled="$ctrl.isContinueDisabled()">
Continue
</button>
<button id="previousStepButton2" class="btn btn-link btn-block visible-xs" ng-click="$ctrl.changeStep({newStep: 'contact'})"><i class="fa fa-angle-left"></i> Previous Step</button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ <h3 class="text-center">
</div>
</div>
</div>

0 comments on commit 8128308

Please sign in to comment.