require 'rails_helper'

RSpec.describe SubmitHubspotTaskRequestFormJob, type: :job do
  describe '#perform_now' do
    before do
      ENV["HUBSPOT_TASK_REQUEST_FORM_GUID"] = "hubspot_task_request_form_guid"
    end

    it 'submits to the Hubspot API with the right params' do
      form = double(:form)
      allow(Hubspot::Form).to receive(:new).and_return(form)
      allow(form).to receive(:submit).and_return(true)

      SubmitHubspotTaskRequestFormJob.perform_now("email@test.com", "https://example.com/admin/task_requests/1")

      expect(Hubspot::Form).to have_received(:new).with("guid" => "hubspot_task_request_form_guid")
      expect(form).to have_received(:submit).with(
        email: "email@test.com",
        taskme_url: "https://example.com/admin/task_requests/1"
      )
    end

    context 'when HUBSPOT_TASK_REQUEST_FORM_GUID is not available' do
      it 'does not submit to the API' do
        ENV["HUBSPOT_TASK_REQUEST_FORM_GUID"] = nil
        form = double(:form)
        allow(Hubspot::Form).to receive(:new).and_return(form)
        allow(form).to receive(:submit)

        SubmitHubspotTaskRequestFormJob.perform_now("email@test.com", "https://example.com/admin/task_requests/1")

        expect(form).not_to have_received(:submit)
      end
    end
  end
end
