require "rails_helper"

describe Address do
  describe "#format" do
    it "prints the address in a single line" do
      address = Address.new("100 Test Lane", "Suite 1", "New York", "NY", "10001", "US")

      expect(address.to_s).to eq("100 Test Lane, Suite 1, New York, NY 10001, US")
    end

    it "prints the address condensed into two lines" do
      address = Address.new("100 Test Lane", "Suite 1", "New York", "NY", "10001", "US")

      actual = address.to_s(format: :condensed)
      expected = <<~END
        100 Test Lane, Suite 1
        New York, NY 10001, US
      END

      expect(actual).to eq(expected.chomp)
    end

    it "prints the address in full postal format" do
      address = Address.new("100 Test Lane", "Suite 1", "New York", "NY", "10001", "US")

      actual = address.to_s(format: :full)
      expected = <<~END
        100 Test Lane
        Suite 1
        New York, NY 10001
        US
      END

      expect(actual).to eq(expected.chomp)
    end

    context "when there is no 2nd street line" do
      it "does not add extra commas to the single line format" do
        address = Address.new("100 Test Lane", "", "New York", "NY", "10001", "US")

        expect(address.to_s).to eq("100 Test Lane, New York, NY 10001, US")
      end

      it "does not add extra newlines to the full format" do
        address = Address.new("100 Test Lane", "", "New York", "NY", "10001", "US")

        actual = address.to_s(format: :full)
        expected = <<~END
          100 Test Lane
          New York, NY 10001
          US
        END

        expect(actual).to eq(expected.chomp)
      end
    end
  end

  describe "#present?" do
    it "returns false when no parts of the address are present" do
      address = Address.new("", "", "", "", "", "")

      expect(address).not_to be_present
    end
    it "returns true when any part of the address is present" do
      %w(street1 street2 city state zip country).each do |part|
        address = Address.new("", "", "", "", "", "")

        expect(address).not_to be_present

        address.public_send("#{part}=", "value")

        expect(address).to be_present
      end
    end
  end
end
