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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Add component specs for modal
Aaron Contreras0c7ac2e267b1
diff --git a/app/components/work_packages/progress/base_modal_component.rb b/app/components/work_packages/progress/base_modal_component.rbnew file mode 100644index 00000000000..35f51105ba1--- /dev/null+++ b/app/components/work_packages/progress/base_modal_component.rb@@ -0,0 +1,85 @@+# frozen_string_literal: true++# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2024 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+# ++++module WorkPackages+ module Progress+ # rubocop:disable OpenProject/AddPreviewForViewComponent+ class BaseModalComponent < ApplicationComponent+ # rubocop:enable OpenProject/AddPreviewForViewComponent++ FIELD_MAP = {+ "estimatedTime" => :estimated_hours,+ "remainingTime" => :remaining_hours+ }.freeze++ include ApplicationHelper+ include Turbo::FramesHelper+ include OpPrimer::ComponentHelpers+ include OpenProject::StaticRouting::UrlHelpers++ attr_reader :work_package, :mode, :focused_field++ def initialize(work_package, focused_field: nil)+ super()++ @work_package = work_package+ @mode = :work_based+ @focused_field = map_field(focused_field)+ end++ def submit_path+ if work_package.new_record?+ url_for(controller: "work_packages/progress",+ action: "create")+ else+ url_for(controller: "work_packages/progress",+ action: "update",+ work_package_id: work_package.id)+ end+ end++ private++ def map_field(field)+ # Scenarios when a field is not provided occur after a+ # form submission since the last focused element+ # was the submit button. In this case, don't focus on+ # an element by default.+ return nil if field.nil?++ field = FIELD_MAP[field.to_s]++ return field if field.present?++ raise ArgumentError, "The selected field is not one of #{FIELD_MAP.keys.join(', ')}."+ end+ end+ end+enddiff --git a/app/components/work_packages/progress/status_based/modal_body_component.rb b/app/components/work_packages/progress/status_based/modal_body_component.rbindex 83c959022b1..8508df5a782 100644--- a/app/components/work_packages/progress/status_based/modal_body_component.rb+++ b/app/components/work_packages/progress/status_based/modal_body_component.rb@@ -32,46 +32,8 @@ module WorkPackagesmodule Progressmodule StatusBased# rubocop:disable OpenProject/AddPreviewForViewComponent- class ModalBodyComponent < ApplicationComponent+ class ModalBodyComponent < BaseModalComponent# rubocop:enable OpenProject/AddPreviewForViewComponent- include ApplicationHelper- include Turbo::FramesHelper- include OpPrimer::ComponentHelpers-- attr_reader :work_package, :mode, :focused_field-- FIELD_MAP = {- "status_id" => :status_id,- "estimatedTime" => :estimated_hours- }.freeze-- def initialize(work_package, focused_field: nil)- super()-- @work_package = work_package- @mode = :status_based- @focused_field = map_field(focused_field)- end-- private-- def submit_path- work_package.persisted? ? work_package_progress_path(work_package) : work_package_progress_path- end-- def map_field(field)- # Scenarios when a field is not provided occur after a- # form submission since the last focused element- # was the submit button. In this case, don't focus on- # an element by default.- return nil if field.nil?-- field = FIELD_MAP[field.to_s]-- return field if field.present?-- raise ArgumentError, "The selected field is not one of #{FIELD_MAP.keys.join(', ')}."- endendendenddiff --git a/app/components/work_packages/progress/work_based/modal_body_component.rb b/app/components/work_packages/progress/work_based/modal_body_component.rbindex ad62461fe49..81587205f2e 100644--- a/app/components/work_packages/progress/work_based/modal_body_component.rb+++ b/app/components/work_packages/progress/work_based/modal_body_component.rb@@ -32,47 +32,9 @@ module WorkPackagesmodule Progressmodule WorkBased# rubocop:disable OpenProject/AddPreviewForViewComponent- class ModalBodyComponent < ApplicationComponent- # rubocop:enable OpenProject/AddPreviewForViewComponent- include ApplicationHelper- include Turbo::FramesHelper- include OpPrimer::ComponentHelpers+ class ModalBodyComponent < BaseModalComponent; end- attr_reader :work_package, :mode, :focused_field-- FIELD_MAP = {- "estimatedTime" => :estimated_hours,- "remainingTime" => :remaining_hours- }.freeze-- def initialize(work_package, focused_field: nil)- super()-- @work_package = work_package- @mode = :work_based- @focused_field = map_field(focused_field)- end-- private-- def submit_path- work_package.persisted? ? work_package_progress_path(work_package) : work_package_progress_path- end-- def map_field(field)- # Scenarios when a field is not provided occur after a- # form submission since the last focused element- # was the submit button. In this case, don't focus on- # an element by default.- return nil if field.nil?-- field = FIELD_MAP[field.to_s]-- return field if field.present?-- raise ArgumentError, "The selected field is not one of #{FIELD_MAP.keys.join(', ')}."- end- end+ # rubocop:enable OpenProject/AddPreviewForViewComponentendendenddiff --git a/config/routes.rb b/config/routes.rbindex 8784166701c..3a8d46664a1 100644--- a/config/routes.rb+++ b/config/routes.rb@@ -524,7 +524,13 @@ Rails.application.routes.draw doendend- resource :progress, only: %i[create edit update], controller: "work_packages/progress"+ resource :progress, only: %i[edit update], controller: "work_packages/progress"+ collection do+ resource :progress,+ only: :create,+ controller: "work_packages/progress",+ as: :work_package_progress+ end# states managed by client-side (angular) routing on work_package#showget "/" => "work_packages#index", on: :collection, as: "index"diff --git a/spec/components/work_packages/progress/shared_modal_examples.rb b/spec/components/work_packages/progress/shared_modal_examples.rbnew file mode 100644index 00000000000..f5c9ba4e6ed--- /dev/null+++ b/spec/components/work_packages/progress/shared_modal_examples.rb@@ -0,0 +1,78 @@+# frozen_string_literal: true++# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2024 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+# ++++RSpec.shared_examples_for "progress modal validations" do+ describe "validations" do+ context "when focused_field is not an accepted value" do+ it "raises an ArgumentError" do+ work_package = build(:work_package)++ expect do+ described_class.new(work_package, focused_field: :foo)+ end.to raise_error(ArgumentError, /The selected field is not one of/)+ end+ end++ context "when given another progress related not accepted by the component" do+ it "raises an ArgumentError" do+ work_package = build(:work_package)++ expect do+ described_class.new(work_package, focused_field: "percentageDone")+ end.to raise_error(ArgumentError, /The selected field is not one of/)+ end+ end+ end+end++RSpec.shared_examples_for "progress modal submit path" do+ describe "#submit_path" do+ subject(:component) { described_class.new(work_package) }++ before { render_inline(component) }++ context "when the work package is already persisted" do+ let(:work_package) { create(:work_package) }++ it "returns work_package_progress_path with the work package" do+ expect(component.submit_path)+ .to eq(work_package_progress_path(work_package))+ end+ end++ context "when the work package is an unpersisted record" do+ let(:work_package) { WorkPackage.new }++ it "returns work_package_progress_path without a work package id" do+ expect(component.submit_path).to eq("/work_packages/progress")+ end+ end+ end+enddiff --git a/spec/components/work_packages/progress/status_based/modal_body_component_spec.rb b/spec/components/work_packages/progress/status_based/modal_body_component_spec.rbnew file mode 100644index 00000000000..b820f1fd11d--- /dev/null+++ b/spec/components/work_packages/progress/status_based/modal_body_component_spec.rb@@ -0,0 +1,53 @@+# frozen_string_literal: true++# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2010-2024 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+# +++require "spec_helper"+require_relative "../shared_modal_examples"++RSpec.describe WorkPackages::Progress::StatusBased::ModalBodyComponent,+ type: :component do+ include OpenProject::StaticRouting::UrlHelpers++ include_examples "progress modal validations"+ include_examples "progress modal submit path"++ describe "#focused_field" do+ subject(:component) { described_class.new(work_package, focused_field:) }++ let(:work_package) { build(:work_package) }++ context "when given estimatedTime" do+ let(:focused_field) { "estimatedTime" }++ it "returns :estimated_hours" do+ expect(component.focused_field).to eq(:estimated_hours)+ end+ end+ end+enddiff --git a/spec/components/work_packages/progress/work_based/modal_body_component_spec.rb b/spec/components/work_packages/progress/work_based/modal_body_component_spec.rbnew file mode 100644index 00000000000..b645358fb05--- /dev/null+++ b/spec/components/work_packages/progress/work_based/modal_body_component_spec.rb@@ -0,0 +1,61 @@+# frozen_string_literal: true++# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2010-2024 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+# +++require "spec_helper"+require_relative "../shared_modal_examples"++RSpec.describe WorkPackages::Progress::WorkBased::ModalBodyComponent,+ type: :component do+ include OpenProject::StaticRouting::UrlHelpers++ include_examples "progress modal validations"+ include_examples "progress modal submit path"++ describe "#focused_field" do+ subject(:component) { described_class.new(work_package, focused_field:) }++ let(:work_package) { build(:work_package) }++ context "when given estimatedTime" do+ let(:focused_field) { "estimatedTime" }++ it "returns :estimated_hours" do+ expect(component.focused_field).to eq(:estimated_hours)+ end+ end++ context "when given remainingTime" do+ let(:focused_field) { "remainingTime" }++ it "returns :remaining_hours" do+ expect(component.focused_field).to eq(:remaining_hours)+ end+ end+ end+end