1# frozen_string_literal: true
2
3require "test_helper"
4require "payment_methods"
5
6class PaymentMethodsTest < Minitest::Test
7 def test_for
8 braintree_customer = Minitest::Mock.new
9 braintree_customer.expect(:payment_methods, [
10 OpenStruct.new(card_type: "Test", last_4: "1234", token: "wut")
11 ])
12 methods = PaymentMethods.for(braintree_customer, [], {})
13 assert_kind_of PaymentMethods, methods
14 assert_equal 1, methods.to_a.length
15 refute methods.empty?
16 assert_equal(
17 { payment_method_token: "wut" },
18 methods.fetch(0).transaction_details
19 )
20 end
21
22 def test_for_badcards
23 braintree_customer = Minitest::Mock.new
24 braintree_customer.expect(:payment_methods, [
25 OpenStruct.new(
26 card_type: "Test",
27 last_4: "1234",
28 unique_number_identifier: "wut"
29 )
30 ])
31 methods = PaymentMethods.for(braintree_customer, ["wut"], {})
32 assert_kind_of PaymentMethods, methods
33 assert_equal 0, methods.to_a.length
34 assert methods.empty?
35 end
36
37 def test_for_three_d_secure
38 braintree_customer = Minitest::Mock.new
39 braintree_customer.expect(:payment_methods, [
40 OpenStruct.new(
41 card_type: "Test",
42 last_4: "1234",
43 token: "wut"
44 )
45 ])
46 methods = PaymentMethods.for(braintree_customer, [], { "wut" => "hai" })
47 assert_kind_of PaymentMethods, methods
48 assert_equal(
49 { payment_method_token: "wut", three_d_secure_authentication_id: "hai" },
50 methods.fetch(0).transaction_details
51 )
52 end
53
54 def test_for_no_methods
55 braintree_customer = Minitest::Mock.new
56 braintree_customer.expect(:payment_methods, [])
57 methods = PaymentMethods.for(braintree_customer, [], {})
58 assert_kind_of PaymentMethods, methods
59 assert_raises do
60 methods.to_list_single
61 end
62 assert_equal 0, methods.to_a.length
63 assert methods.empty?
64 end
65
66 def test_default_payment_method
67 methods = PaymentMethods.new([
68 OpenStruct.new(card_type: "Test", last_4: "1234"),
69 OpenStruct.new(card_type: "Test", last_4: "1234", default?: true)
70 ])
71 assert_equal methods.fetch(1), methods.default_payment_method
72 end
73
74 def test_default_payment_method_index
75 methods = PaymentMethods.new([
76 OpenStruct.new(card_type: "Test", last_4: "1234"),
77 OpenStruct.new(card_type: "Test", last_4: "1234", default?: true)
78 ])
79 assert_equal "1", methods.default_payment_method_index
80 end
81
82 def test_to_options
83 methods = PaymentMethods.new([
84 OpenStruct.new(card_type: "Test", last_4: "1234")
85 ])
86 assert_equal(
87 [
88 { value: "0", label: "Test 1234" }
89 ],
90 methods.to_options
91 )
92 end
93
94 def test_to_list_single
95 methods = PaymentMethods.new([
96 OpenStruct.new(card_type: "Test", last_4: "1234")
97 ])
98 assert_equal(
99 {
100 var: "payment_method",
101 type: "list-single",
102 label: "Credit card to pay with",
103 required: true,
104 value: nil,
105 options: [
106 { value: "0", label: "Test 1234" }
107 ]
108 },
109 methods.to_list_single
110 )
111 end
112
113 class EmptyTest < Minitest::Test
114 def test_to_list_single
115 assert_raises do
116 PaymentMethods::Empty.new.to_list_single
117 end
118 end
119 end
120end