From f6d885a56c1a94cb37701cc0a126384a1cd57986 Mon Sep 17 00:00:00 2001
From: Ross Byrne <robyrne@gitlab.com>
Date: Mon, 26 Feb 2024 08:39:15 +0000
Subject: [PATCH] Resolve data testid finder Rubocop exceptions

Fixes rubocop exceptions in tests no 13
---
 .rubocop_todo/capybara/testid_finders.yml     | 10 -----
 ...er_interacts_with_batched_mr_diffs_spec.rb | 10 ++++-
 .../user_sees_collaboration_links_spec.rb     |  4 +-
 .../projects/sub_group_issuables_spec.rb      |  2 +-
 spec/features/projects/terraform_spec.rb      |  8 ++--
 .../projects/tree/create_directory_spec.rb    |  2 +-
 .../projects/tree/create_file_spec.rb         |  2 +-
 .../work_items/work_item_children_spec.rb     | 44 +++++++++----------
 .../projects/work_items/work_item_spec.rb     | 20 ++++-----
 spec/features/protected_branches_spec.rb      |  7 ++-
 spec/features/runners_spec.rb                 | 38 ++++++++--------
 11 files changed, 73 insertions(+), 74 deletions(-)

diff --git a/.rubocop_todo/capybara/testid_finders.yml b/.rubocop_todo/capybara/testid_finders.yml
index 16be25fbc9b51..7bc953eb2ca37 100644
--- a/.rubocop_todo/capybara/testid_finders.yml
+++ b/.rubocop_todo/capybara/testid_finders.yml
@@ -1,18 +1,8 @@
 ---
 Capybara/TestidFinders:
   Exclude:
-    - 'spec/features/merge_request/user_interacts_with_batched_mr_diffs_spec.rb'
     - 'spec/features/projects/settings/registry_settings_cleanup_tags_spec.rb'
     - 'spec/features/projects/settings/registry_settings_spec.rb'
-    - 'spec/features/projects/show/user_sees_collaboration_links_spec.rb'
-    - 'spec/features/projects/sub_group_issuables_spec.rb'
-    - 'spec/features/projects/terraform_spec.rb'
-    - 'spec/features/projects/tree/create_directory_spec.rb'
-    - 'spec/features/projects/tree/create_file_spec.rb'
-    - 'spec/features/projects/work_items/work_item_children_spec.rb'
-    - 'spec/features/projects/work_items/work_item_spec.rb'
-    - 'spec/features/protected_branches_spec.rb'
-    - 'spec/features/runners_spec.rb'
     - 'spec/features/search/user_searches_for_code_spec.rb'
     - 'spec/features/search/user_searches_for_issues_spec.rb'
     - 'spec/features/search/user_searches_for_merge_requests_spec.rb'
diff --git a/spec/features/merge_request/user_interacts_with_batched_mr_diffs_spec.rb b/spec/features/merge_request/user_interacts_with_batched_mr_diffs_spec.rb
index df39fe492c1ff..50d9cf9507ded 100644
--- a/spec/features/merge_request/user_interacts_with_batched_mr_diffs_spec.rb
+++ b/spec/features/merge_request/user_interacts_with_batched_mr_diffs_spec.rb
@@ -15,13 +15,19 @@
     visit diffs_project_merge_request_path(merge_request.project, merge_request)
     wait_for_requests
 
-    click_diff_line(get_first_diff.find('[data-testid="left-side"]', match: :first))
+    within(get_first_diff) do
+      click_diff_line(find_by_testid('left-side', match: :first))
+    end
+
     page.within get_first_diff.find('.js-discussion-note-form') do
       fill_in('note_note', with: 'First Line Comment')
       click_button('Add comment now')
     end
 
-    click_diff_line(get_second_diff.find('[data-testid="left-side"]', match: :first))
+    within(get_second_diff) do
+      click_diff_line(find_by_testid('left-side', match: :first))
+    end
+
     page.within get_second_diff.find('.js-discussion-note-form') do
       fill_in('note_note', with: 'Last Line Comment')
       click_button('Add comment now')
