2020-04-30 14:16:27 -07:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "rails_helper"
|
|
|
|
|
2023-10-24 18:10:24 -07:00
|
|
|
describe SubscriptionsController, type: :controller do
|
2020-04-30 14:16:27 -07:00
|
|
|
# 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) }
|
2023-10-24 18:10:24 -07:00
|
|
|
let(:user) { FactoryBot.create(:user) }
|
2020-04-30 14:16:27 -07:00
|
|
|
|
2023-10-24 18:10:24 -07:00
|
|
|
describe "#create" do
|
2020-04-30 14:16:27 -07:00
|
|
|
let(:params) do
|
|
|
|
{
|
2023-10-24 18:10:24 -07:00
|
|
|
answer: answer_id,
|
2020-04-30 14:16:27 -07:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2023-10-24 18:10:24 -07:00
|
|
|
subject { post(:create, params:, format: :turbo_stream) }
|
2020-04-30 14:16:27 -07:00
|
|
|
|
|
|
|
context "when user is signed in" do
|
|
|
|
before(:each) { sign_in(user) }
|
|
|
|
|
|
|
|
context "when answer exists" do
|
|
|
|
let(:answer_id) { answer.id }
|
|
|
|
|
|
|
|
context "when subscription does not exist" do
|
|
|
|
it "creates a subscription on the answer" do
|
|
|
|
expect { subject }.to(change { answer.subscriptions.count }.by(1))
|
2023-03-04 11:42:51 -08:00
|
|
|
expect(answer.subscriptions.map { |s| s.user.id }.sort).to eq([answer_user.id, user.id].sort)
|
2020-04-30 14:16:27 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when subscription already exists" do
|
|
|
|
before(:each) { Subscription.subscribe(user, answer) }
|
|
|
|
|
|
|
|
it "does not modify the answer's subscriptions" do
|
|
|
|
expect { subject }.to(change { answer.subscriptions.count }.by(0))
|
2023-03-04 11:42:51 -08:00
|
|
|
expect(answer.subscriptions.map { |s| s.user.id }.sort).to eq([answer_user.id, user.id].sort)
|
2020-04-30 14:16:27 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when answer does not exist" do
|
|
|
|
let(:answer_id) { "Bielefeld" }
|
|
|
|
|
|
|
|
it "does not create a new subscription" do
|
|
|
|
expect { subject }.not_to(change { Subscription.count })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when user is not signed in" do
|
|
|
|
let(:answer_id) { answer.id }
|
|
|
|
|
|
|
|
it "redirects to somewhere else, apparently" do
|
|
|
|
subject
|
|
|
|
expect(response).to be_a_redirect
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-10-24 18:10:24 -07:00
|
|
|
describe "#destroy" do
|
2020-04-30 14:16:27 -07:00
|
|
|
let(:params) do
|
|
|
|
{
|
2023-10-24 18:10:24 -07:00
|
|
|
answer: answer_id,
|
2020-04-30 14:16:27 -07:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2023-10-24 18:10:24 -07:00
|
|
|
subject { delete(:destroy, params:, format: :turbo_stream) }
|
2020-04-30 14:16:27 -07:00
|
|
|
|
|
|
|
context "when user is signed in" do
|
|
|
|
before(:each) { sign_in(user) }
|
|
|
|
|
|
|
|
context "when answer exists" do
|
|
|
|
let(:answer_id) { answer.id }
|
|
|
|
|
|
|
|
context "when subscription exists" do
|
|
|
|
before(:each) { Subscription.subscribe(user, answer) }
|
|
|
|
|
|
|
|
it "removes an active subscription from the answer" do
|
2023-03-04 11:42:51 -08:00
|
|
|
expect { subject }.to(change { answer.subscriptions.count }.by(-1))
|
|
|
|
expect(answer.subscriptions.map { |s| s.user.id }.sort).to eq([answer_user.id].sort)
|
2020-04-30 14:16:27 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when subscription does not exist" do
|
|
|
|
it "does not modify the answer's subscriptions" do
|
|
|
|
expect { subject }.to(change { answer.subscriptions.count }.by(0))
|
2023-03-04 11:42:51 -08:00
|
|
|
expect(answer.subscriptions.map { |s| s.user.id }.sort).to eq([answer_user.id].sort)
|
2020-04-30 14:16:27 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when answer does not exist" do
|
|
|
|
let(:answer_id) { "Bielefeld" }
|
|
|
|
|
|
|
|
it "does not create a new subscription" do
|
|
|
|
expect { subject }.not_to(change { Subscription.count })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when user is not signed in" do
|
|
|
|
let(:answer_id) { answer.id }
|
|
|
|
|
|
|
|
it "redirects to somewhere else, apparently" do
|
|
|
|
subject
|
|
|
|
expect(response).to be_a_redirect
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|