Files
openfoodnetwork/spec/javascripts/unit/darkswarm/services/stripe_elements_spec.js.coffee
Matt-Yorkley 4cdb892c75 Refactor formatting of credit card brand names and reformat automatically when saving
This little bit of "translation" between what we might receive as input and what ActiveMerchant requires is important, but currently the responsibility for this job is spread all over the code base. It can now just live in the CreditCard model (in one place) and doesn't need to be duplicated anywhere else (like several different places in frontend Javascript!)
2021-12-02 16:22:52 +00:00

55 lines
2.0 KiB
CoffeeScript

describe 'StripeElements Service', ->
$httpBackend = $q = $rootScope = StripeElements = null
StripeMock = { createToken: null }
CardMock = { some: "card" }
beforeEach ->
module 'Darkswarm'
module ($provide) ->
$provide.value "railsFlash", null
null
inject (_StripeElements_, _$httpBackend_, _$q_, _$rootScope_) ->
$httpBackend = _$httpBackend_
StripeElements = _StripeElements_
$q = _$q_
$rootScope = _$rootScope_
describe "requestToken", ->
secrets = {}
submit = null
response = null
beforeEach inject ($window) ->
StripeElements.stripe = StripeMock
StripeElements.card = CardMock
describe "with satifactory data", ->
beforeEach ->
submit = jasmine.createSpy()
response = { token: { id: "token", card: { brand: 'MasterCard', last4: "5678", exp_month: 10, exp_year: 2099 } } }
StripeMock.createToken = => $q.when(response)
it "saves the response data to secrets, and submits the form", ->
StripeElements.requestToken(secrets, submit)
$rootScope.$digest() # required for #then to by called
expect(secrets.token).toEqual "token"
expect(secrets.cc_type).toEqual "MasterCard"
expect(submit).toHaveBeenCalled()
describe "with unsatifactory data", ->
beforeEach ->
submit = jasmine.createSpy()
response = { token: {id: "token" }, error: { message: 'There was a problem' } }
StripeMock.createToken = => $q.when(response)
it "doesn't submit the form, shows an error message instead", inject (Loading, RailsFlashLoader) ->
spyOn(Loading, "clear")
spyOn(RailsFlashLoader, "loadFlash")
spyOn(Bugsnag, "notify")
StripeElements.requestToken(secrets, submit).then (data) ->
expect(submit).not.toHaveBeenCalled()
expect(Loading.clear).toHaveBeenCalled()
expect(RailsFlashLoader.loadFlash).toHaveBeenCalledWith({error: "Error: There was a problem"})
expect(Bugsnag.notify).toHaveBeenCalled()