Skip to content

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

3.5 KB
Raw Blame
# frozen_string_literal: true

RSpec.describe DiscourseSolved::QuestionSchemaSerializer do
  fab!(:user)
  fab!(:topic) { Fabricate(:topic, user: user) }
  fab!(:post) { Fabricate(:post, topic: topic, user: user, like_count: 2) }

  context "without an accepted answer" do
    subject(:json) { described_class.new(topic, root: false).serializable_hash.deep_stringify_keys }

    it "includes the Question @type" do
      expect(json["@type"]).to eq("Question")
    end

    it "serializes the topic attributes" do
      expect(json["name"]).to eq(topic.title)
      expect(json["text"]).to be_present
      expect(json["upvoteCount"]).to eq(2)
      expect(json["answerCount"]).to eq(0)
      expect(json["datePublished"]).to eq(topic.created_at)
      expect(json["dateModified"]).to be_present
    end

    it "serializes the author" do
      expect(json["author"]).to eq(
        { "@type" => "Person", "name" => user.username, "url" => user.full_url },
      )
    end

    it "does not include acceptedAnswer or suggestedAnswer" do
      expect(json).not_to have_key("acceptedAnswer")
      expect(json).not_to have_key("suggestedAnswer")
    end
  end

  context "with an accepted answer" do
    fab!(:answer_user, :user)
    fab!(:answer_post) { Fabricate(:post, topic: topic, user: answer_user, like_count: 7) }

    subject(:json) do
      described_class
        .new(topic, root: false, accepted_answers: [answer_post])
        .serializable_hash
        .deep_stringify_keys
    end

    it "sets answerCount to 1" do
      expect(json["answerCount"]).to eq(1)
    end

    it "includes accepted answer as an array of Answer objects" do
      accepted = json["acceptedAnswer"].sole
      expect(accepted["@type"]).to eq("Answer")
      expect(accepted["text"]).to be_present
      expect(accepted["datePublished"]).to be_present
      expect(accepted["dateModified"]).to be_present
      expect(accepted["upvoteCount"]).to eq(7)
      expect(accepted["author"]).to eq(
        { "@type" => "Person", "name" => answer_user.username, "url" => answer_user.full_url },
      )
    end
  end

  context "with suggested answers" do
    fab!(:answer_post) { Fabricate(:post, topic: topic) }
    fab!(:suggested_post) { Fabricate(:post, topic: topic) }

    subject(:json) do
      described_class
        .new(
          topic,
          root: false,
          accepted_answers: [answer_post],
          suggested_answers: [suggested_post],
        )
        .serializable_hash
        .deep_stringify_keys
    end

    it "sets answerCount to total of accepted and suggested" do
      expect(json["answerCount"]).to eq(2)
    end

    it "includes suggestedAnswer as an array of Answer objects" do
      suggested = json["suggestedAnswer"].sole
      expect(suggested["@type"]).to eq("Answer")
      expect(suggested["text"]).to be_present
      expect(suggested["datePublished"]).to be_present
      expect(suggested["dateModified"]).to be_present
      expect(suggested["upvoteCount"]).to eq(0)
    end
  end

  context "with a first post longer than the excerpt limit" do
    fab!(:long_topic, :topic)
    fab!(:long_post) do
      Fabricate(:post, topic: long_topic, raw: Array.new(120) { |i| "word#{i}" }.join(" "))
    end

    subject(:json) do
      described_class.new(long_topic, root: false).serializable_hash.deep_stringify_keys
    end

    it "uses the full text of the first post" do
      expect(long_post.raw.length).to be > SiteSetting.post_excerpt_maxlength
      expect(json["text"]).to eq(long_post.raw)
    end
  end
end