diff --git a/spec/features/projects/show/user_sees_collaboration_links_spec.rb b/spec/features/projects/show/user_sees_collaboration_links_spec.rb
index f231b4a591ac2..fa065cf0a1021 100644
--- a/spec/features/projects/show/user_sees_collaboration_links_spec.rb
+++ b/spec/features/projects/show/user_sees_collaboration_links_spec.rb
@@ -13,7 +13,7 @@
   end
 
   def find_new_menu_toggle
-    find('[data-testid="base-dropdown-toggle"]', text: 'Create new...')
+    find_by_testid('base-dropdown-toggle', text: 'Create new...')
   end
 
   context 'with developer user' do
@@ -39,7 +39,7 @@ def find_new_menu_toggle
 
       # The dropdown above the tree
       page.within('.repo-breadcrumb') do
-        find('[data-testid="add-to-tree"]').click
+        find_by_testid('add-to-tree').click
 
         aggregate_failures 'dropdown links above the repo tree' do
           expect(page).to have_link('New file')
diff --git a/spec/features/projects/sub_group_issuables_spec.rb b/spec/features/projects/sub_group_issuables_spec.rb
index 601f88708f41e..2ea011f2f912d 100644
--- a/spec/features/projects/sub_group_issuables_spec.rb
+++ b/spec/features/projects/sub_group_issuables_spec.rb
@@ -26,7 +26,7 @@
   end
 
   def expect_to_have_breadcrumb_links
-    links = find('[data-testid="breadcrumb-links"]')
+    links = find_by_testid('breadcrumb-links')
 
     expect(links).to have_content 'group subgroup project'
   end
diff --git a/spec/features/projects/terraform_spec.rb b/spec/features/projects/terraform_spec.rb
index aefc7be7ced66..b6217d9fb8eea 100644
--- a/spec/features/projects/terraform_spec.rb
+++ b/spec/features/projects/terraform_spec.rb
@@ -63,14 +63,14 @@
 
           expect(page).to have_content(additional_state.name)
 
-          find("[data-testid='terraform-state-actions-#{additional_state.name}']").click
-          find("[data-testid='terraform-state-remove']").click
+          find_by_testid("terraform-state-actions-#{additional_state.name}").click
+          find_by_testid('terraform-state-remove').click
           fill_in "terraform-state-remove-input-#{additional_state.name}", with: additional_state.name
           click_button 'Remove'
 
           expect(page).to have_content("#{additional_state.name} successfully removed")
 
-          find("[data-testid='remove-icon']").hover
+          find_by_testid('remove-icon').hover
           expect(page).to have_content("Deletion in progress")
 
           additional_state.reload
@@ -84,7 +84,7 @@
 
           expect(page).to have_content(terraform_state.name)
 
-          page.within("[data-testid='terraform-state-actions-#{terraform_state.name}']") do
+          within_testid("terraform-state-actions-#{terraform_state.name}") do
             click_button class: 'gl-dropdown-toggle'
             click_button 'Copy Terraform init command'
           end
diff --git a/spec/features/projects/tree/create_directory_spec.rb b/spec/features/projects/tree/create_directory_spec.rb
index 8fae8f38025b9..7345082b6fe3a 100644
--- a/spec/features/projects/tree/create_directory_spec.rb
+++ b/spec/features/projects/tree/create_directory_spec.rb
@@ -54,7 +54,7 @@
     # (as it is with WEBDRIVER_HEADLESS=0), this initial commit button will exist. Otherwise, if it is
     # taller (as it is by default with chrome headless) then the button will not exist.
     if page.has_css?('[data-testid="begin-commit-button"]')
-      find('[data-testid="begin-commit-button"]').click
+      find_by_testid('begin-commit-button').click
     end
 
     fill_in('commit-message', with: 'commit message ide')
