|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +describe TopicViewSerializer do |
| 4 | + fab!(:topic) |
| 5 | + fab!(:post1) { Fabricate(:post, topic:) } |
| 6 | + fab!(:post2) { Fabricate(:post, topic:) } |
| 7 | + fab!(:user) |
| 8 | + |
| 9 | + before { SiteSetting.solved_enabled = true } |
| 10 | + |
| 11 | + describe "#accepted_answer" do |
| 12 | + it "returns the accepted answer post when the topic has an accepted answer" do |
| 13 | + Fabricate(:solved_topic, topic: topic, answer_post: post2) |
| 14 | + serializer = TopicViewSerializer.new(TopicView.new(topic), scope: Guardian.new(user)) |
| 15 | + serialized = serializer.as_json |
| 16 | + expect(serialized[:topic_view][:accepted_answer][:post_number]).to eq(post2.post_number) |
| 17 | + end |
| 18 | + |
| 19 | + it "returns nil when the topic does not have an accepted answer" do |
| 20 | + unsolved_topic = Fabricate(:topic) |
| 21 | + serializer = TopicViewSerializer.new(TopicView.new(unsolved_topic), scope: Guardian.new(user)) |
| 22 | + serialized = serializer.as_json |
| 23 | + expect(serialized[:accepted_answer]).to be_nil |
| 24 | + end |
| 25 | + |
| 26 | + it "returns nil when the accepted answer post does not exist" do |
| 27 | + weird_topic = Fabricate(:solved_topic) |
| 28 | + weird_topic.update!(answer_post_id: 19_238_319) |
| 29 | + serializer = |
| 30 | + TopicViewSerializer.new(TopicView.new(weird_topic.topic), scope: Guardian.new(user)) |
| 31 | + serialized = serializer.as_json |
| 32 | + expect(serialized[:accepted_answer]).to be_nil |
| 33 | + end |
| 34 | + end |
| 35 | +end |
0 commit comments