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 Customer::BRAINTREE.expect(:customer, braintree_customer)
21 braintree_customer.expect(:find, EMPromise.resolve(
22 OpenStruct.new(payment_methods: [])
23 ), ["test"])
24
25 assert_kind_of(
26 BuyAccountCreditForm,
27 BuyAccountCreditForm.for(customer).sync
28 )
29 end
30 em :test_for
31
32 def test_balance
33 assert_equal(
34 { type: "fixed", value: "Current balance: $15.12" },
35 @form.balance
36 )
37 end
38
39 def test_add_to_form
40 iq_form = Blather::Stanza::X.new
41 @form.add_to_form(iq_form)
42 assert_equal :form, iq_form.type
43 assert_equal "Buy Account Credit", iq_form.title
44 assert_equal(
45 [
46 Blather::Stanza::X::Field.new(
47 type: "fixed",
48 value: "Current balance: $15.12"
49 ),
50 Blather::Stanza::X::Field.new(
51 type: "list-single",
52 var: "payment_method",
53 label: "Credit card to pay with",
54 required: true,
55 options: [{ label: "Test 1234", value: "0" }]
56 ),
57 BuyAccountCreditForm::AMOUNT_FIELD
58 ],
59 iq_form.fields
60 )
61 end
62
63 def test_parse_amount
64 iq_form = Blather::Stanza::X.new
65 iq_form.fields = [{ var: "amount", value: "123" }]
66 assert_equal "123", @form.parse(iq_form)[:amount]
67 end
68
69 def test_parse_bad_amount
70 iq_form = Blather::Stanza::X.new
71 iq_form.fields = [{ var: "amount", value: "1" }]
72 assert_raises(BuyAccountCreditForm::AmountValidationError) do
73 @form.parse(iq_form)[:amount]
74 end
75 end
76
77 def test_parse_payment_method
78 iq_form = Blather::Stanza::X.new
79 iq_form.fields = [
80 { var: "payment_method", value: "0" },
81 { var: "amount", value: "15" }
82 ]
83 assert_equal @payment_method, @form.parse(iq_form)[:payment_method]
84 end
85end