Skip to content

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

1.4 KB
Raw Blame
# frozen_string_literal: true

describe PostVoting::PostSerializerExtension do
  fab!(:user)
  fab!(:topic) { Fabricate(:topic, subtype: Topic::POST_VOTING_SUBTYPE) }
  fab!(:topic_post) { Fabricate(:post, topic: topic) }
  fab!(:answer) { Fabricate(:post, topic: topic) }
  fab!(:comment) { Fabricate(:post_voting_comment, post: answer) }
  let(:topic_view) { TopicView.new(topic, user) }
  let(:up) { PostVotingVote.directions[:up] }
  let(:guardian) { Guardian.new(user) }

  let(:serialized) do
    serializer = PostSerializer.new(answer, scope: guardian, root: false)
    serializer.topic_view = topic_view
    serializer.as_json
  end

  context "with qa enabled" do
    before { SiteSetting.post_voting_enabled = true }

    it "returns the post-voting attributes" do
      PostVoting::VoteManager.vote(answer, user, direction: up)

      expect(serialized[:post_voting_vote_count]).to eq(1)
      expect(serialized[:post_voting_user_voted_direction]).to eq(up)
      expect(serialized[:comments_count]).to eq(1)
      expect(serialized[:comments].first[:id]).to eq(comment.id)
    end
  end

  context "with qa disabled" do
    before { SiteSetting.post_voting_enabled = false }

    it "does not include dependent_keys" do
      expect(serialized[:qa_vote_count]).to eq(nil)
      expect(serialized[:qa_user_voted_direction]).to eq(nil)
      expect(serialized[:comments_count]).to eq(nil)
      expect(serialized[:comments]).to eq(nil)
    end
  end
end