require 'rails_helper'

RSpec.describe TaxTransaction, type: :model do
  let(:tax_transaction) { Fabricate.build(:tax_transaction) }
  let(:order) { Fabricate(:order) }

  it_behaves_like 'a papertrail versioned model', :tax_transaction

  describe 'validations' do
    it 'requires order' do
      subject = TaxTransaction.new
      expect(subject).not_to be_valid
      expect(subject.errors.full_messages).to include('Order must exist')
    end

    it 'expects a transaction_type of invoice, return, or estimate' do
      subject = TaxTransaction.new(order: order)
      TaxTransaction::TRANSACTION_TYPES.each do |valid_type|
        subject.transaction_type = valid_type
        expect(subject).to be_valid
      end
      subject.transaction_type = 'some other transaction'
      expect(subject).not_to be_valid
    end
  end

  describe 'fields' do
    it '#total_tax returns the totalTax property of the response payload, converted to cents' do
      transaction = Fabricate.build(:tax_transaction, response_payload: { 'totalTax' => 3.00 })
      expect(transaction.total_tax_cents).to eq 300
    end

    it '#status returns the status property of the response payload' do
      transaction = Fabricate.build(:tax_transaction, response_payload: { 'status' => 'Committed' })
      expect(transaction.status).to eq 'Committed'
    end
  end
end
