From 6f91014d646e0e082f6613fc6da6339ba84012e4 Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Wed, 28 Aug 2024 21:39:07 +0200 Subject: [PATCH] FIX: correctly filter user bookmarks (#28612) We were not updating `searchTerm` when changing the input which was making us always send an empty q parameter. This commit is also adding tests for: - initial url with q param - filtering the bookmarks through the input --- .../controllers/user-activity-bookmarks.js | 6 +- .../app/templates/user/bookmarks.hbs | 2 +- .../pages/user_activity_bookmarks.rb | 41 +++++++++++++ spec/system/user_activity_bookmarks_spec.rb | 61 +++++++++++++++++++ 4 files changed, 106 insertions(+), 4 deletions(-) create mode 100644 spec/system/page_objects/pages/user_activity_bookmarks.rb create mode 100644 spec/system/user_activity_bookmarks_spec.rb diff --git a/app/assets/javascripts/discourse/app/controllers/user-activity-bookmarks.js b/app/assets/javascripts/discourse/app/controllers/user-activity-bookmarks.js index 09173967c08..47a94c4b6b1 100644 --- a/app/assets/javascripts/discourse/app/controllers/user-activity-bookmarks.js +++ b/app/assets/javascripts/discourse/app/controllers/user-activity-bookmarks.js @@ -30,11 +30,11 @@ export default class UserActivityBookmarksController extends Controller { @computed("q") get searchTerm() { - return this.q; + return this._searchTerm || this.q; } set searchTerm(value) { - /* noop */ + this._searchTerm = value; } @discourseComputed() @@ -59,7 +59,7 @@ export default class UserActivityBookmarksController extends Controller { @action search() { this.router.transitionTo({ - queryParams: { q: this.searchTerm }, + queryParams: { q: this._searchTerm }, }); } diff --git a/app/assets/javascripts/discourse/app/templates/user/bookmarks.hbs b/app/assets/javascripts/discourse/app/templates/user/bookmarks.hbs index 2a7850319ea..b34c56a6f64 100644 --- a/app/assets/javascripts/discourse/app/templates/user/bookmarks.hbs +++ b/app/assets/javascripts/discourse/app/templates/user/bookmarks.hbs @@ -18,7 +18,7 @@ @type="text" @value={{this.searchTerm}} placeholder={{i18n "bookmarks.search_placeholder"}} - @enter={{action "search"}} + @enter={{this.search}} id="bookmark-search" autocomplete="off" /> diff --git a/spec/system/page_objects/pages/user_activity_bookmarks.rb b/spec/system/page_objects/pages/user_activity_bookmarks.rb new file mode 100644 index 00000000000..d2b5daa13f5 --- /dev/null +++ b/spec/system/page_objects/pages/user_activity_bookmarks.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +module PageObjects + module Pages + class UserActivityBookmarks < PageObjects::Pages::Base + def visit(user, q: nil) + url = "/u/#{user.username_lower}/activity/bookmarks" + url += "?q=#{q}" if q + page.visit(url) + self + end + + def search_for(query) + fill_in_search(query).submit_button.click + self + end + + def clear_query + fill_in_search("").submit_button.click + self + end + + def fill_in_search(query) + fill_in("bookmark-search", with: query) + self + end + + def has_topic?(topic) + has_content?(topic.title) + end + + def has_no_topic?(topic) + has_no_content?(topic.title) + end + + def submit_button + @submit_button ||= page.find(".bookmark-search-form button") + end + end + end +end diff --git a/spec/system/user_activity_bookmarks_spec.rb b/spec/system/user_activity_bookmarks_spec.rb new file mode 100644 index 00000000000..21260378d75 --- /dev/null +++ b/spec/system/user_activity_bookmarks_spec.rb @@ -0,0 +1,61 @@ +# frozen_string_literal: true + +describe "User activity bookmarks", type: :system do + fab!(:current_user) { Fabricate(:user) } + fab!(:bookmark_1) do + Fabricate( + :bookmark, + user: current_user, + name: "Bookmark 1", + bookmarkable: Fabricate(:post, raw: "a nice event"), + ) + end + fab!(:bookmark_2) do + Fabricate( + :bookmark, + user: current_user, + name: "Bookmark 2", + bookmarkable: Fabricate(:post, raw: "a pretty cat"), + ) + end + + let(:user_activity_bookmarks) { PageObjects::Pages::UserActivityBookmarks.new } + + before do + SearchIndexer.enable + SearchIndexer.index(bookmark_1.bookmarkable, force: true) + SearchIndexer.index(bookmark_2.bookmarkable, force: true) + Fabricate(:topic_user, user: current_user, topic: bookmark_1.bookmarkable.topic) + Fabricate(:topic_user, user: current_user, topic: bookmark_2.bookmarkable.topic) + + sign_in(current_user) + end + + after { SearchIndexer.disable } + + it "can filter the list of bookmarks from the URL" do + user_activity_bookmarks.visit(current_user, q: bookmark_1.bookmarkable.raw) + + expect(user_activity_bookmarks).to have_no_topic(bookmark_2.bookmarkable.topic) + expect(user_activity_bookmarks).to have_topic(bookmark_1.bookmarkable.topic) + end + + it "can filter the list of bookmarks" do + user_activity_bookmarks.visit(current_user).search_for(bookmark_2.bookmarkable.raw) + + expect(user_activity_bookmarks).to have_no_topic(bookmark_1.bookmarkable.topic) + expect(user_activity_bookmarks).to have_topic(bookmark_2.bookmarkable.topic) + end + + it "can clear the query" do + user_activity_bookmarks.visit(current_user).search_for(bookmark_2.bookmarkable.raw) + + expect(user_activity_bookmarks).to have_no_topic(bookmark_1.bookmarkable.topic) + expect(user_activity_bookmarks).to have_topic(bookmark_2.bookmarkable.topic) + + user_activity_bookmarks.clear_query + + expect(user_activity_bookmarks).to have_topic(bookmark_1.bookmarkable.topic) + expect(user_activity_bookmarks).to have_topic(bookmark_2.bookmarkable.topic) + end +end