test_payment_methods.rb

 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 "1", methods.default_payment_method
31	end
32
33	def test_to_options
34		methods = PaymentMethods.new([
35			OpenStruct.new(card_type: "Test", last_4: "1234")
36		])
37		assert_equal(
38			[
39				{ value: "0", label: "Test 1234" }
40			],
41			methods.to_options
42		)
43	end
44
45	def test_to_list_single
46		methods = PaymentMethods.new([
47			OpenStruct.new(card_type: "Test", last_4: "1234")
48		])
49		assert_equal(
50			{
51				var: "payment_method",
52				type: "list-single",
53				label: "Credit card to pay with",
54				required: true,
55				value: "",
56				options: [
57					{ value: "0", label: "Test 1234" }
58				]
59			},
60			methods.to_list_single
61		)
62	end
63
64	class EmptyTest < Minitest::Test
65		def test_to_list_single
66			assert_raises do
67				PaymentMethods::Empty.new.to_list_single
68			end
69		end
70	end
71end