Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

3.9 KB
Raw Blame
# frozen_string_literal: true

require "spec_helper"

RSpec.describe AttributeHelpTextsController do
  let(:user) { build_stubbed(:user) }
  let(:model) { build_stubbed(:work_package_help_text) }

  let(:find_expectation) do
    allow(AttributeHelpText)
      .to receive(:find)
      .with(1234.to_s)
      .and_return(model)
  end

  before do
    login_as user

    mock_permissions_for(user) do |mock|
      mock.allow_globally :edit_attribute_help_texts
    end
  end

  describe "#index" do
    before do
      allow(AttributeHelpText).to receive(:all).and_return [model]

      get :index
    end

    it "is successful" do
      expect(response).to be_successful
      expect(assigns(:texts_by_type)).to eql("WorkPackage" => [model])
    end
  end

  describe "#show_dialog" do
    let(:visible_scope) { instance_double(ActiveRecord::Relation) }

    before do
      allow(AttributeHelpText)
        .to receive(:visible)
        .with(user)
        .and_return(visible_scope)

      find_expectation

      get :show_dialog, params: { id: 1234 }, format: :turbo_stream
    end

    context "when found" do
      let(:find_expectation) do
        allow(visible_scope)
          .to receive(:find)
          .with("1234")
          .and_return(model)
      end

      it "renders turbo stream dialog action", :aggregate_failures do
        expect(response).to be_successful
        expect(assigns(:attribute_help_text)).to eq model
        expect(response).to have_turbo_stream action: "dialog", target: "attribute-help-texts-show-dialog-component"
      end
    end

    context "when not found" do
      let(:find_expectation) do
        allow(visible_scope)
          .to receive(:find)
          .with("1234")
          .and_raise(ActiveRecord::RecordNotFound)
      end

      it "responds with 404 Not Found status", :aggregate_failures do
        expect(response).not_to be_successful
        expect(response).to have_http_status :not_found
      end
    end
  end

  describe "#edit" do
    before do
      find_expectation

      get :edit, params: { id: 1234 }
    end

    context "when found" do
      it "is successful" do
        expect(response).to be_successful
        expect(assigns(:attribute_help_text)).to eql model
      end
    end

    context "when not found" do
      let(:find_expectation) do
        allow(AttributeHelpText)
          .to receive(:find)
          .with(1234.to_s)
          .and_raise(ActiveRecord::RecordNotFound)
      end

      it "returns 404" do
        expect(response).to have_http_status :not_found
      end
    end
  end

  describe "#update" do
    let(:call) do
      put :update,
          params: {
            id: 1234,
            attribute_help_text: {
              help_text: "my new help text"
            }
          }
    end

    before do
      find_expectation
    end

    context "when save is success" do
      before do
        expect(model).to receive(:save).and_return(true)

        call
      end

      it "edits the announcement" do
        expect(response).to redirect_to action: :index, tab: "WorkPackage"
        expect(controller).to set_flash[:notice].to I18n.t(:notice_successful_update)

        expect(model.help_text).to eq("my new help text")
      end
    end

    context "when save is failure" do
      before do
        expect(model).to receive(:save).and_return(false)

        call
      end

      it "fails to update the announcement" do
        expect(response).to have_http_status(:unprocessable_entity)
        expect(response).to render_template "edit"
      end
    end

    context "when not found" do
      let(:find_expectation) do
        allow(AttributeHelpText)
          .to receive(:find)
          .with(1234.to_s)
          .and_raise(ActiveRecord::RecordNotFound)
      end

      before do
        call
      end

      it "returns 404" do
        expect(response).to have_http_status :not_found
      end
    end
  end
end