diff --git a/spec/features/projects/tree/create_file_spec.rb b/spec/features/projects/tree/create_file_spec.rb
index 2f8935b9ce36d..a3ff8de719a40 100644
--- a/spec/features/projects/tree/create_file_spec.rb
+++ b/spec/features/projects/tree/create_file_spec.rb
@@ -43,7 +43,7 @@
     # (as it is with WEBDRIVER_HEADLESS=0), this initial commit button will exist. Otherwise, if it is
     # taller (as it is by default with chrome headless) then the button will not exist.
     if page.has_css?('[data-testid="begin-commit-button"]')
-      find('[data-testid="begin-commit-button"]').click
+      find_by_testid('begin-commit-button').click
     end
 
     fill_in('commit-message', with: 'commit message ide')
diff --git a/spec/features/projects/work_items/work_item_children_spec.rb b/spec/features/projects/work_items/work_item_children_spec.rb
index 28f7ee2db10e5..71f1ceca90293 100644
--- a/spec/features/projects/work_items/work_item_children_spec.rb
+++ b/spec/features/projects/work_items/work_item_children_spec.rb
@@ -22,15 +22,15 @@
     end
 
     it 'are not displayed when issue does not have work item children', :aggregate_failures do
-      page.within('[data-testid="work-item-links"]') do
-        expect(find('[data-testid="links-empty"]')).to have_content(_('No child items are currently assigned.'))
+      within_testid('work-item-links') do
+        expect(find_by_testid('links-empty')).to have_content(_('No child items are currently assigned.'))
         expect(page).not_to have_selector('[data-testid="add-links-form"]')
         expect(page).not_to have_selector('[data-testid="links-child"]')
       end
     end
 
     it 'toggles widget body', :aggregate_failures do
-      page.within('[data-testid="work-item-links"]') do
+      within_testid('work-item-links') do
         expect(page).to have_selector('[data-testid="widget-body"]')
 
         click_button 'Collapse'
@@ -44,7 +44,7 @@
     end
 
     it 'toggles form', :aggregate_failures do
-      page.within('[data-testid="work-item-links"]') do
+      within_testid('work-item-links') do
         expect(page).not_to have_selector('[data-testid="add-links-form"]')
 
         click_button 'Add'
@@ -59,7 +59,7 @@
     end
 
     it 'adds a new child task', :aggregate_failures do
-      page.within('[data-testid="work-item-links"]') do
+      within_testid('work-item-links') do
         click_button 'Add'
         click_button 'New task'
 
@@ -72,20 +72,20 @@
 
         wait_for_all_requests
 
-        expect(find('[data-testid="links-child"]')).to have_content('Task 1')
+        expect(find_by_testid('links-child')).to have_content('Task 1')
       end
     end
 
     it 'removes a child task and undoing', :aggregate_failures do
-      page.within('[data-testid="work-item-links"]') do
+      within_testid('work-item-links') do
         click_button 'Add'
         click_button 'New task'
         fill_in 'Add a title', with: 'Task 1'
         click_button 'Create task'
         wait_for_all_requests
 
-        expect(find('[data-testid="links-child"]')).to have_content('Task 1')
-        expect(find('[data-testid="children-count"]')).to have_content('1')
+        expect(find_by_testid('links-child')).to have_content('Task 1')
+        expect(find_by_testid('children-count')).to have_content('1')
 
         find_by_testid('links-child').hover
         find_by_testid('remove-work-item-link').click
@@ -93,7 +93,7 @@
         wait_for_all_requests
 
         expect(page).not_to have_content('Task 1')
-        expect(find('[data-testid="children-count"]')).to have_content('0')
+        expect(find_by_testid('children-count')).to have_content('0')
       end
 
       page.within('.gl-toast') do
@@ -103,9 +103,9 @@
 
       wait_for_all_requests
 
-      page.within('[data-testid="work-item-links"]') do
-        expect(find('[data-testid="links-child"]')).to have_content('Task 1')
-        expect(find('[data-testid="children-count"]')).to have_content('1')
+      within_testid('work-item-links') do
+        expect(find_by_testid('links-child')).to have_content('Task 1')
+        expect(find_by_testid('children-count')).to have_content('1')
       end
     end
 
