From 150af75e3e93e04435e4ddeb5b9bdb434a2d9d12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Tue, 26 Aug 2025 16:31:23 +0200 Subject: [PATCH] Make resources component tests more readable We're testing things from the user's point of view by finding elements given their texts, instead of checking for elements with a certain ID. --- .../dashboard/resources_component_spec.rb | 33 ++++++++++--------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/spec/components/dashboard/resources_component_spec.rb b/spec/components/dashboard/resources_component_spec.rb index 46d261299..00978ecb4 100644 --- a/spec/components/dashboard/resources_component_spec.rb +++ b/spec/components/dashboard/resources_component_spec.rb @@ -5,8 +5,8 @@ describe Dashboard::ResourcesComponent do before { sign_in(proposal.author) } describe "Available resources section" do - let!(:available) { create(:dashboard_action, :resource, :active) } - let(:requested) { create(:dashboard_action, :resource, :admin_request, :active) } + let!(:available) { create(:dashboard_action, :resource, :active, title: "Available!") } + let(:requested) { create(:dashboard_action, :resource, :admin_request, :active, title: "Requested!") } let(:executed_action) do create( :dashboard_executed_action, @@ -15,7 +15,7 @@ describe Dashboard::ResourcesComponent do executed_at: Time.current ) end - let(:solved) { create(:dashboard_action, :resource, :admin_request, :active) } + let(:solved) { create(:dashboard_action, :resource, :admin_request, :active, title: "Solved!") } let(:executed_solved_action) do create( :dashboard_executed_action, @@ -29,7 +29,8 @@ describe Dashboard::ResourcesComponent do :dashboard_action, :resource, :active, - required_supports: proposal.votes_for.size + 1_000 + required_supports: proposal.votes_for.size + 1_000, + title: "Unavailable!" ) end @@ -49,19 +50,19 @@ describe Dashboard::ResourcesComponent do expect(page).to have_content requested.title expect(page).to have_content solved.title - page.find("div#dashboard_action_#{available.id}") do |dashboard_action_available| + page.find(".resource-card", text: "Available!") do |dashboard_action_available| expect(dashboard_action_available).to have_link "See resource" end - page.find("div#dashboard_action_#{requested.id}") do |dashboard_action_requested| + page.find(".resource-card", text: "Requested!") do |dashboard_action_requested| expect(dashboard_action_requested).to have_content "Resource already requested" end - page.find("div#dashboard_action_#{unavailable.id}") do |dashboard_action_unavailable| + page.find(".resource-card", text: "Unavailable!") do |dashboard_action_unavailable| expect(dashboard_action_unavailable).to have_content "1.000 supports required" end - page.find("div#dashboard_action_#{solved.id}") do |dashboard_action_solved| + page.find(".resource-card", text: "Solved!") do |dashboard_action_solved| expect(dashboard_action_solved).to have_link "See resource" end end @@ -79,19 +80,19 @@ describe Dashboard::ResourcesComponent do expect(page).to have_content requested.title expect(page).to have_content solved.title - page.find("div#dashboard_action_#{available.id}") do |dashboard_action_available| + page.find(".resource-card", text: "Available!") do |dashboard_action_available| expect(dashboard_action_available).to have_link "See resource" end - page.find("div#dashboard_action_#{requested.id}") do |dashboard_action_requested| + page.find(".resource-card", text: "Requested!") do |dashboard_action_requested| expect(dashboard_action_requested).to have_content "Resource already requested" end - page.find("div#dashboard_action_#{unavailable.id}") do |dashboard_action_unavailable| + page.find(".resource-card", text: "Unavailable!") do |dashboard_action_unavailable| expect(dashboard_action_unavailable).to have_content "1.000 supports required" end - page.find("div#dashboard_action_#{solved.id}") do |dashboard_action_solved| + page.find(".resource-card", text: "Solved!") do |dashboard_action_solved| expect(dashboard_action_solved).to have_link "See resource" end end @@ -112,17 +113,17 @@ describe Dashboard::ResourcesComponent do describe "Tags for new actions" do it "displays tag 'new' only when resource is detected like new action" do - new_resource = create(:dashboard_action, :resource, :active) - old_resource = create(:dashboard_action, :resource, :active) + create(:dashboard_action, :resource, :active, title: "Old!") + new_resource = create(:dashboard_action, :resource, :active, title: "Recent!") new_actions_since_last_login = [new_resource.id] render_inline Dashboard::ResourcesComponent.new(proposal, new_actions_since_last_login) - page.find("div#dashboard_action_#{new_resource.id}") do |dashboard_action_new_resource| + page.find(".resource-card", text: "Recent!") do |dashboard_action_new_resource| expect(dashboard_action_new_resource).to have_content "New" end - page.find("div#dashboard_action_#{old_resource.id}") do |dashboard_action_old_resource| + page.find(".resource-card", text: "Old!") do |dashboard_action_old_resource| expect(dashboard_action_old_resource).not_to have_content "New" end end