test_buy_account_credit_form.rb

 1# frozen_string_literal: true
 2
 3require "test_helper"
 4require "buy_account_credit_form"
 5require "customer"
 6
 7class BuyAccountCreditFormTest < Minitest::Test
 8	def setup
 9		@payment_method = OpenStruct.new(card_type: "Test", last_4: "1234")
10		@form = BuyAccountCreditForm.new(
11			BigDecimal.new("12.1234"),
12			PaymentMethods.new([@payment_method])
13		)
14	end
15
16	def test_balance
17		assert_equal(
18			{ type: "fixed", value: "Current balance: $12.12" },
19			@form.balance
20		)
21	end
22
23	def test_add_to_form
24		iq_form = Blather::Stanza::X.new
25		@form.add_to_form(iq_form)
26		assert_equal :form, iq_form.type
27		assert_equal "Buy Account Credit", iq_form.title
28		assert_equal(
29			[
30				Blather::Stanza::X::Field.new(
31					type: "fixed",
32					value: "Current balance: $12.12"
33				),
34				Blather::Stanza::X::Field.new(
35					type: "list-single",
36					var: "payment_method",
37					label: "Credit card to pay with",
38					required: true,
39					options: [{ label: "Test 1234", value: "0" }]
40				),
41				BuyAccountCreditForm::AMOUNT_FIELD
42			],
43			iq_form.fields
44		)
45	end
46
47	def test_parse_amount
48		iq_form = Blather::Stanza::X.new
49		iq_form.fields = [{ var: "amount", value: "123" }]
50		assert_equal "123", @form.parse(iq_form)[:amount]
51	end
52
53	def test_parse_payment_method
54		iq_form = Blather::Stanza::X.new
55		iq_form.fields = [{ var: "payment_method", value: "0" }]
56		assert_equal @payment_method, @form.parse(iq_form)[:payment_method]
57	end
58end