Skip to content

A platform for community discussion. Free, open, simple.

3.7 KB
Raw Blame
# frozen_string_literal: true

describe Topic do
  fab!(:user)
  fab!(:category)
  fab!(:topic) { Fabricate(:topic, category: category, subtype: Topic::POST_VOTING_SUBTYPE) }
  fab!(:topic_post) { Fabricate(:post, topic: topic) }

  fab!(:answers) { 5.times.map { Fabricate(:post, topic: topic) }.sort_by(&:post_number) }

  fab!(:comments) do
    answer = answers.first

    5.times.map { Fabricate(:post_voting_comment, post: answer) }.sort_by(&:created_at)
  end

  let(:up) { PostVotingVote.directions[:up] }

  describe "validations" do
    describe "#subtype" do
      it "rejects Post Voting topics when post_voting_enabled is disabled" do
        SiteSetting.post_voting_enabled = false

        topic =
          Fabricate.build(:topic, archetype: Archetype.default, subtype: Topic::POST_VOTING_SUBTYPE)

        expect(topic.valid?).to eq(false)
        expect(topic.errors.full_messages).to eq(
          [I18n.t("topic.post_voting.errors.post_voting_not_enabled")],
        )
      end

      it "rejects changing an existing topic to the Post Voting subtype" do
        topic_2 = Fabricate(:topic)
        topic_2.subtype = Topic::POST_VOTING_SUBTYPE

        expect(topic_2.valid?).to eq(false)
        expect(topic_2.errors.full_messages).to eq(
          [I18n.t("topic.post_voting.errors.cannot_change_to_post_voting_subtype")],
        )
      end

      it "allows the Post Voting subtype only on regular topics" do
        topic =
          Fabricate.build(:topic, archetype: Archetype.default, subtype: Topic::POST_VOTING_SUBTYPE)

        expect(topic.valid?).to eq(true)

        topic.archetype = Archetype.private_message

        expect(topic.valid?).to eq(false)
        expect(topic.errors.full_messages).to eq(
          [I18n.t("topic.post_voting.errors.subtype_not_allowed")],
        )
      end
    end
  end

  it "returns the topic's comments" do
    comment_ids = comments.map(&:id)
    topic_comment_ids = topic.comments.pluck(:id)

    expect(comment_ids).to eq(topic_comment_ids)
  end

  it "returns the topic's answers" do
    answer_ids = answers.map(&:id)
    topic_answer_ids = topic.answers.pluck(:id)

    expect(answer_ids).to eq(topic_answer_ids)
  end

  it "returns the answer count" do
    expect(topic.answers.size).to eq(answers.size)
  end

  it "returns the last answer's timestamp" do
    expected = answers.last.created_at

    expect(topic.last_answered_at).to eq_time(expected)
  end

  it "returns the last comment's timestamp" do
    expected = comments.last.created_at

    expect(topic.last_commented_on).to eq_time(expected)
  end

  it "returns the last answer's post number" do
    expected = answers.last.post_number

    expect(topic.last_answer_post_number).to eq(expected)
  end

  it "returns the last answer's author" do
    expected = answers.last.user.id

    expect(topic.last_answerer.id).to eq(expected)
  end

  describe "#is_post_voting?" do
    before { SiteSetting.post_voting_enabled = true }

    it "returns false when the topic renders as nested" do
      SiteSetting.nested_replies_enabled = true
      Fabricate(:nested_topic, topic: topic)

      expect(topic).not_to be_is_post_voting
    end
  end

  describe ".post_voting_votes" do
    it "returns nil if user is blank" do
      expect(Topic.post_voting_votes(topic, nil)).to eq(nil)
    end

    it "returns nil if disabled" do
      SiteSetting.post_voting_enabled = false

      expect(Topic.post_voting_votes(topic, user)).to eq(nil)
    end

    it "returns voted post IDs" do
      expected =
        answers
          .first(3)
          .map do |a|
            PostVoting::VoteManager.vote(a, user, direction: up)

            a.id
          end
          .sort

      expect(Topic.post_voting_votes(topic, user).pluck(:votable_id)).to contain_exactly(*expected)
    end
  end
end