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")
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 end
17
18 def test_for_badcards
19 braintree_customer = Minitest::Mock.new
20 braintree_customer.expect(:payment_methods, [
21 OpenStruct.new(
22 card_type: "Test",
23 last_4: "1234",
24 unique_number_identifier: "wut"
25 )
26 ])
27 methods = PaymentMethods.for(braintree_customer, ["wut"])
28 assert_kind_of PaymentMethods, methods
29 assert_equal 0, methods.to_a.length
30 assert methods.empty?
31 end
32
33 def test_for_no_methods
34 braintree_customer = Minitest::Mock.new
35 braintree_customer.expect(:payment_methods, [])
36 methods = PaymentMethods.for(braintree_customer, [])
37 assert_kind_of PaymentMethods, methods
38 assert_raises do
39 methods.to_list_single
40 end
41 assert_equal 0, methods.to_a.length
42 assert methods.empty?
43 end
44
45 def test_default_payment_method
46 methods = PaymentMethods.new([
47 OpenStruct.new(card_type: "Test", last_4: "1234"),
48 OpenStruct.new(card_type: "Test", last_4: "1234", default?: true)
49 ])
50 assert_equal methods.fetch(1), methods.default_payment_method
51 end
52
53 def test_default_payment_method_index
54 methods = PaymentMethods.new([
55 OpenStruct.new(card_type: "Test", last_4: "1234"),
56 OpenStruct.new(card_type: "Test", last_4: "1234", default?: true)
57 ])
58 assert_equal "1", methods.default_payment_method_index
59 end
60
61 def test_to_options
62 methods = PaymentMethods.new([
63 OpenStruct.new(card_type: "Test", last_4: "1234")
64 ])
65 assert_equal(
66 [
67 { value: "0", label: "Test 1234" }
68 ],
69 methods.to_options
70 )
71 end
72
73 def test_to_list_single
74 methods = PaymentMethods.new([
75 OpenStruct.new(card_type: "Test", last_4: "1234")
76 ])
77 assert_equal(
78 {
79 var: "payment_method",
80 type: "list-single",
81 label: "Credit card to pay with",
82 required: true,
83 value: nil,
84 options: [
85 { value: "0", label: "Test 1234" }
86 ]
87 },
88 methods.to_list_single
89 )
90 end
91
92 class EmptyTest < Minitest::Test
93 def test_to_list_single
94 assert_raises do
95 PaymentMethods::Empty.new.to_list_single
96 end
97 end
98 end
99end