1# frozen_string_literal: true
2
3require "test_helper"
4require "payment_methods"
5
6class PaymentMethodsTest < Minitest::Test
7 def test_for_braintree_customer
8 braintree_customer = Minitest::Mock.new
9 braintree_customer.expect(:payment_methods, [
10 OpenStruct.new(card_type: "Test", last_4: "1234")
11 ])
12 methods = PaymentMethods.for_braintree_customer(braintree_customer)
13 assert_kind_of PaymentMethods, methods
14 end
15
16 def test_for_braintree_customer_no_methods
17 braintree_customer = Minitest::Mock.new
18 braintree_customer.expect(:payment_methods, [])
19 methods = PaymentMethods.for_braintree_customer(braintree_customer)
20 assert_raises do
21 methods.to_list_single
22 end
23 end
24
25 def test_default_payment_method
26 methods = PaymentMethods.new([
27 OpenStruct.new(card_type: "Test", last_4: "1234"),
28 OpenStruct.new(card_type: "Test", last_4: "1234", default?: true)
29 ])
30 assert_equal methods.fetch(1), methods.default_payment_method
31 end
32
33 def test_default_payment_method_index
34 methods = PaymentMethods.new([
35 OpenStruct.new(card_type: "Test", last_4: "1234"),
36 OpenStruct.new(card_type: "Test", last_4: "1234", default?: true)
37 ])
38 assert_equal "1", methods.default_payment_method_index
39 end
40
41 def test_to_options
42 methods = PaymentMethods.new([
43 OpenStruct.new(card_type: "Test", last_4: "1234")
44 ])
45 assert_equal(
46 [
47 { value: "0", label: "Test 1234" }
48 ],
49 methods.to_options
50 )
51 end
52
53 def test_to_list_single
54 methods = PaymentMethods.new([
55 OpenStruct.new(card_type: "Test", last_4: "1234")
56 ])
57 assert_equal(
58 {
59 var: "payment_method",
60 type: "list-single",
61 label: "Credit card to pay with",
62 required: true,
63 value: nil,
64 options: [
65 { value: "0", label: "Test 1234" }
66 ]
67 },
68 methods.to_list_single
69 )
70 end
71
72 class EmptyTest < Minitest::Test
73 def test_to_list_single
74 assert_raises do
75 PaymentMethods::Empty.new.to_list_single
76 end
77 end
78 end
79end