@@ -113,12 +113,12 @@
       let_it_be(:task) { create(:work_item, :task, project: project) }
 
       it 'adds an existing child task', :aggregate_failures do
-        page.within('[data-testid="work-item-links"]') do
+        within_testid('work-item-links') do
           click_button 'Add'
           click_button 'Existing task'
 
           expect(page).to have_button('Add task', disabled: true)
-          find('[data-testid="work-item-token-select-input"]').set(task.title)
+          find_by_testid('work-item-token-select-input').set(task.title)
           wait_for_all_requests
           click_button task.title
 
@@ -130,7 +130,7 @@
 
           wait_for_all_requests
 
-          expect(find('[data-testid="links-child"]')).to have_content(task.title)
+          expect(find_by_testid('links-child')).to have_content(task.title)
         end
       end
 
@@ -139,12 +139,12 @@
         let_it_be(:task) { create(:work_item, :confidential, :task, project: project) }
 
         it 'adds an existing child task', :aggregate_failures do
-          page.within('[data-testid="work-item-links"]') do
+          within_testid('work-item-links') do
             click_button 'Add'
             click_button 'Existing task'
 
             expect(page).to have_button('Add task', disabled: true)
-            find('[data-testid="work-item-token-select-input"]').set(task.title)
+            find_by_testid('work-item-token-select-input').set(task.title)
             wait_for_all_requests
             click_button task.title
 
@@ -156,7 +156,7 @@
 
             wait_for_all_requests
 
-            expect(find('[data-testid="links-child"]')).to have_content(task.title)
+            expect(find_by_testid('links-child')).to have_content(task.title)
           end
         end
       end
@@ -183,11 +183,11 @@
       end
 
       it 'displays labels, milestone and assignee for work item children', :aggregate_failures do
-        page.within('[data-testid="work-item-links"]') do
+        within_testid('work-item-links') do
           click_button 'Add'
           click_button 'Existing task'
 
-          find('[data-testid="work-item-token-select-input"]').set(task.title)
+          find_by_testid('work-item-token-select-input').set(task.title)
           wait_for_all_requests
           click_button task.title
 
@@ -198,7 +198,7 @@
           wait_for_all_requests
         end
 
-        page.within('[data-testid="links-child"]') do
+        within_testid('links-child') do
           expect(page).to have_content(task.title)
           expect(page).to have_content(label.title)
           expect(page).to have_link(user.name)
diff --git a/spec/features/projects/work_items/work_item_spec.rb b/spec/features/projects/work_items/work_item_spec.rb
index cb5214b81574f..ae7eec5c4c6d9 100644
--- a/spec/features/projects/work_items/work_item_spec.rb
+++ b/spec/features/projects/work_items/work_item_spec.rb
@@ -27,13 +27,13 @@
     end
 
     it 'shows project issues link in breadcrumbs' do
-      within('[data-testid="breadcrumb-links"]') do
+      within_testid('breadcrumb-links') do
         expect(page).to have_link('Issues', href: project_issues_path(project))
       end
     end
 
     it 'uses IID path in breadcrumbs' do
-      within('[data-testid="breadcrumb-current-link"]') do
+      within_testid('breadcrumb-current-link') do
         expect(page).to have_link("##{work_item.iid}", href: work_items_path)
       end
     end
@@ -52,14 +52,14 @@
 
       it 'reassigns to another user',
         quarantine: 'https://gitlab.com/gitlab-org/gitlab/-/issues/413074' do
-        find('[data-testid="work-item-assignees-input"]').fill_in(with: user.username)
+        find_by_testid('work-item-assignees-input').fill_in(with: user.username)
         wait_for_requests
 
         send_keys(:enter)
         find("body").click
         wait_for_requests
 
