Files
danbooru/test/components/post_votes_component_test.rb
evazion d0c9f6e0b8 posts: allow toggling between upvotes and downvotes.
Like 9efb374ae, allow users to toggle between upvoting and downvoting a
post without raising an error or having to manually remove the vote
first. If you upvote a post, then downvote it, the upvote is
automatically removed and replaced by the downvote.

Other changes:

* Tagging a post with `upvote:self` or `downvote:self` is now silently
  ignored when the user doesn't have permission to vote, instead of
  raising an error.
* Undoing a vote that doesn't exist now does nothing instead of
  returning an error. This can happen if you open the same post in two
  tabs, undo the vote in tab 1, then try to undo the vote again in tab 2.

Changes to the /post_votes API:

* `POST /post_votes` and `DELETE /post_votes` now return a post vote
  instead of a post.
* The `score` param in `POST /post_votes` is now 1 or -1, not `up` or
  `down`.
2021-01-29 02:22:23 -06:00

57 lines
1.6 KiB
Ruby

require "test_helper"
class PostVotesComponentTest < ViewComponent::TestCase
def render_post_votes(post, current_user:)
render_inline(PostVotesComponent.new(post: post, current_user: current_user))
end
context "The PostVotesComponent" do
setup do
@post = as(create(:user)) { create(:post) }
end
context "for a user who can't vote" do
should "not show the vote buttons" do
render_post_votes(@post, current_user: User.anonymous)
assert_css(".post-score")
assert_no_css(".post-upvote-link")
assert_no_css(".post-downvote-link")
end
end
context "for a user who can vote" do
setup do
@user = create(:gold_user)
end
should "show the vote buttons" do
render_post_votes(@post, current_user: @user)
assert_css(".post-upvote-link.inactive-link")
assert_css(".post-downvote-link.inactive-link")
end
context "for a downvoted post" do
should "highlight the downvote button as active" do
@post.vote!(-1, @user)
render_post_votes(@post, current_user: @user)
assert_css(".post-upvote-link.inactive-link")
assert_css(".post-downvote-link.active-link")
end
end
context "for an upvoted post" do
should "highlight the upvote button as active" do
@post.vote!(1, @user)
render_post_votes(@post, current_user: @user)
assert_css(".post-upvote-link.active-link")
assert_css(".post-downvote-link.inactive-link")
end
end
end
end
end