Files
growstuff/spec/controllers/comments_controller_spec.rb
T
51feec8de1 Add comment support for plantings (#4984)
* Add comment support for plantings

- Add polymorphic comments association, subject, and author methods to Planting.
- Add comments_count column migration for Planting counter cache.
- Update CommentsController and comments view templates to support Planting.
- Render comments list and form on Planting show view.
- Add specs for Planting comments in models, controllers, and requests.

Co-authored-by: CloCkWeRX <365751+CloCkWeRX@users.noreply.github.com>

* put 4.0.7 back

* Opt comments on posts into record-page styling temporarily

* Drop hr

* Flatten comments

* Update schema

---------

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
Co-authored-by: Brenda Wallace <brenda@wallace.net.nz>
2026-10-22 15:22:32 +00:00

168 lines
4.5 KiB
Ruby

# frozen_string_literal: true
require 'rails_helper'
describe CommentsController do
subject { response }
let(:member) { create(:member) }
before do
sign_in member
controller.stub(:current_member) { member }
end
def valid_attributes
@post = create(:post)
{ post_id: @post.id, body: "some text" }
end
describe "GET RSS feed" do
let!(:first_comment) { create(:comment, created_at: 10.days.ago) }
let!(:last_comment) { create(:comment, created_at: 4.minutes.ago) }
describe "returns an RSS feed" do
before { get :index, format: "rss" }
it { is_expected.to be_successful }
it { is_expected.to render_template("comments/index") }
it { expect(response.content_type).to eq("application/rss+xml; charset=utf-8") }
it { expect(assigns(:comments)).to eq([last_comment, first_comment]) }
end
end
describe "GET new" do
let(:post) { create(:post) }
let(:planting) { create(:planting) }
describe "with valid post params" do
before do
get :new, params: {
comment: { commentable_id: post.id, commentable_type: "Post" }
}
end
let(:old_comment) { create(:comment, commentable: post) }
it "picks up post from params" do
expect(assigns(:commentable)).to eq(post)
end
it "assigns the old comments as @comments" do
expect(assigns(:comments)).to eq [old_comment]
end
end
describe "with valid planting params" do
let!(:old_comment) { create(:comment, commentable: planting) }
before do
get :new, params: {
comment: { commentable_id: planting.to_param, commentable_type: "Planting" }
}
end
it "picks up planting from params" do
expect(assigns(:commentable)).to eq(planting)
end
it "assigns the old comments as @comments" do
expect(assigns(:comments)).to eq [old_comment]
end
end
it "dies if no post specified" do
get :new
expect(response).not_to be_successful
end
end
describe "GET edit" do
let(:post) { create(:post) }
before { get :edit, params: { id: comment.to_param } }
describe "my comment" do
let!(:comment) { create(:comment, author: member, commentable: post) }
let!(:old_comment) { create(:comment, commentable: post, created_at: Time.zone.yesterday) }
it "assigns previous comments as @comments" do
expect(assigns(:comments)).to eq([comment, old_comment])
end
end
describe "not my comment" do
let(:comment) { create(:comment, commentable: post) }
it { expect(response).not_to be_successful }
end
end
describe "POST create" do
let(:planting) { create(:planting) }
it "creates a comment on a planting" do
expect do
post :create, params: {
comment: {
commentable_id: planting.id,
commentable_type: "Planting",
body: "Great planting!"
}
}
end.to change(Comment, :count).by(1)
expect(response).to redirect_to(planting)
expect(Comment.last.commentable).to eq(planting)
end
end
describe "PUT update" do
before { put :update, params: { id: comment.to_param, comment: valid_attributes } }
describe "my comment" do
let(:comment) { create(:comment, author: member) }
it "redirects to the comment's post" do
expect(response).to redirect_to(comment.commentable)
end
end
describe "not my comment" do
let(:comment) { create(:comment) }
it { expect(response).not_to be_successful }
end
describe "attempting to change post_id" do
let(:post) { create(:post, subject: 'our post') }
let(:other_post) { create(:post, subject: 'the other post') }
let(:valid_attributes) { { commentable_type: "Post", commentable_id: other_post.id, body: "kōrero" } }
let(:comment) { create(:comment, author: member, commentable: post) }
it "does not change post_id" do
comment.reload
expect(comment.commentable_id).to eq(post.id)
end
end
end
describe "DELETE destroy" do
before { delete :destroy, params: { id: comment.to_param } }
describe "my comment" do
let(:comment) { create(:comment, author: member) }
it "redirects to the post the comment was on" do
expect(response).to redirect_to(comment.commentable)
end
end
describe "not my comment" do
let(:comment) { create(:comment) }
it { expect(response).not_to be_successful }
end
end
end