# frozen_string_literal: true
require "test_helper"
require "buy_account_credit_form"
require "customer"
Customer::BRAINTREE = Minitest::Mock.new
class BuyAccountCreditFormTest < Minitest::Test
def setup
@payment_method = OpenStruct.new(card_type: "Test", last_4: "1234")
@form = BuyAccountCreditForm.new(
BigDecimal("15.1234"),
PaymentMethods.new([@payment_method])
)
end
def test_for
braintree_customer = Minitest::Mock.new
Customer::BRAINTREE.expect(:customer, braintree_customer)
braintree_customer.expect(
:find,
EMPromise.resolve(OpenStruct.new(payment_methods: [])),
["test"]
)
assert_kind_of(
BuyAccountCreditForm,
BuyAccountCreditForm.for(customer).sync
)
end
em :test_for
def test_balance
assert_equal(
{ type: "fixed", value: "Current balance: $15.12" },
@form.balance
)
end
def test_add_to_form
iq_form = Blather::Stanza::X.new
@form.add_to_form(iq_form)
assert_equal :form, iq_form.type
assert_equal "Buy Account Credit", iq_form.title
assert_equal(
[
Blather::Stanza::X::Field.new(
type: "fixed",
value: "Current balance: $15.12"
),
Blather::Stanza::X::Field.new(
type: "list-single",
var: "payment_method",
label: "Credit card to pay with",
required: true,
options: [{ label: "Test 1234", value: "0" }]
),
BuyAccountCreditForm::AMOUNT_FIELD
],
iq_form.fields
)
end
def test_parse_amount
iq_form = Blather::Stanza::X.new
iq_form.fields = [{ var: "amount", value: "123" }]
assert_equal "123", @form.parse(iq_form)[:amount]
end
def test_parse_bad_amount
iq_form = Blather::Stanza::X.new
iq_form.fields = [{ var: "amount", value: "1" }]
assert_raises(BuyAccountCreditForm::AmountValidationError) do
@form.parse(iq_form)[:amount]
end
end
def test_parse_payment_method
iq_form = Blather::Stanza::X.new
iq_form.fields = [
{ var: "payment_method", value: "0" },
{ var: "amount", value: "15" }
]
assert_equal @payment_method, @form.parse(iq_form)[:payment_method]
end
end