Simplify using loading and flash messages together

This commit is contained in:
Maikel Linke
2020-08-26 15:02:21 +10:00
parent da1569abda
commit f435039061
10 changed files with 116 additions and 34 deletions

View File

@@ -1,5 +1,9 @@
describe 'CreditCard service', ->
CreditCard = null
CreditCards = null
$http = null
Loading = null
RailsFlashLoader = null
beforeEach ->
module 'Darkswarm'
@@ -8,19 +12,43 @@ describe 'CreditCard service', ->
$provide.value "railsFlash", null
null
inject (_CreditCard_)->
inject (_CreditCard_, _CreditCards_, _$httpBackend_, _Loading_, _RailsFlashLoader_)->
CreditCard = _CreditCard_
CreditCards = _CreditCards_
$http = _$httpBackend_
Loading = _Loading_
RailsFlashLoader = _RailsFlashLoader_
CreditCard.secrets =
card:
exp_month: "12"
exp_year: "2030"
last4: "1234"
cc_type: 'mastercard'
token: "token123"
describe "submit", ->
it "adds a credit card", ->
$http.expectPUT("/credit_cards/new_from_token").respond(200, {})
spyOn(CreditCards, "add")
CreditCard.submit()
$http.flush()
expect(CreditCards.add).toHaveBeenCalled()
it "reports errors", ->
$http.expectPUT("/credit_cards/new_from_token").respond(500, {})
spyOn(Loading, "clear")
spyOn(RailsFlashLoader, "loadFlash")
CreditCard.submit()
$http.flush()
expect(Loading.clear).toHaveBeenCalled()
expect(RailsFlashLoader.loadFlash).toHaveBeenCalled()
describe "process_params", ->
beforeEach ->
CreditCard.secrets =
card:
exp_month: "12"
exp_year: "2030"
last4: "1234"
cc_type: 'mastercard'
token: "token123"
it "uses cc_type, rather than fetching the brand from the card", ->
# This is important for processing the card with activemerchant
process_params = CreditCard.process_params()

View File

@@ -0,0 +1,41 @@
describe 'Messages service', ->
Messages = null
Loading = null
RailsFlashLoader = null
beforeEach ->
module 'Darkswarm'
module ($provide)->
$provide.value "railsFlash", null
null
inject (_Messages_, _Loading_, _RailsFlashLoader_)->
Messages = _Messages_
Loading = _Loading_
RailsFlashLoader = _RailsFlashLoader_
it "shows a loading message", ->
Messages.loading("Hang on...")
expect(Loading.message).toEqual "Hang on..."
it "shows a success message", ->
spyOn(RailsFlashLoader, "loadFlash")
Messages.success("Yay!")
expect(RailsFlashLoader.loadFlash).toHaveBeenCalledWith({success: "Yay!"})
it "shows a error message", ->
spyOn(RailsFlashLoader, "loadFlash")
Messages.error("Boo!")
expect(RailsFlashLoader.loadFlash).toHaveBeenCalledWith({error: "Boo!"})
it "shows a flash message", ->
data = {info: "thinking"}
spyOn(RailsFlashLoader, "loadFlash")
Messages.flash(data)
expect(RailsFlashLoader.loadFlash).toHaveBeenCalledWith(data)
it "clears a loading message", ->
Messages.loading("Hang on...")
Messages.success("Done.")
expect(Loading.message).toEqual null