A platform for community discussion. Free, open, simple.
FIX: Show 'Show more' link in categories modal after intersection observer loads (#34516)
Martin Brennan6b3c4385165f
diff --git a/app/assets/javascripts/discourse/app/components/sidebar/edit-navigation-menu/categories-modal.gjs b/app/assets/javascripts/discourse/app/components/sidebar/edit-navigation-menu/categories-modal.gjsindex 1cb6947cb0d..fffe55ebb82 100644--- a/app/assets/javascripts/discourse/app/components/sidebar/edit-navigation-menu/categories-modal.gjs+++ b/app/assets/javascripts/discourse/app/components/sidebar/edit-navigation-menu/categories-modal.gjs@@ -200,10 +200,10 @@ export default class SidebarEditNavigationMenuCategoriesModal extends Componentcategories = [...this.fetchedCategories.slice(index), ...categories];}- this.partialCategoryInfos = new Map([- ...this.partialCategoryInfos,- ...findPartialCategories(categories),- ]);+ // Recalculate the partialCategoryInfos using the full set of categories+ // to properly identify categories that now have exactly 5 subcategories+ // after loading more via the intersection observer+ this.partialCategoryInfos = findPartialCategories(this.fetchedCategories);this.recomputeGroupings();}@@ -224,10 +224,9 @@ export default class SidebarEditNavigationMenuCategoriesModal extends Component...this.fetchedCategories.slice(index),];- this.partialCategoryInfos = new Map([- ...this.partialCategoryInfos,- ...findPartialCategories(subcategories),- ]);+ // Recalculate partial categories based on the full set of categories+ // to ensure we properly identify categories with exactly 5 subcategories+ this.partialCategoryInfos = findPartialCategories(this.fetchedCategories);this.partialCategoryInfos.set(id, {offset: offset + subcategories.length,@@ -504,7 +503,7 @@ export default class SidebarEditNavigationMenuCategoriesModal extends Component<DButton@label="sidebar.categories_form_modal.show_more"@action={{fn this.loadSubcategories c.id c.offset}}- class="btn-flat"+ class="sidebar-categories-form__show-more-btn btn-flat"/></div></div>diff --git a/spec/system/editing_sidebar_categories_navigation_spec.rb b/spec/system/editing_sidebar_categories_navigation_spec.rbindex a0727b82f6d..8f1ae7eaa01 100644--- a/spec/system/editing_sidebar_categories_navigation_spec.rb+++ b/spec/system/editing_sidebar_categories_navigation_spec.rb@@ -197,6 +197,83 @@ RSpec.describe "Editing sidebar categories navigation", type: :system do)end+ context "when loading more subcategories via scrolling" do+ fab!(:parent_category) { Fabricate(:category, name: "parent category") }++ # Create 4 subcategories initially (under the threshold of 5)+ fab!(:subcategory1) do+ Fabricate(:category, parent_category_id: parent_category.id, name: "subcategory 1")+ end++ fab!(:subcategory2) do+ Fabricate(:category, parent_category_id: parent_category.id, name: "subcategory 2")+ end++ fab!(:subcategory3) do+ Fabricate(:category, parent_category_id: parent_category.id, name: "subcategory 3")+ end++ fab!(:subcategory4) do+ Fabricate(:category, parent_category_id: parent_category.id, name: "subcategory 4")+ end++ # Create additional subcategories that will be loaded via intersection observer+ fab!(:subcategory5) do+ Fabricate(:category, parent_category_id: parent_category.id, name: "subcategory 5")+ end++ fab!(:subcategory6) do+ Fabricate(:category, parent_category_id: parent_category.id, name: "subcategory 6")+ end++ before_all do+ Jobs.with_immediate_jobs do+ SearchIndexer.with_indexing do+ parent_category.index_search+ subcategory1.index_search+ subcategory2.index_search+ subcategory3.index_search+ subcategory4.index_search+ subcategory5.index_search+ subcategory6.index_search+ end+ end+ end++ # Test stub to force pagination in the category search+ around(:each) do |example|+ stub_const(Object, :CategoriesController, CategoriesController.clone) do+ stub_const(CategoriesController, :MAX_CATEGORIES_LIMIT, 5) { example.run }+ end+ end++ it "shows the 'Show more' link after loading additional subcategories via intersection observer" do+ visit "/latest"++ modal = sidebar.click_edit_categories_button++ expect(modal).to have_category_row(parent_category)+ expect(modal).to have_category_row(subcategory1)+ expect(modal).to have_category_row(subcategory2)+ expect(modal).to have_category_row(subcategory3)+ expect(modal).to have_category_row(subcategory4)++ expect(modal).to have_no_show_more_button++ modal.scroll_to_category(subcategory4)++ expect(modal).to have_category_row(subcategory5)++ expect(modal).to have_show_more_button++ # We need to briefly wait for things to settle, otherwise clicking the button doesn't work.+ wait_for_timeout(200)+ modal.click_show_more_button++ expect(page).to have_content(subcategory6.name)+ end+ end+context "when there are more categories than the page limit" doaround(:each) do |example|search_calls = 0diff --git a/spec/system/page_objects/modals/sidebar_edit_categories.rb b/spec/system/page_objects/modals/sidebar_edit_categories.rbindex 3d5ba3b224d..7a08b04a544 100644--- a/spec/system/page_objects/modals/sidebar_edit_categories.rb+++ b/spec/system/page_objects/modals/sidebar_edit_categories.rb@@ -50,6 +50,28 @@ module PageObjects".sidebar-categories-form .sidebar-categories-form__category-row[data-category-id='#{category.id}'] .sidebar-categories-form__input#{disabled ? "[disabled]" : ""}",)end++ def has_category_row?(category)+ has_css?(".sidebar-categories-form__category-row[data-category-id='#{category.id}']")+ end++ def has_no_show_more_button?+ has_no_css?(".sidebar-categories-form__show-more-btn")+ end++ def has_show_more_button?+ has_css?(".sidebar-categories-form__show-more-btn")+ end++ def scroll_to_category(category)+ scroll_to(find(".sidebar-categories-form__category-row[data-category-id='#{category.id}']"))+ self+ end++ def click_show_more_button+ find(".sidebar-categories-form__show-more-btn").click+ self+ endendendend