Skip to content

Commit 35f611c

Browse files
authored
[63921] Wrong tab header is selected within a hierachy CF item (#19019)
Do not check for exact paths but allow parent and child routes to match
2 parents ef9b552 + 13851e4 commit 35f611c

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

app/helpers/tabs_helper.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def tab_label(tab)
6565
end
6666

6767
def selected_tab(tabs)
68-
selected = tabs.detect { |t| t[:name].to_s == params[:tab].to_s } || tabs.detect { |t| current_page?(t[:path]) }
68+
selected = tabs.detect { |t| t[:name].to_s == params[:tab].to_s } || tabs.detect { |t| tab_route_shown?(t) }
6969

7070
return selected unless selected.nil?
7171

@@ -78,4 +78,10 @@ def tabs_for_key(key, params = {})
7878
tab.dup.merge(path:)
7979
end
8080
end
81+
82+
def tab_route_shown?(tab)
83+
# Check not only for exact matches but also for sub-routes, like
84+
# /module_a/items and /module_a/items/:id
85+
request&.path&.starts_with?(tab[:path])
86+
end
8187
end

spec/features/custom_fields/hierarchy_custom_field_spec.rb

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,10 @@
3838

3939
before do
4040
allow(EnterpriseToken).to receive(:allows_to?).and_return(true)
41+
login_as admin
4142
end
4243

4344
it "lets you create, update and delete a custom field of type hierarchy" do
44-
login_as admin
45-
4645
# region CustomField creation
4746

4847
custom_field_index_page.visit!
@@ -77,7 +76,7 @@
7776
fill_in "Name", with: "", fill_options: { clear: :backspace }
7877
fill_in "Name", with: hierarchy_name
7978
click_on "Save"
80-
expect(page).to have_css(".PageHeader-title", text: hierarchy_name)
79+
expect(page).to have_heading(hierarchy_name)
8180

8281
# endregion
8382

@@ -158,4 +157,32 @@
158157

159158
# endregion
160159
end
160+
161+
context "when navigating the hierarchy" do
162+
let(:service) { CustomFields::Hierarchy::HierarchicalItemService.new }
163+
let(:custom_field) { create(:wp_custom_field, field_format: "hierarchy", hierarchy_root: nil) }
164+
let!(:root) { service.generate_root(custom_field).value! }
165+
let!(:luke) { service.insert_item(parent: root, label: "Luke", short: "LS").value! }
166+
let!(:r2d2) { service.insert_item(parent: luke, label: "R2-D2", short: "R2").value! }
167+
let!(:mouse) { service.insert_item(parent: r2d2, label: "Mouse Droid", short: "MD").value! }
168+
let!(:c3po) { service.insert_item(parent: luke, label: "C-3PO", short: "3PO").value! }
169+
let!(:mara) { service.insert_item(parent: root, label: "Mara", short: "MJ").value! }
170+
171+
before do
172+
custom_field.reload
173+
hierarchy_page.add_custom_field_state(custom_field)
174+
175+
visit custom_field_item_path(root.custom_field_id, luke)
176+
end
177+
178+
it "can navigate and keep the tab selection (regression #63921)" do
179+
# Expect items to be loaded and the tab nav to be selected correctly
180+
expect(page).to have_test_selector("op-custom-fields--hierarchy-item", count: 2)
181+
hierarchy_page.expect_tab "Items"
182+
183+
# Navigating to an item will keep the tab nav selection
184+
page.find_test_selector("op-custom-fields--hierarchy-item", text: "C-3PO").click
185+
hierarchy_page.expect_tab "Items"
186+
end
187+
end
161188
end

spec/support/pages/custom_fields/hierarchy_page.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ def switch_tab(tab)
5656
end
5757
end
5858

59+
def expect_tab(tab)
60+
@tab = tab.downcase
61+
62+
within_test_selector("custom_field_detail_header") do
63+
expect(page).to have_css("a[href='#{path}']", text: tab, aria: { current: "page" })
64+
end
65+
end
66+
5967
def open_action_menu_for(label)
6068
within_test_selector("op-custom-fields--hierarchy-item", text: label) do
6169
within_test_selector("op-hierarchy-item--action-menu") do

0 commit comments

Comments
 (0)