Skip to content

Commit

Permalink
Do not focus custom fields in the create child dialog
Browse files Browse the repository at this point in the history
The field to focus is the subject field.

Also test that all custom fields are displayed in the create
child dialog.

Also updated the `:custom_field` factory for "hierarchy" to create the
root item using `CustomFields::Hierarchy::HierarchicalItemService.new`
by default. A factory should not created half-baked objects.
  • Loading branch information
cbliard committed Jan 16, 2025
1 parent c436989 commit 27a75c7
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 9 deletions.
8 changes: 2 additions & 6 deletions app/forms/custom_fields/custom_field_rendering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ def single_value_custom_field_input(builder, custom_field)
CustomFields::Inputs::Int.new(builder, **form_args)
when "float"
CustomFields::Inputs::Float.new(builder, **form_args)
when "hierarchy"
CustomFields::Inputs::SingleSelectList.new(builder, **form_args)
when "list"
when "hierarchy", "list"
CustomFields::Inputs::SingleSelectList.new(builder, **form_args)
when "date"
CustomFields::Inputs::Date.new(builder, **form_args)
Expand All @@ -103,9 +101,7 @@ def multi_value_custom_field_input(builder, custom_field)
form_args = form_arguments(custom_field)

case custom_field.field_format
when "hierarchy"
CustomFields::Inputs::MultiSelectList.new(builder, **form_args)
when "list"
when "hierarchy", "list"
CustomFields::Inputs::MultiSelectList.new(builder, **form_args)
when "user"
CustomFields::Inputs::MultiUserSelectList.new(builder, **form_args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def autocomplete_options
{
multiple: true,
decorated: decorated?,
focusDirectly: false,
append_to:
}
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def autocomplete_options
{
multiple: false,
decorated: decorated?,
focusDirectly: false,
append_to:
}
end
Expand Down
9 changes: 9 additions & 0 deletions spec/factories/custom_field_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,15 @@

trait :hierarchy do
field_format { "hierarchy" }
hierarchy_root do
service = CustomFields::Hierarchy::HierarchicalItemService.new
service.generate_root(instance).value!
end
end

trait :multi_hierarchy do
hierarchy
multi_value
end

factory :project_custom_field, class: "ProjectCustomField" do
Expand Down
44 changes: 44 additions & 0 deletions spec/features/work_packages/tabs/relations_children_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,48 @@
relations_tab.expect_no_new_relation_type("New child")
end
end

context "when all possible custom fields are there" do
let!(:user) { create(:admin) }

before do
# Introspect FactoryBot to find all traits used to create work package custom fields
traits = FactoryBot.factories[:wp_custom_field].defined_traits
traits = traits.reject { |t| t.name == "multi_value" }
traits = traits.map { |t| t.name.to_sym }

traits.each do |trait|
[true, false].each do |required| # rubocop:disable Performance/CollectionLiteralInLoop
cf = create(:wp_custom_field,
trait,
is_required: required)
project.types.first.custom_fields << cf
project.work_package_custom_fields << cf
end
end
end

it "displays a field for each required custom field" do
wp_page.visit_tab!("relations")
relations_tab.select_relation_type "New child"

project.work_package_custom_fields.each do |cf|
create_dialog.in_dialog do
if cf.required?
# `visible: :all` is needed as text custom field use a hidden textarea internally
expect(page).to have_field cf.name, visible: :all
else
expect(page).to have_no_field cf.name
end
end
end
end

it "focuses the subject input field" do
wp_page.visit_tab!("relations")
relations_tab.select_relation_type "New child"

create_dialog.expect_subject_field_focused
end
end
end
2 changes: 1 addition & 1 deletion spec/lib/journal_formatter/custom_field_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ def url_helper = Rails.application.routes.url_helpers
context "for hierarchy custom field" do
shared_let(:custom_field) { create(:hierarchy_wp_custom_field) }
shared_let(:service) { CustomFields::Hierarchy::HierarchicalItemService.new }
shared_let(:root) { service.generate_root(custom_field).value! }
shared_let(:root) { custom_field.hierarchy_root }
shared_let(:luke) { service.insert_item(parent: root, label: "luke", short: "LS").value! }
shared_let(:mara) { service.insert_item(parent: luke, label: "mara").value! }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
require Rails.root.join("db/migrate/20250102161733_adds_position_cache_to_hierarchy_items.rb")

RSpec.describe AddsPositionCacheToHierarchyItems, type: :model do
let(:custom_field) { create(:hierarchy_wp_custom_field) }
let(:custom_field) { create(:hierarchy_wp_custom_field, hierarchy_root: nil) }
let(:service) { CustomFields::Hierarchy::HierarchicalItemService.new }

it "backfills the position_cache value on already existing hierarchy items" do
Expand Down
2 changes: 1 addition & 1 deletion spec/models/custom_field/order_statements_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
# integration tests at spec/models/query/results_cf_sorting_integration_spec.rb
context "when hierarchy" do
let(:service) { CustomFields::Hierarchy::HierarchicalItemService.new }
let(:item) { service.generate_root(custom_field).value! }
let(:item) { custom_field.hierarchy_root }

subject(:custom_field) { create(:hierarchy_wp_custom_field) }

Expand Down
8 changes: 8 additions & 0 deletions spec/support/components/work_packages/create_dialog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ def expect_subject(value)
end
end

def expect_subject_field_focused
in_dialog do
subject_field = page.find_field("Subject")
subject_field_id_selector = "##{subject_field[:id]}"
expect(page).to have_focus_on(subject_field_id_selector)
end
end

def set_description(value)
@description.set_markdown(value)
end
Expand Down

0 comments on commit 27a75c7

Please sign in to comment.