Files
growstuff/spec/views/posts/show.html.haml_spec.rb
google-labs-jules[bot] 8564ec7a7c Add comments to photos (#4130)
* Add comments to photos

Extend the photo show page to support comments by logged in users.

- Make the Comment model polymorphic.
- Update the Photo and Post models to have comments.
- Update the comments controller to handle the polymorphic association.
- Update the photo show page to display comments and a comment form.
- Create a reusable comments partial.

* Add migration

* Fix tests

* Fix tests

* Slightly fix tests

* Fix variables

* Add field

* Refactor slightly

* Refactor slightly

* Refactor slightly

* Refactor

* Photos respond to this as well

* Refactor to polymorphic_url

* Rename

* Wrong relationship

* Refactor and fix tests

* Fix relationships

* Fix rendering

* Fix tests

* Fix model tests

* Fix test

* Fix test

* Fix test

* Fix test

* Fix controller spec

* Fix view tests

---------

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
Co-authored-by: Daniel O'Connor <daniel.oconnor@gmail.com>
2025-08-24 21:10:16 +09:30

128 lines
3.8 KiB
Ruby

# frozen_string_literal: true
require 'rails_helper'
describe "posts/show" do
subject { rendered }
let(:author) { FactoryBot.create(:member, login_name: 'mary') }
before do
controller.stub(:current_user) { nil }
assign(:post, post)
end
describe 'render post' do
before { render }
describe "basic post" do
let(:post) { FactoryBot.create(:post, author:, body: 'hello there') }
# show the name of the member who posted the post
it { is_expected.to have_text author.login_name }
# Subject goes in title
it { is_expected.to have_text('hello there') }
# shouldn't show the subject on a single post page
# (it appears in the title/h1 via the layout, not via this view)
it { is_expected.to have_no_text('An Update') }
end
describe "should parse markdown into html" do
let(:post) { FactoryBot.create(:markdown_post, author:) }
it { assert_select "strong", "strong" }
end
describe "shouldn't let html through in body" do
let(:post) { FactoryBot.create(:post, author:, body: '<a href="http://evil.com">EVIL</a>') }
it { is_expected.to have_content('EVIL') }
it { is_expected.to have_no_link("http://evil.com") }
end
describe 'script tag in post body' do
let(:post) { FactoryBot.create(:post, author:, body: "<script>alert('hakker!')</script>") }
it { is_expected.to have_no_selector('script') }
end
describe 'script tag in post title' do
let(:post) { FactoryBot.create(:post, author:, subject: "<script>alert('hakker!')</script>") }
it { is_expected.to have_no_selector('script') }
end
describe 'has an anchor to the comments' do
let(:post) { FactoryBot.create(:post, author:) }
it { is_expected.to have_selector('a[name=comments]') }
end
end
context "when there is one comment" do
let(:post) { FactoryBot.create(:html_post, author:) }
let!(:comment) { FactoryBot.create(:comment, commentable: post) }
before do
@comments = post.comments
render
end
it 'shows comment count only 1' do
assert_select "div.post_comments", false
end
it "shows comments" do
expect(subject).to have_content comment.body
end
it 'has an anchor to the comments' do
assert_select 'a[name=comments]'
end
end
context "when there is more than one comment" do
let(:post) { FactoryBot.create(:html_post, author:) }
before do
@comment1 = FactoryBot.create(:comment, commentable: post, body: "F1rst!!!",
created_at: Date.new(2010, 5, 17))
@comment3 = FactoryBot.create(:comment, commentable: post, body: "Th1rd!!!",
created_at: Date.new(2012, 5, 17))
@comment4 = FactoryBot.create(:comment, commentable: post, body: "F0urth!!!")
@comment2 = FactoryBot.create(:comment, commentable: post, body: "S3c0nd!!1!",
created_at: Date.new(2011, 5, 17))
@comments = post.comments
render
end
it "shows the oldest comments first" do
expect(subject).to have_content(/#{@comment1.body}.*#{@comment2.body}.*#{@comment3.body}.*#{@comment4.body}/m)
end
end
context "forum post" do
let(:post) { FactoryBot.create(:forum_post, author:) }
before { render }
it "shows forum name" do
expect(subject).to have_content "in #{post.forum.name}"
end
end
context "signed in" do
let(:post) { FactoryBot.create(:post, author:) }
before do
sign_in author
controller.stub(:current_user) { author }
render
end
it 'shows a comment button' do
expect(subject).to have_link "Comment", href: new_comment_path(comment: { commentable_type: "Post", commentable_id: post.id })
end
end
end