Add specs for `SubscriptionsController`

These are mostly the old `AjaxController::SubscriptionController` specs, but adjusted for Turbo (and the proper action names)
This commit is contained in:
Andreas Nedbal 2023-10-25 03:10:24 +02:00
parent 7fdf978be1
commit 6e6cf5358b
1 changed files with 8 additions and 55 deletions

View File

@ -2,41 +2,33 @@
require "rails_helper"
describe Ajax::SubscriptionController, :ajax_controller, type: :controller do
describe SubscriptionsController, type: :controller do
# need to use a different user here, as after a create the user owning the
# answer is automatically subscribed to it
let(:answer_user) { FactoryBot.create(:user) }
let(:answer) { FactoryBot.create(:answer, user: answer_user) }
let(:user) { FactoryBot.create(:user) }
describe "#subscribe" do
describe "#create" do
let(:params) do
{
answer: answer_id
answer: answer_id,
}
end
subject { post(:subscribe, params: params) }
subject { post(:create, params:, format: :turbo_stream) }
context "when user is signed in" do
before(:each) { sign_in(user) }
context "when answer exists" do
let(:answer_id) { answer.id }
let(:expected_response) do
{
"success" => true,
"status" => "okay",
"message" => anything
}
end
context "when subscription does not exist" do
it "creates a subscription on the answer" do
expect { subject }.to(change { answer.subscriptions.count }.by(1))
expect(answer.subscriptions.map { |s| s.user.id }.sort).to eq([answer_user.id, user.id].sort)
end
include_examples "returns the expected response"
end
context "when subscription already exists" do
@ -46,26 +38,15 @@ describe Ajax::SubscriptionController, :ajax_controller, type: :controller do
expect { subject }.to(change { answer.subscriptions.count }.by(0))
expect(answer.subscriptions.map { |s| s.user.id }.sort).to eq([answer_user.id, user.id].sort)
end
include_examples "returns the expected response"
end
end
context "when answer does not exist" do
let(:answer_id) { "Bielefeld" }
let(:expected_response) do
{
"success" => false,
"status" => "not_found",
"message" => anything
}
end
it "does not create a new subscription" do
expect { subject }.not_to(change { Subscription.count })
end
include_examples "returns the expected response"
end
end
@ -79,27 +60,20 @@ describe Ajax::SubscriptionController, :ajax_controller, type: :controller do
end
end
describe "#unsubscribe" do
describe "#destroy" do
let(:params) do
{
answer: answer_id
answer: answer_id,
}
end
subject { post(:unsubscribe, params: params) }
subject { delete(:destroy, params:, format: :turbo_stream) }
context "when user is signed in" do
before(:each) { sign_in(user) }
context "when answer exists" do
let(:answer_id) { answer.id }
let(:expected_response) do
{
"success" => true,
"status" => "okay",
"message" => anything
}
end
context "when subscription exists" do
before(:each) { Subscription.subscribe(user, answer) }
@ -108,43 +82,22 @@ describe Ajax::SubscriptionController, :ajax_controller, type: :controller do
expect { subject }.to(change { answer.subscriptions.count }.by(-1))
expect(answer.subscriptions.map { |s| s.user.id }.sort).to eq([answer_user.id].sort)
end
include_examples "returns the expected response"
end
context "when subscription does not exist" do
let(:expected_response) do
{
"success" => false,
"status" => "okay",
"message" => anything
}
end
it "does not modify the answer's subscriptions" do
expect { subject }.to(change { answer.subscriptions.count }.by(0))
expect(answer.subscriptions.map { |s| s.user.id }.sort).to eq([answer_user.id].sort)
end
include_examples "returns the expected response"
end
end
context "when answer does not exist" do
let(:answer_id) { "Bielefeld" }
let(:expected_response) do
{
"success" => false,
"status" => "not_found",
"message" => anything
}
end
it "does not create a new subscription" do
expect { subject }.not_to(change { Subscription.count })
end
include_examples "returns the expected response"
end
end