From c3d14c6786553c33eca72e7057d22ed116ece7b3 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Thu, 25 Jan 2024 20:43:59 +0100 Subject: [PATCH] Add specs for `Moderation::ReportsController` --- .../moderation/reports_controller_spec.rb | 113 +++++++++++++++++- 1 file changed, 107 insertions(+), 6 deletions(-) diff --git a/spec/controllers/moderation/reports_controller_spec.rb b/spec/controllers/moderation/reports_controller_spec.rb index 0a845606..daed0ab8 100644 --- a/spec/controllers/moderation/reports_controller_spec.rb +++ b/spec/controllers/moderation/reports_controller_spec.rb @@ -6,15 +6,116 @@ describe Moderation::ReportsController, type: :controller do let(:user) { FactoryBot.create :user, roles: ["moderator"] } describe "#index" do - subject { get :index } + shared_examples_for "sets the expected ivars" do + let(:expected_assigns) { {} } - before do - sign_in user + it "sets the expected ivars" do + subject + + expected_assigns.each do |name, value| + expect(assigns[name]).to eq(value) + end + end end - it "renders the moderation/reports/index template" do - subject - expect(response).to render_template("moderation/reports/index") + context "template rendering" do + let(:other_user) { FactoryBot.create :user } + let(:report) { Report.create(user:, target_id: other_user.id, type: "Reports::User") } + + subject { get :index } + + before do + report + sign_in user + end + + it "renders the moderation/reports/index template" do + subject + expect(response).to render_template("moderation/reports/index") + end + + include_examples "sets the expected ivars" do + let(:expected_assigns) do + { + reports: [report], + reports_last_id: report.id, + } + end + end + end + + context "filtering for target users" do + let(:other_user) { FactoryBot.create :user } + let(:question) { FactoryBot.create :question } + let(:report) { Report.create(user:, target_id: other_user.id, target_user_id: other_user.id, type: "Reports::User") } + let(:report2) { Report.create(user:, target_id: question.id, target_user_id: nil, type: "Reports::Question") } + + subject { get :index, params: { target_user: other_user.screen_name } } + + before do + report + report2 + sign_in user + end + + include_examples "sets the expected ivars" do + let(:expected_assigns) do + { + reports: [report], + reports_last_id: report.id, + } + end + end + end + + context "filtering for users" do + let(:report_user) { FactoryBot.create :user } + let(:other_user) { FactoryBot.create :user } + let(:question) { FactoryBot.create :question } + let(:report) { Report.create(user:, target_id: other_user.id, target_user_id: other_user.id, type: "Reports::User") } + let(:report2) { Report.create(user: report_user, target_id: question.id, target_user_id: nil, type: "Reports::Question") } + + subject { get :index, params: { user: report_user.screen_name } } + + before do + report + report2 + sign_in user + end + + include_examples "sets the expected ivars" do + let(:expected_assigns) do + { + reports: [report2], + reports_last_id: report2.id, + } + end + end + end + + context "filtering for type" do + let(:report_user) { FactoryBot.create :user } + let(:other_user) { FactoryBot.create :user } + let(:question) { FactoryBot.create :question } + let(:report) { Report.create(user:, target_id: other_user.id, target_user_id: other_user.id, type: "Reports::User") } + let(:report2) { Report.create(user: report_user, target_id: question.id, target_user_id: nil, type: "Reports::Question") } + + subject { get :index, params: { type: "question" } } + + before do + report + report2 + sign_in user + end + + include_examples "sets the expected ivars" do + let(:expected_assigns) do + { + reports: [report2], + reports_last_id: report2.id, + } + end + end end end end