require "rails_helper"

RSpec.describe TrackAnalyticsJob, type: :job do
  let(:user) { create(:user) }
  let(:account) { user.primary_account }

  describe ".perform_now" do
    it "uses the given user and account to build an analytics tracker" do
      analytics = instance_double(Analytics)
      allow(Analytics).to receive(:new).and_return(analytics)
      allow(analytics).to receive(:track_video_upload)

      TrackAnalyticsJob.perform_now user, account, :track_video_upload, user_agent: "Mozilla Firefox", user_ip: "0.0.0.0"

      expect(Analytics).to have_received(:new).with(user, account)
    end

    it "calls the right analytics tracking method" do
      analytics = instance_double(Analytics)
      allow(Analytics).to receive(:new).and_return(analytics)
      allow(analytics).to receive(:track_video_upload)

      TrackAnalyticsJob.perform_now user, account, :track_video_upload, user_agent: "Mozilla Firefox", user_ip: "0.0.0.0"

      expect(analytics).to have_received(:track_video_upload)
    end

    it "passes arguments to the analytics tracking call" do
      account = build(:account)
      analytics = instance_double(Analytics)
      allow(Analytics).to receive(:new).and_return(analytics)
      allow(analytics).to receive(:track_create_native_release)

      TrackAnalyticsJob.perform_now(
        user, account,
        :track_create_native_release,
        release_type: "AppearanceRelease",
        account: account,
        user_agent: "Mozilla Firefox",
        user_ip: "0.0.0.0",
      )

      expect(analytics).to(
        have_received(:track_create_native_release)
        .with(release_type: "AppearanceRelease", account: account, user_agent: "Mozilla Firefox", user_ip: "0.0.0.0")
      )
    end
  end
end
