describe ApplicationHelper::Button::InstanceAttach do
  describe '#disabled?' do
    it "when there are available volumes, then the button is enabled" do
      view_context = setup_view_context_with_sandbox({})

      tenant = FactoryBot.create(:cloud_tenant_openstack)
      FactoryBot.create(:cloud_volume_openstack, :cloud_tenant => tenant, :status => 'available')
      record = FactoryBot.create(:vm_openstack, :cloud_tenant => tenant)
      button = described_class.new(view_context, {}, {"record" => record}, {})
      expect(button.disabled?).to be false
    end

    it "when there are no available volumes then the button is disabled" do
      view_context = setup_view_context_with_sandbox({})
      tenant = FactoryBot.create(:cloud_tenant_openstack)
      FactoryBot.create(:cloud_volume_openstack, :cloud_tenant => tenant, :status => 'in-use')
      record = FactoryBot.create(:vm_openstack, :cloud_tenant => tenant)
      button = described_class.new(view_context, {}, {"record" => record}, {})
      expect(button.disabled?).to be true
    end
  end

  describe '#calculate_properties' do
    it "when there are no available volumes then the button has the error in the title" do
      view_context = setup_view_context_with_sandbox({})
      tenant = FactoryBot.create(:cloud_tenant_openstack)
      FactoryBot.create(:cloud_volume_openstack, :cloud_tenant => tenant, :status => 'in-use')
      record = FactoryBot.create(:vm_openstack, :cloud_tenant => tenant)
      button = described_class.new(view_context, {}, {"record" => record}, {})
      button.calculate_properties
      expect(button[:title]).to eq("There are no Cloud Volumes available to attach to this Instance.")
    end

    it "when there are available volumes, the button has no error in the title" do
      view_context = setup_view_context_with_sandbox({})
      tenant = FactoryBot.create(:cloud_tenant_openstack)
      FactoryBot.create(:cloud_volume_openstack, :cloud_tenant => tenant, :status => 'available')
      record = FactoryBot.create(:vm_openstack, :cloud_tenant => tenant)
      button = described_class.new(view_context, {}, {"record" => record}, {})
      button.calculate_properties
      expect(button[:title]).to be nil
    end
  end
end
