1# frozen_string_literal: true
2
3require "test_helper"
4require "buy_account_credit_form"
5require "customer"
6
7Customer::BRAINTREE = Minitest::Mock.new
8
9class BuyAccountCreditFormTest < Minitest::Test
10 def setup
11 @payment_method = OpenStruct.new(card_type: "Test", last_4: "1234")
12 @form = BuyAccountCreditForm.new(
13 BigDecimal("15.1234"),
14 PaymentMethods.new([@payment_method])
15 )
16 end
17
18 def test_for
19 braintree_customer = Minitest::Mock.new
20 CustomerFinancials::BRAINTREE.expect(:customer, braintree_customer)
21 braintree_customer.expect(
22 :find,
23 EMPromise.resolve(OpenStruct.new(payment_methods: [])),
24 ["test"]
25 )
26
27 assert_kind_of(
28 BuyAccountCreditForm,
29 BuyAccountCreditForm.for(customer).sync
30 )
31 end
32 em :test_for
33
34 def test_balance
35 assert_equal(
36 { type: "fixed", value: "Current balance: $15.12" },
37 @form.balance
38 )
39 end
40
41 def test_add_to_form
42 iq_form = Blather::Stanza::X.new
43 @form.add_to_form(iq_form)
44 assert_equal :form, iq_form.type
45 assert_equal "Buy Account Credit", iq_form.title
46 assert_equal(
47 [
48 Blather::Stanza::X::Field.new(
49 type: "fixed",
50 value: "Current balance: $15.12"
51 ),
52 Blather::Stanza::X::Field.new(
53 type: "list-single",
54 var: "payment_method",
55 label: "Credit card to pay with",
56 required: true,
57 options: [{ label: "Test 1234", value: "0" }]
58 ),
59 BuyAccountCreditForm::AMOUNT_FIELD
60 ],
61 iq_form.fields
62 )
63 end
64
65 def test_parse_amount
66 iq_form = Blather::Stanza::X.new
67 iq_form.fields = [{ var: "amount", value: "123" }]
68 assert_equal "123", @form.parse(iq_form)[:amount]
69 end
70
71 def test_parse_bad_amount
72 iq_form = Blather::Stanza::X.new
73 iq_form.fields = [{ var: "amount", value: "1" }]
74 assert_raises(BuyAccountCreditForm::AmountValidationError) do
75 @form.parse(iq_form)[:amount]
76 end
77 end
78
79 def test_parse_payment_method
80 iq_form = Blather::Stanza::X.new
81 iq_form.fields = [
82 { var: "payment_method", value: "0" },
83 { var: "amount", value: "15" }
84 ]
85 assert_equal @payment_method, @form.parse(iq_form)[:payment_method]
86 end
87end