# frozen_string_literal: true

require "test_helper"
require "payment_methods"

class PaymentMethodsTest < Minitest::Test
	def test_for_braintree_customer
		braintree_customer = Minitest::Mock.new
		braintree_customer.expect(:payment_methods, [
			OpenStruct.new(card_type: "Test", last_4: "1234")
		])
		methods = PaymentMethods.for_braintree_customer(braintree_customer)
		assert_kind_of PaymentMethods, methods
	end

	def test_for_braintree_customer_no_methods
		braintree_customer = Minitest::Mock.new
		braintree_customer.expect(:payment_methods, [])
		methods = PaymentMethods.for_braintree_customer(braintree_customer)
		assert_raises do
			methods.to_list_single
		end
	end

	def test_default_payment_method
		methods = PaymentMethods.new([
			OpenStruct.new(card_type: "Test", last_4: "1234"),
			OpenStruct.new(card_type: "Test", last_4: "1234", default?: true)
		])
		assert_equal methods.fetch(1), methods.default_payment_method
	end

	def test_default_payment_method_index
		methods = PaymentMethods.new([
			OpenStruct.new(card_type: "Test", last_4: "1234"),
			OpenStruct.new(card_type: "Test", last_4: "1234", default?: true)
		])
		assert_equal "1", methods.default_payment_method_index
	end

	def test_to_options
		methods = PaymentMethods.new([
			OpenStruct.new(card_type: "Test", last_4: "1234")
		])
		assert_equal(
			[
				{ value: "0", label: "Test 1234" }
			],
			methods.to_options
		)
	end

	def test_to_list_single
		methods = PaymentMethods.new([
			OpenStruct.new(card_type: "Test", last_4: "1234")
		])
		assert_equal(
			{
				var: "payment_method",
				type: "list-single",
				label: "Credit card to pay with",
				required: true,
				value: nil,
				options: [
					{ value: "0", label: "Test 1234" }
				]
			},
			methods.to_list_single
		)
	end

	class EmptyTest < Minitest::Test
		def test_to_list_single
			assert_raises do
				PaymentMethods::Empty.new.to_list_single
			end
		end
	end
end
