From d5378ec67ca88f6ed426dcea6b4df08e777d3cf8 Mon Sep 17 00:00:00 2001
From: Doug Stull <dstull@gitlab.com>
Date: Tue, 15 Aug 2023 11:29:40 -0400
Subject: [PATCH] Add testid capybara helper for rspec tests

- allows easy use of testid without having
  to remember syntax or recreate the setup
  of it.
- mirrors the frontend helper concept
---
 danger/qa_selector/Dangerfile                |  2 +-
 spec/features/abuse_report_spec.rb           | 10 +++++-----
 spec/features/profile_spec.rb                |  8 ++++----
 spec/spec_helper.rb                          |  1 +
 spec/support/helpers/features/dom_helpers.rb | 13 +++++++++++++
 5 files changed, 24 insertions(+), 10 deletions(-)
 create mode 100644 spec/support/helpers/features/dom_helpers.rb

diff --git a/danger/qa_selector/Dangerfile b/danger/qa_selector/Dangerfile
index 93692ba0d6c6d..57f992ec16035 100644
--- a/danger/qa_selector/Dangerfile
+++ b/danger/qa_selector/Dangerfile
@@ -2,7 +2,7 @@
 
 return if helper.stable_branch?
 
-data_testids = /testid|data-testid/
+data_testids = /testid|data-testid|find_by_testid|within_testid/
 
 deprecated_qa_selectors = /(?=qa_selector|data-qa-selector)|(?!.*\bdata-qa-)(?=class=.*qa-.*|class: .*qa-.*)/
 
diff --git a/spec/features/abuse_report_spec.rb b/spec/features/abuse_report_spec.rb
index 0312efeb9ceed..f934736ced9b5 100644
--- a/spec/features/abuse_report_spec.rb
+++ b/spec/features/abuse_report_spec.rb
@@ -61,7 +61,7 @@
 
         before do
           visit user_path(abusive_user)
-          find('[data-testid="base-dropdown-toggle"').click
+          find_by_testid('base-dropdown-toggle').click
         end
 
         it_behaves_like 'reports the user with an abuse category'
@@ -69,7 +69,7 @@
         it 'allows the reporter to report the same user for different abuse categories' do
           visit user_path(abusive_user)
 
-          find('[data-testid="base-dropdown-toggle"').click
+          find_by_testid('base-dropdown-toggle').click
           fill_and_submit_abuse_category_form
           fill_and_submit_report_abuse_form
 
@@ -77,7 +77,7 @@
 
           visit user_path(abusive_user)
 
-          find('[data-testid="base-dropdown-toggle"').click
+          find_by_testid('base-dropdown-toggle').click
           fill_and_submit_abuse_category_form("They're being offensive or abusive.")
           fill_and_submit_report_abuse_form
 
@@ -95,7 +95,7 @@
 
           visit user_path(abusive_user)
 
-          find('[data-testid="base-dropdown-toggle"').click
+          find_by_testid('base-dropdown-toggle').click
           fill_and_submit_abuse_category_form
           fill_and_submit_report_abuse_form
 
@@ -159,7 +159,7 @@
 
       before do
         visit project_merge_request_path(project, merge_request)
-        find('[data-testid="merge-request-actions"]').click
+        find_by_testid('merge-request-actions').click
       end
 
       it_behaves_like 'reports the user with an abuse category'
diff --git a/spec/features/profile_spec.rb b/spec/features/profile_spec.rb
index e190dfda937fc..ebe428a65d456 100644
--- a/spec/features/profile_spec.rb
+++ b/spec/features/profile_spec.rb
@@ -71,7 +71,7 @@
 
     previous_token = ''
 
-    within('[data-testid="feed-token-container"]') do
+    within_testid('feed-token-container') do
       previous_token = find_field('Feed token').value
 
       click_link('reset this token')
@@ -79,7 +79,7 @@
 
     accept_gl_confirm
 
-    within('[data-testid="feed-token-container"]') do
+    within_testid('feed-token-container') do
       click_button('Click to reveal')
 
       expect(find_field('Feed token').value).not_to eq(previous_token)
@@ -93,7 +93,7 @@
 
     previous_token = ''
 
-    within('[data-testid="incoming-email-token-container"]') do
+    within_testid('incoming-email-token-container') do
       previous_token = find_field('Incoming email token').value
 
       click_link('reset this token')
@@ -101,7 +101,7 @@
 
     accept_gl_confirm
 
-    within('[data-testid="incoming-email-token-container"]') do
+    within_testid('incoming-email-token-container') do
       click_button('Click to reveal')
 
       expect(find_field('Incoming email token').value).not_to eq(previous_token)
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index 19e32837b1356..d7ceab1289e8a 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -181,6 +181,7 @@
   config.include SearchHelpers, type: :feature
   config.include WaitHelpers, type: :feature
   config.include WaitForRequests, type: :feature
+  config.include Features::DomHelpers, type: :feature
   config.include EmailHelpers, :mailer, type: :mailer
   config.include Warden::Test::Helpers, type: :request
   config.include Gitlab::Routing, type: :routing
diff --git a/spec/support/helpers/features/dom_helpers.rb b/spec/support/helpers/features/dom_helpers.rb
new file mode 100644
index 0000000000000..ac6523f336041
--- /dev/null
+++ b/spec/support/helpers/features/dom_helpers.rb
@@ -0,0 +1,13 @@
+# frozen_string_literal: true
+
+module Features
+  module DomHelpers
+    def find_by_testid(testid)
+      page.find("[data-testid='#{testid}']")
+    end
+
+    def within_testid(testid, &block)
+      page.within("[data-testid='#{testid}']", &block)
+    end
+  end
+end
-- 
GitLab