require "rails_helper" RSpec.describe InterviewDownloadsController, type: :controller do render_views let(:current_user) { create(:user) } let(:project) { create(:project, :discovery_client, account: current_user.primary_account) } let(:casting_call) { create(:casting_call, project: project, title: "My Title") } let(:casting_call_interview) { create(:casting_call_interview, casting_call: casting_call, performer_name: "John Doe") } before do sign_in current_user end describe "#create" do it "enqueues zip file generation job" do expect { post :create, params: { project_id: project.id, casting_call_interview_id: casting_call_interview.id }, format: :js }.to have_enqueued_job(GenerateInterviewFilesZipJob) end it "creates a download record with 'not_started' status" do expect { post :create, params: { project_id: project.id, casting_call_interview_id: casting_call_interview.id }, format: :js }.to change(Download, :count).by(1) expect(Download.last.status).to eq('not_started') end context "When there is no existing job" do it "shows a notification to user" do allow(ProjectsChannel).to receive(:broadcast_download_generation_update).with(be_kind_of(Download), I18n.t("interview_downloads.download.pending", release_type: "Casting Call Interview")) post :create, params: { project_id: project.id, casting_call_interview_id: casting_call_interview.id }, format: :js expect(ProjectsChannel).to have_received(:broadcast_download_generation_update).with(be_kind_of(Download), I18n.t("interview_downloads.download.pending", release_type: "Casting Call Interview")) end end context "When there are existing jobs" do let(:appearance_release_download) { create(:download, project_id: project.id, name: "#{project.name.parameterize}_appearance-releases") } let(:acquired_media_release_download) { create(:download, project_id: project.id, name: "#{project.name.parameterize}_acquired-media-releases", release_type: "AcquiredMediaRelease") } before do allow(Download).to receive_message_chain(:unfinished_desc_order, :offset).and_return([acquired_media_release_download, appearance_release_download]) allow(ProjectsChannel).to receive(:broadcast_download_generation_update) end it "shows names of other contracts in the notification, which are in progress" do broadcast_message = "

Your Casting Call Interview files are being prepared for download. You will be notified when it's ready.\n

\n

The following downloads are also in progress:

\n\n" post :create, params: { project_id: project.id, casting_call_interview_id: casting_call_interview.id }, format: :js expect(ProjectsChannel).to have_received(:broadcast_download_generation_update).with(be_kind_of(Download), broadcast_message) end end end end