Skip to content

Commit 66dc683

Browse files
committed
Add unit tests for new inplace edit fields
1 parent 4630e05 commit 66dc683

22 files changed

+1229
-11
lines changed

spec/components/open_project/common/inplace_edit_field_component_spec.rb

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,38 @@
9898
render_inline(described_class.new(model: project, attribute: :description, update_registry:))
9999

100100
expect(rendered_content)
101-
.not_to include("click->inplace-edit#request")
101+
.not_to include("click->inplace-edit#request")
102102
expect(rendered_content)
103103
.to have_no_css(".op-inplace-edit--display-field.op-inplace-edit--display-field_editable")
104104
end
105105
end
106+
107+
describe "wrapper" do
108+
let(:allowed_attributes) { %w(description) }
109+
110+
it "renders a stable key on the wrapper for calculated field refresh" do
111+
render_inline(described_class.new(model: project, attribute: :description, update_registry:))
112+
113+
expected_key = "project_#{project.id}_description"
114+
expect(rendered_content)
115+
.to have_css("[data-inplace-edit-stable-key='#{expected_key}']")
116+
end
117+
end
118+
119+
describe "open_in_dialog" do
120+
let(:allowed_attributes) { %w(description) }
121+
122+
it "uses the dialog controller on the display field when open_in_dialog is true" do
123+
render_inline(
124+
described_class.new(
125+
model: project,
126+
attribute: :description,
127+
open_in_dialog: true,
128+
update_registry:
129+
)
130+
)
131+
132+
expect(rendered_content).to include("click->inplace-edit#openDialog")
133+
end
134+
end
106135
end
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# frozen_string_literal: true
2+
3+
#-- copyright
4+
# OpenProject is an open source project management software.
5+
# Copyright (C) the OpenProject GmbH
6+
#
7+
# This program is free software; you can redistribute it and/or
8+
# modify it under the terms of the GNU General Public License version 3.
9+
#
10+
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
11+
# Copyright (C) 2006-2013 Jean-Philippe Lang
12+
# Copyright (C) 2010-2013 the ChiliProject Team
13+
#
14+
# This program is free software; you can redistribute it and/or
15+
# modify it under the terms of the GNU General Public License
16+
# as published by the Free Software Foundation; either version 2
17+
# of the License, or (at your option) any later version.
18+
#
19+
# This program is distributed in the hope that it will be useful,
20+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
21+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22+
# GNU General Public License for more details.
23+
#
24+
# You should have received a copy of the GNU General Public License
25+
# along with this program; if not, write to the Free Software
26+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27+
#
28+
# See COPYRIGHT and LICENSE files for more details.
29+
#++
30+
require "rails_helper"
31+
32+
RSpec.describe OpenProject::Common::InplaceEditFieldDialogComponent, type: :component do
33+
include ViewComponent::TestHelpers
34+
35+
let(:project) { build_stubbed(:project) }
36+
let(:allowed_attributes) { %w[description] }
37+
let(:contract) do
38+
contract = instance_double(BaseContract)
39+
allow(contract).to receive(:writable?) { |attr| allowed_attributes.include?(attr.to_s) }
40+
allow(contract).to receive(:model).and_return(instance_double(Project))
41+
contract
42+
end
43+
let(:contract_class) do
44+
instance_double(Class).tap do |klass|
45+
allow(klass).to receive(:new).with(project, User.current).and_return(contract)
46+
end
47+
end
48+
let(:update_registry) do
49+
registry = OpenProject::InplaceEdit::UpdateRegistry.new
50+
registry.register(Project, handler: double, contract: contract_class)
51+
registry
52+
end
53+
54+
before { allow(User).to receive(:current).and_return(build_stubbed(:user)) }
55+
56+
it "renders a dialog with the expected ID and label" do
57+
render_inline(described_class.new(model: project, attribute: :description, system_arguments: { update_registry: }))
58+
59+
expect(rendered_content).to have_css("#inplace-edit-field-dialog--project-#{project.id}--description")
60+
expect(rendered_content).to have_text(Project.human_attribute_name(:description))
61+
end
62+
63+
it "uses system_arguments[:label] as dialog title when provided" do
64+
render_inline(
65+
described_class.new(
66+
model: project,
67+
attribute: :description,
68+
system_arguments: { update_registry:, label: "My Label" }
69+
)
70+
)
71+
72+
expect(rendered_content).to have_text("My Label")
73+
end
74+
75+
it "renders Cancel and Save buttons" do
76+
render_inline(described_class.new(model: project, attribute: :description, system_arguments: { update_registry: }))
77+
78+
expect(rendered_content).to have_button(I18n.t(:button_cancel))
79+
expect(rendered_content).to have_button(I18n.t(:button_save))
80+
end
81+
end
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# frozen_string_literal: true
2+
3+
#-- copyright
4+
# OpenProject is an open source project management software.
5+
# Copyright (C) the OpenProject GmbH
6+
#
7+
# This program is free software; you can redistribute it and/or
8+
# modify it under the terms of the GNU General Public License version 3.
9+
#
10+
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
11+
# Copyright (C) 2006-2013 Jean-Philippe Lang
12+
# Copyright (C) 2010-2013 the ChiliProject Team
13+
#
14+
# This program is free software; you can redistribute it and/or
15+
# modify it under the terms of the GNU General Public License
16+
# as published by the Free Software Foundation; either version 2
17+
# of the License, or (at your option) any later version.
18+
#
19+
# This program is distributed in the hope that it will be useful,
20+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
21+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22+
# GNU General Public License for more details.
23+
#
24+
# You should have received a copy of the GNU General Public License
25+
# along with this program; if not, write to the Free Software
26+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27+
#
28+
# See COPYRIGHT and LICENSE files for more details.
29+
#++
30+
require "rails_helper"
31+
32+
RSpec.describe OpenProject::Common::InplaceEditFields::BooleanInputComponent,
33+
type: :component do
34+
include ViewComponent::TestHelpers
35+
36+
let(:project) { build_stubbed(:project) }
37+
38+
it "renders a checkbox for the attribute with stimulus controller attached" do
39+
component_class = described_class
40+
render_in_view_context(project) do |model|
41+
primer_form_with(url: "/foo", model:) do |f|
42+
render_inline_form(f) do |form|
43+
render component_class.new(form:, model:, attribute: :name, label: "Name")
44+
end
45+
end
46+
end
47+
48+
expect(rendered_content).to have_field("project[name]", type: "checkbox")
49+
expect(rendered_content).to include("click->inplace-edit#submitForm")
50+
end
51+
52+
it "does not add a submit-on-click Stimulus action whe show_action_buttons is false" do
53+
component_class = described_class
54+
render_in_view_context(project) do |model|
55+
primer_form_with(url: "/foo", model:) do |f|
56+
render_inline_form(f) do |form|
57+
render component_class.new(form:, model:, attribute: :name, label: "Name", show_action_buttons: false)
58+
end
59+
end
60+
end
61+
62+
expect(rendered_content).not_to include("click->inplace-edit#submitForm")
63+
end
64+
end
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# frozen_string_literal: true
2+
3+
#-- copyright
4+
# OpenProject is an open source project management software.
5+
# Copyright (C) the OpenProject GmbH
6+
#
7+
# This program is free software; you can redistribute it and/or
8+
# modify it under the terms of the GNU General Public License version 3.
9+
#
10+
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
11+
# Copyright (C) 2006-2013 Jean-Philippe Lang
12+
# Copyright (C) 2010-2013 the ChiliProject Team
13+
#
14+
# This program is free software; you can redistribute it and/or
15+
# modify it under the terms of the GNU General Public License
16+
# as published by the Free Software Foundation; either version 2
17+
# of the License, or (at your option) any later version.
18+
#
19+
# This program is distributed in the hope that it will be useful,
20+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
21+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22+
# GNU General Public License for more details.
23+
#
24+
# You should have received a copy of the GNU General Public License
25+
# along with this program; if not, write to the Free Software
26+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27+
#
28+
# See COPYRIGHT and LICENSE files for more details.
29+
#++
30+
require "rails_helper"
31+
32+
RSpec.describe OpenProject::Common::InplaceEditFields::CalculatedValueInputComponent,
33+
type: :component do
34+
include ViewComponent::TestHelpers
35+
36+
let(:project) { build_stubbed(:project) }
37+
38+
it "renders a readonly text input without buttons" do
39+
component_class = described_class
40+
render_in_view_context(project) do |model|
41+
primer_form_with(url: "/foo", model:) do |f|
42+
render_inline_form(f) do |form|
43+
render component_class.new(form:, model:, attribute: :name, label: "Name")
44+
end
45+
end
46+
end
47+
48+
expect(rendered_content).to have_field("project[name]", type: "text", readonly: true)
49+
50+
expect(rendered_content).to have_no_button(I18n.t(:button_save))
51+
expect(rendered_content).to have_no_button(I18n.t(:button_cancel))
52+
end
53+
end
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# frozen_string_literal: true
2+
3+
#-- copyright
4+
# OpenProject is an open source project management software.
5+
# Copyright (C) the OpenProject GmbH
6+
#
7+
# This program is free software; you can redistribute it and/or
8+
# modify it under the terms of the GNU General Public License version 3.
9+
#
10+
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
11+
# Copyright (C) 2006-2013 Jean-Philippe Lang
12+
# Copyright (C) 2010-2013 the ChiliProject Team
13+
#
14+
# This program is free software; you can redistribute it and/or
15+
# modify it under the terms of the GNU General Public License
16+
# as published by the Free Software Foundation; either version 2
17+
# of the License, or (at your option) any later version.
18+
#
19+
# This program is distributed in the hope that it will be useful,
20+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
21+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22+
# GNU General Public License for more details.
23+
#
24+
# You should have received a copy of the GNU General Public License
25+
# along with this program; if not, write to the Free Software
26+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27+
#
28+
# See COPYRIGHT and LICENSE files for more details.
29+
#++
30+
require "rails_helper"
31+
32+
RSpec.describe OpenProject::Common::InplaceEditFields::DateInputComponent,
33+
type: :component do
34+
include ViewComponent::TestHelpers
35+
36+
let(:project) { build_stubbed(:project) }
37+
38+
it "renders a date input for the attribute with Stimulus controller attached" do
39+
component_class = described_class
40+
render_in_view_context(project) do |model|
41+
primer_form_with(url: "/foo", model:) do |f|
42+
render_inline_form(f) do |form|
43+
render component_class.new(form:, model:, attribute: :name, label: "Name")
44+
end
45+
end
46+
end
47+
48+
expect(rendered_content).to have_field("project[name]", type: "date")
49+
expect(rendered_content).to include("change->inplace-edit#submitForm")
50+
end
51+
52+
it "does not add a submit-on-change Stimulus action whe show_action_buttons is false" do
53+
component_class = described_class
54+
render_in_view_context(project) do |model|
55+
primer_form_with(url: "/foo", model:) do |f|
56+
render_inline_form(f) do |form|
57+
render component_class.new(form:, model:, attribute: :name, label: "Name", show_action_buttons: false)
58+
end
59+
end
60+
end
61+
62+
expect(rendered_content).not_to include("change->inplace-edit#submitForm")
63+
end
64+
end
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# frozen_string_literal: true
2+
3+
#-- copyright
4+
# OpenProject is an open source project management software.
5+
# Copyright (C) the OpenProject GmbH
6+
#
7+
# This program is free software; you can redistribute it and/or
8+
# modify it under the terms of the GNU General Public License version 3.
9+
#
10+
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
11+
# Copyright (C) 2006-2013 Jean-Philippe Lang
12+
# Copyright (C) 2010-2013 the ChiliProject Team
13+
#
14+
# This program is free software; you can redistribute it and/or
15+
# modify it under the terms of the GNU General Public License
16+
# as published by the Free Software Foundation; either version 2
17+
# of the License, or (at your option) any later version.
18+
#
19+
# This program is distributed in the hope that it will be useful,
20+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
21+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22+
# GNU General Public License for more details.
23+
#
24+
# You should have received a copy of the GNU General Public License
25+
# along with this program; if not, write to the Free Software
26+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27+
#
28+
# See COPYRIGHT and LICENSE files for more details.
29+
#++
30+
require "rails_helper"
31+
32+
RSpec.describe OpenProject::Common::InplaceEditFields::DisplayFields::CalculatedValueInputComponent,
33+
type: :component do
34+
include ViewComponent::TestHelpers
35+
36+
let(:project) { build_stubbed(:project, name: "42") }
37+
38+
it "never attaches the inplace-edit Stimulus controller, even when writable is passed" do
39+
render_inline(
40+
described_class.new(model: project, attribute: :name, writable: true, truncated: false, id: "cf-42")
41+
)
42+
43+
expect(rendered_content).not_to include("click->inplace-edit#request")
44+
expect(rendered_content).to have_no_css(".op-inplace-edit--display-field_editable")
45+
end
46+
47+
it "renders the not-editable tooltip" do
48+
render_inline(
49+
described_class.new(model: project, attribute: :name, writable: false, truncated: false, id: "cf-42")
50+
)
51+
52+
expect(rendered_content).to have_text(I18n.t("custom_fields.calculated_field_not_editable"))
53+
end
54+
end

0 commit comments

Comments
 (0)