Add call to $evalAsync() after Loading and FlashLoader are updated so

that a angular digest is triggered

This is required so that Loading.clear triggers a refresh and makes its placeholder to be cleared
This commit is contained in:
Luis Ramos
2020-03-30 19:31:47 +01:00
parent c3d25bf163
commit 957b398a54
2 changed files with 10 additions and 5 deletions

View File

@@ -15,6 +15,7 @@ Darkswarm.factory 'StripeElements', ($rootScope, Loading, RailsFlashLoader) ->
if(response.error)
Loading.clear()
RailsFlashLoader.loadFlash({error: t("error") + ": #{response.error.message}"})
@triggerAngularDigest()
else
secrets.token = response.token.id
secrets.cc_type = @mapCC(response.token.card.brand)
@@ -32,12 +33,17 @@ Darkswarm.factory 'StripeElements', ($rootScope, Loading, RailsFlashLoader) ->
if(response.error)
Loading.clear()
RailsFlashLoader.loadFlash({error: t("error") + ": #{response.error.message}"})
@triggerAngularDigest()
else
secrets.token = response.paymentMethod.id
secrets.cc_type = response.paymentMethod.card.brand
secrets.card = response.paymentMethod.card
submit()
triggerAngularDigest: ->
# $evalAsync is improved way of triggering a digest without calling $apply
$rootScope.$evalAsync()
# Maps the brand returned by Stripe to that required by activemerchant
mapCC: (ccType) ->
switch ccType

View File

@@ -46,11 +46,10 @@ describe 'StripeElements Service', ->
it "doesn't submit the form, shows an error message instead", inject (Loading, RailsFlashLoader) ->
spyOn(Loading, "clear")
spyOn(RailsFlashLoader, "loadFlash")
StripeElements.requestToken(secrets, submit)
$rootScope.$digest() # required for #then to by called
expect(submit).not.toHaveBeenCalled()
expect(Loading.clear).toHaveBeenCalled()
expect(RailsFlashLoader.loadFlash).toHaveBeenCalledWith({error: "Error: There was a problem"})
StripeElements.requestToken(secrets, submit).then (data) ->
expect(submit).not.toHaveBeenCalled()
expect(Loading.clear).toHaveBeenCalled()
expect(RailsFlashLoader.loadFlash).toHaveBeenCalledWith({error: "Error: There was a problem"})
describe 'mapCC', ->
it "maps the brand returned by Stripe to that required by activemerchant", ->