From bc0ce6be3f1fb9bc2ca8de8b3105467e17fc2d27 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Mon, 23 Oct 2023 22:48:08 +0200 Subject: [PATCH] Add specs for comment and modal controller Turbo Frame actions --- app/controllers/modal_controller.rb | 2 +- spec/controllers/comment_controller_spec.rb | 29 +++++++++++++++++++++ spec/controllers/modal_controller_spec.rb | 29 +++++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 spec/controllers/modal_controller_spec.rb diff --git a/app/controllers/modal_controller.rb b/app/controllers/modal_controller.rb index 16ae890e..a8d61d60 100644 --- a/app/controllers/modal_controller.rb +++ b/app/controllers/modal_controller.rb @@ -7,7 +7,7 @@ class ModalController < ApplicationController skip_before_action :find_active_announcements, :banned? def close - redirect_to root_path unless turbo_frame_request? + return redirect_to root_path unless turbo_frame_request? render inline: turbo_frame_tag("modal") end end diff --git a/spec/controllers/comment_controller_spec.rb b/spec/controllers/comment_controller_spec.rb index ace2aad8..63812485 100644 --- a/spec/controllers/comment_controller_spec.rb +++ b/spec/controllers/comment_controller_spec.rb @@ -34,4 +34,33 @@ describe CommentController, type: :controller do end end end + + describe "#show_reactions" do + let(:answer_author) { FactoryBot.create(:user) } + let(:answer) { FactoryBot.create(:answer, user: answer_author) } + let(:commenter) { FactoryBot.create(:user) } + let(:comment) { FactoryBot.create(:comment, answer:, user: commenter) } + + context "a regular web navigation request" do + subject { get :show_reactions, params: { username: commenter.screen_name, id: comment.id } } + + it "should redirect to the answer page" do + subject + + expect(response).to redirect_to answer_path(username: answer_author.screen_name, id: answer.id) + end + end + + context "a Turbo Frame request" do + subject { get :show_reactions, params: { username: commenter.screen_name, id: comment.id } } + + it "renders the show_reaction template" do + @request.headers["Turbo-Frame"] = "some_id" + + subject + + expect(response).to render_template(:show_reactions) + end + end + end end diff --git a/spec/controllers/modal_controller_spec.rb b/spec/controllers/modal_controller_spec.rb new file mode 100644 index 00000000..d927173a --- /dev/null +++ b/spec/controllers/modal_controller_spec.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +require "rails_helper" + +describe ModalController, type: :controller do + describe "#close" do + context "a regular web navigation request" do + subject { get :close } + + it "should redirect to the root page" do + subject + + expect(response).to redirect_to root_path + end + end + + context "a Turbo Frame request" do + subject { get :close } + + it "renders the show_reaction template" do + @request.headers["Turbo-Frame"] = "some_id" + + subject + + expect(response.body).to include('') + end + end + end +end