Files
nairobi/spec/components/shared/in_favor_against_component_spec.rb
taitus fd5fa2da79 Refactoring: Move 'vote' action to Votes Controllers
As far as possible I think the code is clearer if we use CRUD actions
rather than custom actions. This will make it easier to add the action
to remove votes in the next commit.

Note that we are adding this line as we need to validate it that a vote
can be created on a debate by the current user:

```authorize! :create, Vote.new(voter: current_user, votable: @debate)```

We have done it this way and not with the following code as you might
expect, as this way two votes are created instead of one.

```load_and_authorize_resource through: :debate, through_association: :votes_for```

This line tries to load the resource @debate and through the association
"votes_for" it tries to create a new vote associated to that debate.
Therefore a vote is created when trying to authorise the resource and
then another one in the create action, when calling @debate.vote_by (which
is called by @debate.register_vote).
2023-10-09 07:21:49 +02:00

71 lines
2.2 KiB
Ruby

require "rails_helper"
describe Shared::InFavorAgainstComponent do
let(:debate) { create(:debate) }
let(:component) { Shared::InFavorAgainstComponent.new(debate) }
let(:user) { create(:user) }
describe "Agree and disagree buttons" do
it "can create a vote when the user has not yet voted" do
sign_in user
render_inline component
page.find(".in-favor") do |in_favor_block|
expect(in_favor_block).to have_css "form[action*='votes'][method='post']"
expect(in_favor_block).not_to have_css "input[name='_method']", visible: :all
end
page.find(".against") do |against_block|
expect(against_block).to have_css "form[action*='votes'][method='post']"
expect(against_block).not_to have_css "input[name='_method']", visible: :all
end
end
it "does not include result percentages" do
create(:vote, votable: debate)
sign_in(user)
render_inline component
expect(page).to have_button count: 2
expect(page).to have_button "I agree"
expect(page).to have_button "I disagree"
expect(page).not_to have_button text: "%"
expect(page).not_to have_button text: "100"
expect(page).not_to have_button text: "0"
end
describe "aria-pressed attribute" do
it "is true when the in-favor button is pressed" do
debate.register_vote(user, "yes")
sign_in(user)
render_inline component
expect(page.find(".in-favor")).to have_css "button[aria-pressed='true']"
expect(page.find(".against")).to have_css "button[aria-pressed='false']"
end
it "is true when the against button is pressed" do
debate.register_vote(user, "no")
sign_in(user)
render_inline component
expect(page.find(".in-favor")).to have_css "button[aria-pressed='false']"
expect(page.find(".against")).to have_css "button[aria-pressed='true']"
end
it "is false when neither the 'in-favor' button nor the 'against' button are pressed" do
sign_in(user)
render_inline component
expect(page.find(".in-favor")).to have_css "button[aria-pressed='false']"
expect(page.find(".against")).to have_css "button[aria-pressed='false']"
end
end
end
end