require 'rails_helper'

RSpec.describe SubmitHubspotFormJob, type: :job do
  describe '#perform_now' do
    before do
      ENV["HUBSPOT_FORM_GUID"] = "hubspot_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)

      SubmitHubspotFormJob.perform_now("email@test.com", "My Account")

      expect(Hubspot::Form).to have_received(:new).with("guid" => "hubspot_form_guid")
      expect(form).to have_received(:submit).with(
        email: "email@test.com",
        company: "My Account"
      )
    end

    it 'allows additional params to be submitted' do
      form = double(:form)
      allow(Hubspot::Form).to receive(:new).and_return(form)
      allow(form).to receive(:submit).and_return(true)

      SubmitHubspotFormJob.perform_now("email@test.com", "My Account", additional_param_one: "Foo", additional_param_two: "Bar")

      expect(form).to have_received(:submit).with(
        email: "email@test.com",
        company: "My Account",
        additional_param_one: "Foo",
        additional_param_two: "Bar",
      )
    end

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

        SubmitHubspotFormJob.perform_now("email@test.com", "My Account")

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