-        find('[data-testid="work-item-assignees-input"]').fill_in(with: user2.username)
+        find_by_testid('work-item-assignees-input').fill_in(with: user2.username)
         wait_for_requests
 
         send_keys(:enter)
@@ -80,7 +80,7 @@
 
       it 'reassigns to another user',
         quarantine: 'https://gitlab.com/gitlab-org/gitlab/-/issues/413074' do
-        within('[data-testid="work-item-assignees-with-edit"]') do
+        within_testid('work-item-assignees-with-edit') do
           click_button 'Edit'
         end
 
@@ -88,7 +88,7 @@
 
         wait_for_requests
 
-        within('[data-testid="work-item-assignees-with-edit"]') do
+        within_testid('work-item-assignees-with-edit') do
           click_button 'Edit'
         end
 
@@ -167,13 +167,13 @@
       end
 
       it 'disabled the assignees input field' do
-        within('[data-testid="work-item-assignees-input"]') do
+        within_testid('work-item-assignees-input') do
           expect(page).to have_field(type: 'text', disabled: true)
         end
       end
 
       it 'disables the labels input field' do
-        within('[data-testid="work-item-labels-input"]') do
+        within_testid('work-item-labels-input') do
           expect(page).to have_field(type: 'text', disabled: true)
         end
       end
@@ -188,13 +188,13 @@
       end
 
       it 'hides the assignees edit button' do
-        within('[data-testid="work-item-assignees-with-edit"]') do
+        within_testid('work-item-assignees-with-edit') do
           expect(page).not_to have_button('Edit')
         end
       end
 
       it 'hides the labels edit button' do
-        within('[data-testid="work-item-labels-with-edit"]') do
+        within_testid('work-item-labels-with-edit') do
           expect(page).not_to have_button('Edit')
         end
       end
diff --git a/spec/features/protected_branches_spec.rb b/spec/features/protected_branches_spec.rb
index 4f7f7b425fb5e..ba6e38ae82ba4 100644
--- a/spec/features/protected_branches_spec.rb
+++ b/spec/features/protected_branches_spec.rb
@@ -66,11 +66,14 @@
         expect(page).to have_content('fix')
         expect(find('.all-branches')).to have_selector('li', count: 1)
 
-        find('[data-testid="branch-more-actions"] button').click
+        within_testid('branch-more-actions') do
+          find('button').click
+        end
+
         wait_for_requests
         expect(page).to have_button('Delete protected branch', disabled: false)
 
-        find('[data-testid="delete-branch-button"]').click
+        find_by_testid('delete-branch-button').click
         fill_in 'delete_branch_input', with: 'fix'
         click_button 'Yes, delete protected branch'
 
diff --git a/spec/features/runners_spec.rb b/spec/features/runners_spec.rb
index eb61d1afa354b..3e1ae36a9159b 100644
--- a/spec/features/runners_spec.rb
+++ b/spec/features/runners_spec.rb
@@ -53,7 +53,7 @@
       it 'user sees the project runner' do
         visit project_runners_path(project)
 
-        within '[data-testid="assigned_project_runners"]' do
+        within_testid 'assigned_project_runners' do
           expect(page).to have_content(project_runner.display_name)
         end
 
@@ -65,19 +65,19 @@
       it 'user can pause and resume the project runner' do
         visit project_runners_path(project)
 
-        within '[data-testid="assigned_project_runners"]' do
+        within_testid 'assigned_project_runners' do
           expect(page).to have_link('Pause')
         end
 
         click_on 'Pause'
 
-        within '[data-testid="assigned_project_runners"]' do
+        within_testid 'assigned_project_runners' do
           expect(page).to have_link('Resume')
         end
 
         click_on 'Resume'
 
-        within '[data-testid="assigned_project_runners"]' do
+        within_testid 'assigned_project_runners' do
           expect(page).to have_link('Pause')
         end
       end
@@ -85,7 +85,7 @@
       it 'user removes an activated project runner if this is last project for that runners' do
         visit project_runners_path(project)
 
