# frozen_string_literal: true

require "spec_helper"

module Decidim::Admin
  describe PromoteManagedUser do
    subject { described_class.new(form, user) }

    let(:organization) { create(:organization) }
    let!(:current_user) { create(:user, :admin, organization:) }
    let(:email) { "foo@example.org" }
    let(:form_params) do
      {
        email:
      }
    end
    let(:form) do
      ManagedUserPromotionForm.from_params(
        form_params
      ).with_context(
        current_organization: organization,
        current_user:
      )
    end
    let!(:user) { create(:user, :managed, organization:) }

    context "when everything is ok" do
      before do
        clear_enqueued_jobs
      end

      it "broadcasts ok" do
        expect { subject.call }.to broadcast(:ok)
      end

      it "user is invited to the application" do
        subject.call
        expect(user.reload.email).to eq(form.email)
        expect(ActionMailer::MailDeliveryJob).to have_been_enqueued.on_queue("mailers")
      end

      it "log promoted user" do
        subject.call
        expect(Decidim::ActionLog.last.action).to eq("promote")
      end
    end

    context "when the form is not valid" do
      before do
        allow(form).to receive(:invalid?).and_return(true)
      end

      it "broadcasts invalid" do
        expect { subject.call }.to broadcast(:invalid)
      end
    end

    context "when the user is not managed" do
      let(:user) { create(:user, organization:) }

      it "broadcasts invalid" do
        expect { subject.call }.to broadcast(:invalid)
      end
    end

    context "when the email address already exists" do
      let!(:other_user) { create(:user, email:, organization:) }

      it "broadcasts invalid" do
        expect { subject.call }.to broadcast(:invalid)
      end
    end
  end
end
