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.
This commit is contained in:
Javi Martín
2025-08-26 16:31:23 +02:00
parent 1e06e676a4
commit 150af75e3e

View File

@@ -5,8 +5,8 @@ describe Dashboard::ResourcesComponent do
before { sign_in(proposal.author) } before { sign_in(proposal.author) }
describe "Available resources section" do describe "Available resources section" do
let!(:available) { create(:dashboard_action, :resource, :active) } let!(:available) { create(:dashboard_action, :resource, :active, title: "Available!") }
let(:requested) { create(:dashboard_action, :resource, :admin_request, :active) } let(:requested) { create(:dashboard_action, :resource, :admin_request, :active, title: "Requested!") }
let(:executed_action) do let(:executed_action) do
create( create(
:dashboard_executed_action, :dashboard_executed_action,
@@ -15,7 +15,7 @@ describe Dashboard::ResourcesComponent do
executed_at: Time.current executed_at: Time.current
) )
end 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 let(:executed_solved_action) do
create( create(
:dashboard_executed_action, :dashboard_executed_action,
@@ -29,7 +29,8 @@ describe Dashboard::ResourcesComponent do
:dashboard_action, :dashboard_action,
:resource, :resource,
:active, :active,
required_supports: proposal.votes_for.size + 1_000 required_supports: proposal.votes_for.size + 1_000,
title: "Unavailable!"
) )
end end
@@ -49,19 +50,19 @@ describe Dashboard::ResourcesComponent do
expect(page).to have_content requested.title expect(page).to have_content requested.title
expect(page).to have_content solved.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" expect(dashboard_action_available).to have_link "See resource"
end 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" expect(dashboard_action_requested).to have_content "Resource already requested"
end 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" expect(dashboard_action_unavailable).to have_content "1.000 supports required"
end 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" expect(dashboard_action_solved).to have_link "See resource"
end end
end end
@@ -79,19 +80,19 @@ describe Dashboard::ResourcesComponent do
expect(page).to have_content requested.title expect(page).to have_content requested.title
expect(page).to have_content solved.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" expect(dashboard_action_available).to have_link "See resource"
end 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" expect(dashboard_action_requested).to have_content "Resource already requested"
end 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" expect(dashboard_action_unavailable).to have_content "1.000 supports required"
end 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" expect(dashboard_action_solved).to have_link "See resource"
end end
end end
@@ -112,17 +113,17 @@ describe Dashboard::ResourcesComponent do
describe "Tags for new actions" do describe "Tags for new actions" do
it "displays tag 'new' only when resource is detected like new action" do it "displays tag 'new' only when resource is detected like new action" do
new_resource = create(:dashboard_action, :resource, :active) create(:dashboard_action, :resource, :active, title: "Old!")
old_resource = create(:dashboard_action, :resource, :active) new_resource = create(:dashboard_action, :resource, :active, title: "Recent!")
new_actions_since_last_login = [new_resource.id] new_actions_since_last_login = [new_resource.id]
render_inline Dashboard::ResourcesComponent.new(proposal, new_actions_since_last_login) 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" expect(dashboard_action_new_resource).to have_content "New"
end 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" expect(dashboard_action_old_resource).not_to have_content "New"
end end
end end