From 6e6cf5358bc11ebbcf9b4a1440ee53fb258c8431 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Wed, 25 Oct 2023 03:10:24 +0200 Subject: [PATCH] Add specs for `SubscriptionsController` These are mostly the old `AjaxController::SubscriptionController` specs, but adjusted for Turbo (and the proper action names) --- ...ec.rb => subscriptions_controller_spec.rb} | 63 +++---------------- 1 file changed, 8 insertions(+), 55 deletions(-) rename spec/controllers/{ajax/subscription_controller_spec.rb => subscriptions_controller_spec.rb} (66%) diff --git a/spec/controllers/ajax/subscription_controller_spec.rb b/spec/controllers/subscriptions_controller_spec.rb similarity index 66% rename from spec/controllers/ajax/subscription_controller_spec.rb rename to spec/controllers/subscriptions_controller_spec.rb index 23499211..6b9951d4 100644 --- a/spec/controllers/ajax/subscription_controller_spec.rb +++ b/spec/controllers/subscriptions_controller_spec.rb @@ -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