require "rails_helper" RSpec.describe Comment, type: :model do let(:user) { create(:user, created_at: 3.weeks.ago) } let(:user2) { create(:user) } let(:article) { create(:article, user_id: user.id, published: true) } let(:article_with_video) { create(:article, :video, user_id: user.id, published: true) } let(:comment) { create(:comment, user_id: user2.id, commentable_id: article.id) } let(:video_comment) { create(:comment, user_id: user2.id, commentable_id: article_with_video.id) } let(:comment_2) { create(:comment, user_id: user2.id, commentable_id: article.id) } let(:child_comment) do build(:comment, user_id: user.id, commentable_id: article.id, parent_id: comment.id) end describe "validations" do subject { described_class.new(user: user, commentable: article) } let(:article) { Article.new(user: user2) } before do allow(article).to receive(:touch).and_return(true) end it { is_expected.to belong_to(:user) } it { is_expected.to belong_to(:commentable) } it { is_expected.to have_many(:reactions).dependent(:destroy) } it { is_expected.to have_many(:mentions).dependent(:destroy) } it { is_expected.to have_many(:notifications).dependent(:delete_all) } it { is_expected.to have_many(:notification_subscriptions).dependent(:destroy) } it { is_expected.to validate_presence_of(:commentable_id) } it { is_expected.to validate_presence_of(:body_markdown) } it { is_expected.to validate_uniqueness_of(:body_markdown).scoped_to(:user_id, :ancestry, :commentable_id, :commentable_type) } it { is_expected.to validate_length_of(:body_markdown).is_at_least(1).is_at_most(25_000) } it { is_expected.to validate_inclusion_of(:commentable_type).in_array(%w[Article PodcastEpisode]) } end it "gets proper generated ID code" do comment = described_class.new(id: 1) expect(comment.id_code_generated).to eq(comment.id.to_s(26)) end it "generates character count before saving" do expect(comment.markdown_character_count).to eq(comment.body_markdown.size) end describe "#processed_html" do let(:comment) { create(:comment, commentable: article, body_markdown: "# hello\n\nhy hey hey") } it "converts body_markdown to proper processed_html" do expect(comment.processed_html.include?("