-        within '[data-testid="assigned_project_runners"]' do
+        within_testid 'assigned_project_runners' do
           click_on 'Remove runner'
         end
 
@@ -100,7 +100,7 @@
         it 'user edits the runner to be protected' do
           visit project_runners_path(project)
 
-          within '[data-testid="assigned_project_runners"]' do
+          within_testid 'assigned_project_runners' do
             first('[data-testid="edit-runner-link"]').click
           end
 
@@ -120,7 +120,7 @@
           it 'user edits runner not to run untagged jobs' do
             visit project_runners_path(project)
 
-            within '[data-testid="assigned_project_runners"]' do
+            within_testid 'assigned_project_runners' do
               first('[data-testid="edit-runner-link"]').click
             end
 
@@ -140,7 +140,7 @@
         it 'user sees CI/CD setting page' do
           visit project_runners_path(project)
 
-          within '[data-testid="available-shared-runners"]' do
+          within_testid 'available-shared-runners' do
             expect(page).to have_content(shared_runner.display_name)
           end
         end
@@ -151,7 +151,7 @@
           it 'shows the runner count' do
             visit project_runners_path(project)
 
-            within '[data-testid="available-shared-runners"]' do
+            within_testid 'available-shared-runners' do
               expect(page).to have_content format(_('Available instance runners: %{count}'), { count: 2 })
             end
           end
@@ -161,7 +161,7 @@
 
             visit project_runners_path(project)
 
-            within '[data-testid="available-shared-runners"]' do
+            within_testid 'available-shared-runners' do
               expect(find('.pagination')).not_to be_nil
             end
           end
@@ -192,17 +192,17 @@
       it 'user enables and disables a project runner' do
         visit project_runners_path(project)
 
-        within '[data-testid="available_project_runners"]' do
+        within_testid 'available_project_runners' do
           click_on 'Enable for this project'
         end
 
-        expect(page.find('[data-testid="assigned_project_runners"]')).to have_content(project_runner.display_name)
+        expect(find_by_testid('assigned_project_runners')).to have_content(project_runner.display_name)
 
-        within '[data-testid="assigned_project_runners"]' do
+        within_testid 'assigned_project_runners' do
           click_on 'Disable for this project'
         end
 
-        expect(page.find('[data-testid="available_project_runners"]')).to have_content(project_runner.display_name)
+        expect(find_by_testid('available_project_runners')).to have_content(project_runner.display_name)
       end
     end
 
@@ -218,7 +218,7 @@
         it 'user sees shared runners description' do
           visit project_runners_path(project)
 
-          page.within("[data-testid='shared-runners-description']") do
+          within_testid('shared-runners-description') do
             expect(page).not_to have_content('The same shared runner executes code from multiple projects')
             expect(page).to have_content(shared_runners_html)
           end
@@ -235,7 +235,7 @@
         it 'user sees no link' do
           visit project_runners_path(project)
 
-          page.within("[data-testid='shared-runners-description']") do
+          within_testid('shared-runners-description') do
             expect(page).to have_content('link')
             expect(page).not_to have_link('link')
           end
@@ -252,7 +252,7 @@
         it 'user sees image safely' do
           visit project_runners_path(project)
 
-          page.within("[data-testid='shared-runners-description']") do
+          within_testid('shared-runners-description') do
             expect(page).to have_css('img')
             expect(page).not_to have_css('img[onerror]')
           end
@@ -384,7 +384,7 @@
             it 'shows the runner count' do
               visit project_runners_path(project)
 
-              within '[data-testid="group-runners"]' do
+              within_testid 'group-runners' do
                 expect(page).to have_content format(_('Available group runners: %{runners}'), { runners: 2 })
               end
             end
@@ -394,7 +394,7 @@
 
               visit project_runners_path(project)
 
-              within '[data-testid="group-runners"]' do
+              within_testid 'group-runners' do
                 expect(find('.pagination')).not_to be_nil
               end
             end
-- 
GitLab