require "rails_helper"

RSpec.describe ReportDownloadsController, type: :controller do
  render_views

  let(:current_user) { create(:user) }
  let(:project) { create(:project, :discovery_client, account: current_user.primary_account) }

  before do
    sign_in current_user
  end

  describe "#create" do
    it "Enques zip file generation job" do
      create_list(:download, 4, project_id: project.id, name: "#{project.name.parameterize}_appearance-releases")

      expect {
        post :create, params: { project_id: project.id }, format: :js
      }.to have_enqueued_job(GenerateReportsZipJob)
    end

    it "creates a download record with 'not_started' status" do
      create_list(:download, 4, project_id: project.id, name: "#{project.name.parameterize}_appearance-releases")

      expect {
        post :create, params: { project_id: project.id }, format: :js
      }.to change(Download, :count).by(1)

      expect(Download.last.status).to eq('not_started')
    end
    
    it "shows a notification to user" do
      allow(ProjectsChannel).to receive(:broadcast_download_generation_update).with(be_kind_of(Download), I18n.t("report_downloads.download.pending"))

      post :create, params: { project_id: project.id }, format: :js

      expect(ProjectsChannel).to have_received(:broadcast_download_generation_update).with(be_kind_of(Download), I18n.t("report_downloads.download.pending"))
    end
  end
end
