Allow liking/unliking when JavaScript is disabled

Even if pretty much nobody uses a browser with JavaScript disabled when
navigating our sites, there might be times where JavaScript isn't loaded
for reasons like a slow internet connections not getting the JavaScript
files or a technical issue.

So we're making it possible to still use the like/unlike buttons in
these cases.
This commit is contained in:
Javi Martín
2023-10-11 14:11:27 +02:00
parent 38e81f2858
commit 7070b0915b
7 changed files with 81 additions and 1 deletions

View File

@@ -1,7 +1,7 @@
require "rails_helper"
describe Comments::VotesController do
let(:comment) { create(:comment) }
let(:comment) { create(:debate_comment) }
describe "POST create" do
it "allows voting" do
@@ -11,6 +11,18 @@ describe Comments::VotesController do
post :create, xhr: true, params: { comment_id: comment.id, value: "yes" }
end.to change { comment.reload.votes_for.size }.by(1)
end
it "redirects authenticated users without JavaScript to the same page" do
request.env["HTTP_REFERER"] = comment_path(comment)
sign_in create(:user)
expect do
post :create, params: { comment_id: comment.id, value: "yes" }
end.to change { comment.reload.votes_for.size }.by(1)
expect(response).to redirect_to comment_path(comment)
expect(flash[:notice]).to eq "Vote created successfully"
end
end
describe "DELETE destroy" do
@@ -30,5 +42,17 @@ describe Comments::VotesController do
delete :destroy, xhr: true, params: { comment_id: comment.id, id: vote }
end.to change { comment.reload.votes_for.size }.by(-1)
end
it "redirects authenticated users without JavaScript to the same page" do
request.env["HTTP_REFERER"] = debate_path(comment.commentable)
sign_in user
expect do
delete :destroy, params: { comment_id: comment.id, id: vote }
end.to change { comment.reload.votes_for.size }.by(-1)
expect(response).to redirect_to debate_path(comment.commentable)
expect(flash[:notice]).to eq "Vote deleted successfully"
end
end
end